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Ā
Ā
Ā
Brute Force Approach:
Brute force approach to solve this problem would be to use nested loops to count the number of elements to the right and left of each element that are equal to it. For each element, we can initialize two counters, one for counting the number of equal elements to the right and the other for counting the number of equal elements to the left. Then we can use nested loops to traverse the array and count the number of equal elements to the right and left of each element. Finally, we can subtract the two counts to get the required result.
- Iterate over the array using a for loop starting from index 0 to index n-1.
- For each element at index i, initialize the variables āright_countā and āleft_countā to 0.
- Use another for loop to iterate over the elements from i+1 to n-1 to count the number of elements to the right of i that are equal to a[i]. Increment āright_countā for each such element.
- Use another for loop to iterate over the elements from i-1 to 0 to count the number of elements to the left of i that are equal to a[i]. Increment āleft_countā for each such element.
- Calculate the difference between āright_countā and āleft_countā for each i.
Below is the implementation of the above approach:
C++
// C++ implementation of the brute force 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) { Ā Ā Ā Ā for ( int i=0; i<n; i++) { Ā Ā Ā Ā Ā Ā Ā Ā int right_count = 0, left_count = 0; Ā Ā Ā Ā Ā Ā Ā Ā for ( int j=i+1; j<n; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[i] == a[j]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā right_count++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā for ( int j=i-1; j>=0; j--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[i] == a[j]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā left_count++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā cout << right_count - left_count << " " ; Ā Ā Ā Ā } } Ā
// 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
import java.util.*; Ā
public class GFG { Ā Ā Ā Ā // Function to find the count of equal elements to the right Ā Ā Ā Ā // minus the count of equal elements to the left for each element Ā Ā Ā Ā static void rightLeft( int [] a, int n) { Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0 ; i < n; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int rightCount = 0 , leftCount = 0 ; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Count equal elements to the right Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = i + 1 ; j < n; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[i] == a[j]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā rightCount++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Count equal elements to the left Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = i - 1 ; j >= 0 ; j--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[i] == a[j]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā leftCount++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā System.out.print(rightCount - leftCount + " " ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā public static void main(String[] args) { Ā Ā Ā Ā Ā Ā Ā Ā int [] a = { 1 , 2 , 3 , 2 , 1 }; Ā Ā Ā Ā Ā Ā Ā Ā int n = a.length; Ā
Ā Ā Ā Ā Ā Ā Ā Ā rightLeft(a, n); Ā Ā Ā Ā } } |
Python
# Function to find the count of equal elements to the right # minus the count of equal elements to the left for each element Ā
Ā
def right_left_counts(arr): Ā Ā Ā Ā n = len (arr) Ā Ā Ā Ā result = [] Ā
Ā Ā Ā Ā for i in range (n): Ā Ā Ā Ā Ā Ā Ā Ā right_count = 0 Ā Ā Ā Ā Ā Ā Ā Ā left_count = 0 Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Count equal elements to the right Ā Ā Ā Ā Ā Ā Ā Ā for j in range (i + 1 , n): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if arr[i] = = arr[j]: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā right_count + = 1 Ā
Ā Ā Ā Ā Ā Ā Ā Ā # Count equal elements to the left Ā Ā Ā Ā Ā Ā Ā Ā for j in range (i - 1 , - 1 , - 1 ): Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if arr[i] = = arr[j]: Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā left_count + = 1 Ā
Ā Ā Ā Ā Ā Ā Ā Ā result.append(right_count - left_count) Ā
Ā Ā Ā Ā return result Ā
Ā
# Driver code if __name__ = = "__main__" : Ā Ā Ā Ā arr = [ 1 , 2 , 3 , 2 , 1 ] Ā Ā Ā Ā result = right_left_counts(arr) Ā Ā Ā Ā print (result) |
C#
using System; Ā
public class GFG { Ā Ā Ā Ā // Function to find the count of equal elements to the Ā Ā Ā Ā // right minus the count of equal elements to the left Ā Ā Ā Ā // for each element Ā Ā Ā Ā static void RightLeft( int [] a, int n) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā for ( int i = 0; i < n; i++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā int rightCount = 0, leftCount = 0; Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Count equal elements to the right Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = i + 1; j < n; j++) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[i] == a[j]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā rightCount++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Count equal elements to the left Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā for ( int j = i - 1; j >= 0; j--) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (a[i] == a[j]) { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā leftCount++; Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(rightCount - leftCount + " " ); Ā Ā Ā Ā Ā Ā Ā Ā } Ā Ā Ā Ā } Ā
Ā Ā Ā Ā public static void Main( string [] args) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā int [] a = { 1, 2, 3, 2, 1 }; Ā Ā Ā Ā Ā Ā Ā Ā int n = a.Length; Ā
Ā Ā Ā Ā Ā Ā Ā Ā RightLeft(a, n); Ā Ā Ā Ā } } |
1 1 0 -1 -1
Time Complexity: O(n^2) because it uses two nested loops to count the equal elements to the right and left of each element.
Space Complexity: O(1), as we are not using extra space.
Approach: An efficient 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 |
Javascript
<script> Ā
Ā Ā Ā Ā // Javascript 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 function right_left(a, n) { Ā Ā Ā Ā Ā Ā Ā // Maps to store the frequency and same Ā Ā Ā Ā // elements to the left of an element Ā Ā Ā let total = new Map(); Ā Ā Ā Ā let left = new Map(); Ā Ā Ā Ā Ā Ā Ā // Count the frequency of each element Ā Ā Ā Ā for (let i = 0; i < n; i++) Ā Ā Ā Ā Ā Ā Ā Ā total.set(a[i], Ā Ā Ā Ā Ā Ā Ā Ā total.get(a[i]) == null ? 1 : Ā Ā Ā Ā Ā Ā Ā Ā total.get(a[i]) + 1); Ā Ā Ā Ā Ā Ā Ā for (let i = 0; i < n; i++) Ā Ā Ā Ā { Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Print the answer for each element Ā Ā Ā Ā Ā Ā Ā Ā document.write((total.get(a[i]) - 1 - Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā (2 * (left.has(a[i]) == true ? Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā left.get(a[i]) : 0))) + " " ); Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Increment it's left frequency Ā Ā Ā Ā Ā Ā Ā Ā left.set(a[i], Ā Ā Ā Ā Ā Ā Ā Ā left.get(a[i]) == null ? 1 : Ā Ā Ā Ā Ā Ā Ā Ā left.get(a[i]) + 1); Ā Ā Ā Ā } } Ā Ā Ā Ā Ā Ā Ā Ā Ā // Driver code Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā let a = [ 1, 2, 3, 2, 1 ]; Ā Ā Ā Ā let n = a.length; Ā Ā Ā Ā Ā Ā Ā right_left(a, n); Ā Ā Ā Ā Ā Ā Ā Ā Ā // This code is contributed by susmitakundugoaldanga. </script> |
1 1 0 -1 -1
Time complexity: O(N), where N is the size of the given array.
Auxiliary space: O(N), as two hashmaps are required to store the frequency of the elements.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!