Find the difference of count of equal elements on the right and the left for each element
Given an array arr[] of size N. The task is to find X – Y for each of the element where X is the count of j such that arr[i] = arr[j] and j > i. Y is the count of j such that arr[i] = arr[j] and j < i.
Examples:
Input: arr[] = {1, 2, 3, 2, 1}
Output: 1 1 0 -1 -1
For index 0, X – Y = 1 – 0 = 1
For index 1, X – Y = 1 – 0 = 1
For index 2, X – Y = 0 – 0 = 0
For index 3, X – Y = 0 – 1 = -1
For index 4, X – Y = 0 – 1 = -1
Input: arr[] = {1, 1, 1, 1, 1}
Output: 4 2 0 -2 -4
Approach: An effient approach is to use a map. One map is to store the count of each element in the array and another map to count the number of same elements left to each element.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find the count of equal // elements to the right - count of equal // elements to the left for each of the element void right_left(int a[], int n) { // Maps to store the frequency and same // elements to the left of an element unordered_map<int, int> total, left; // Count the frequency of each element for (int i = 0; i < n; i++) total[a[i]]++; for (int i = 0; i < n; i++) { // Print the answer for each element cout << (total[a[i]] - 1 - (2 * left[a[i]])) << " "; // Increment it's left frequency left[a[i]]++; } } // Driver code int main() { int a[] = { 1, 2, 3, 2, 1 }; int n = sizeof(a) / sizeof(a[0]); right_left(a, n); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to find the count of equal // elements to the right - count of equal // elements to the left for each of the element static void right_left(int a[], int n) { // Maps to store the frequency and same // elements to the left of an element Map<Integer, Integer> total = new HashMap<>(); Map<Integer, Integer> left = new HashMap<>(); // Count the frequency of each element for (int i = 0; i < n; i++) total.put(a[i], total.get(a[i]) == null ? 1 : total.get(a[i]) + 1); for (int i = 0; i < n; i++) { // Print the answer for each element System.out.print((total.get(a[i]) - 1 - (2 * (left.containsKey(a[i]) == true ? left.get(a[i]) : 0))) + " "); // Increment it's left frequency left.put(a[i], left.get(a[i]) == null ? 1 : left.get(a[i]) + 1); } } // Driver code public static void main(String[] args) { int a[] = { 1, 2, 3, 2, 1 }; int n = a.length; right_left(a, n); } } // This code is contributed by Princi Singh |
Python3
# Python3 implementation of the approach # Function to find the count of equal # elements to the right - count of equal # elements to the left for each of the element def right_left(a, n) : # Maps to store the frequency and same # elements to the left of an element total = dict.fromkeys(a, 0); left = dict.fromkeys(a, 0); # Count the frequency of each element for i in range(n) : if a[i] not in total : total[a[i]] = 1 total[a[i]] += 1; for i in range(n) : # Print the answer for each element print(total[a[i]] - 1 - (2 * left[a[i]]), end = " "); # Increment it's left frequency left[a[i]] += 1; # Driver code if __name__ == "__main__" : a = [ 1, 2, 3, 2, 1 ]; n = len(a); right_left(a, n); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; using System.Collections.Generic; class GFG { // Function to find the count of equal // elements to the right - count of equal // elements to the left for each of the element static void right_left(int []a, int n) { // Maps to store the frequency and same // elements to the left of an element Dictionary<int, int> total = new Dictionary<int, int>(); Dictionary<int, int> left = new Dictionary<int, int>(); // Count the frequency of each element for (int i = 0; i < n; i++) { if(total.ContainsKey(a[i])) { total[a[i]] = total[a[i]] + 1; } else{ total.Add(a[i], 1); } } for (int i = 0; i < n; i++) { // Print the answer for each element Console.Write((total[a[i]] - 1 - (2 * (left.ContainsKey(a[i]) == true ? left[a[i]] : 0))) + " "); // Increment it's left frequency if(left.ContainsKey(a[i])) { left[a[i]] = left[a[i]] + 1; } else { left.Add(a[i], 1); } } } // Driver code public static void Main(String[] args) { int []a = { 1, 2, 3, 2, 1 }; int n = a.Length; right_left(a, n); } } // This code is contributed by Rajput-Ji |
1 1 0 -1 -1
Recommended Posts:
- Replace elements with absolute difference of smallest element on left and largest element on right
- Maximum count of elements divisible on the left for any element
- Count of elements on the left which are divisible by current element
- Find maximum difference between nearest left and right smaller elements
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Find an element in array such that sum of left array is equal to sum of right array
- For each element in 1st array count elements less than or equal to it in 2nd array | Set 2
- For each element in 1st array count elements less than or equal to it in 2nd array
- Find Partition Line such that sum of values on left and right is equal
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count elements such that there are exactly X elements with values greater than or equal to X
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Count all distinct pairs with difference equal to k
- Longest subarray having difference in the count of 1's and 0's equal to k
- Count of elements whose absolute difference with the sum of all the other elements is greater than k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



