Given an array A[] consisting of N integers, the task is to construct an array B[] such that for every ith index, B[i] = X – Y, where X and Y are the count of occurrences of A[i] after and before the ith index.
Examples:
Input: A[] = {3, 2, 1, 2, 3}
Output: 1 1 0 -1 -1
Explanation:
arr[0] = 3, X = 1, Y = 0. Therefore, print 1.
arr[1] = 2, X = 1, Y = 0. Therefore, print 1.
arr[2] = 1, X = 0, Y = 0. Therefore, print 0.
arr[3] = 2, X = 0, Y = 1. Therefore, print -1.
arr[4] = 3, X = 0, Y = 1. Therefore, print -1.Input: A[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Naive Approach:
The simplest approach to solve the problem is to travere the array and consider every array element and compare it with all the elements on its left and right. For every array element, print the difference in its cunt of occurrences in its left and right.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: Follow the steps below to optimize the above approach:
- Initialize two arrays left[] and right[] to store frequencies of array elements present on the left and right indices of every array element.
- Compute the left and right cumulative frequency tables.
- Print the difference of same indexed elements from the two frequency arrays.
Below is the implementation of the above approach:
C++
// C++ program of the above approach #include <bits/stdc++.h> using namespace std; // Function to construct array of // differences of counts on the left // and right of the given array void constructArray(int A[], int N) { // Initialize left and right // frequency arrays int left[N + 1] = { 0 }; int right[N + 1] = { 0 }; int X[N + 1] = { 0 }, Y[N + 1] = { 0 }; // Construct left cumulative // frequency table for (int i = 0; i < N; i++) { X[i] = left[A[i]]; left[A[i]]++; } // Construct right cumulative // frequency table for (int i = N - 1; i >= 0; i--) { Y[i] = right[A[i]]; right[A[i]]++; } // Print the result for (int i = 0; i < N; i++) { cout << Y[i] - X[i] << " "; } } // Driver Code int main() { int A[] = { 3, 2, 1, 2, 3 }; int N = sizeof(A) / sizeof(A[0]); // Function Call constructArray(A, N); return 0; } |
1 1 0 -1 -1
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Generate Array whose difference of each element with its left yields the given Array
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Replace every element with the least greater element on its right
- Largest element smaller than current element on left for every element in Array
- Replace every element of array with sum of elements on its right side
- Find Array formed by adding each element of given array with largest element in new array to its left
- Replace elements with absolute difference of smallest element on left and largest element on right
- Count occurrences of a prime number in the prime factorization of every element from the given range
- Find the difference of count of equal elements on the right and the left for each element
- Longest subsequence of a number having same left and right rotation
- Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
- Count greater elements on the left side of every array element
- Maximize the number of indices such that element is greater than element to its left
- Find an element in array such that sum of left array is equal to sum of right array
- Count of elements such that difference between sum of left and right sub arrays is equal to a multiple of k
- Replace every element with the greatest element on right side
- Replace every element of the array by its previous element
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.

