Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Generate array having differences between count of occurrences of every array element on its left and right

  • Last Updated : 30 Sep, 2021

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: 

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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 traverse 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:

  1. Initialize two arrays left[] and right[] to store frequencies of array elements present on the left and right indices of every array element.
  2. Compute the left and right cumulative frequency tables.
  3. 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;
}

Java




// Java program of the above approach
import java.io.*;
 
class GFG{
 
// Function to construct array of
// differences of counts on the left
// and right of the given array
static void constructArray(int A[], int N)
{
     
    // Initialize left and right
    // frequency arrays
    int[] left = new int[N + 1];
    int[] right = new int[N + 1];
    int[] X = new int[N + 1];
    int[] Y = new int[N + 1];
 
    // 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++)
    {
        System.out.print(Y[i] - X[i] + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int A[] = { 3, 2, 1, 2, 3 };
    int N = A.length;
 
    // Function Call
    constructArray(A, N);
}
}
 
// This code is contributed by akhilsaini

Python3




# Python3 program of the above approach
 
# Function to construct array of
# differences of counts on the left
# and right of the given array
def constructArray(A, N):
     
    # Initialize left and right
    # frequency arrays
    left = [0] * (N + 1)
    right = [0] * (N + 1)
    X = [0] * (N + 1)
    Y = [0] * (N + 1)
 
    # Construct left cumulative
    # frequency table
    for i in range(0, N):
        X[i] = left[A[i]]
        left[A[i]] += 1
 
    # Construct right cumulative
    # frequency table
    for i in range(N - 1, -1, -1):
        Y[i] = right[A[i]]
        right[A[i]] += 1
 
    # Print the result
    for i in range(0, N):
        print(Y[i] - X[i], end = " ")
 
# Driver Code
if __name__ == '__main__':
 
    A = [ 3, 2, 1, 2, 3 ]
    N = len(A)
 
    # Function Call
    constructArray(A, N)
 
# This code is contributed by akhilsaini

C#




// C# program of the above approach
using System;
 
class GFG{
 
// Function to construct array of
// differences of counts on the left
// and right of the given array
static void constructArray(int[] A, int N)
{
     
    // Initialize left and right
    // frequency arrays
    int[] left = new int[N + 1];
    int[] right = new int[N + 1];
    int[] X = new int[N + 1];
    int[] Y = new int[N + 1];
 
    // 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++)
    {
        Console.Write(Y[i] - X[i] + " ");
    }
}
 
// Driver Code
public static void Main()
{
    int[] A = { 3, 2, 1, 2, 3 };
    int N = A.Length;
 
    // Function Call
    constructArray(A, N);
}
}
 
// This code is contributed by akhilsaini

Javascript




<script>
 
// JavaScript program of the above approach
 
// Function to construct array of
// differences of counts on the left
// and right of the given array
function constructArray(A, N)
{
      
    // Initialize left and right
    // frequency arrays
    let left = [];
    let right = [];
    let X = [];
    let Y = [];
     
    for(let i = 0; i < N; i++)
    {
        X[i] = 0;
        left[i] = 0;
        right[i] = 0;
        Y[i]= 0;
    }
  
    // Construct left cumulative
    // frequency table
    for(let i = 0; i < N; i++)
    {
        X[i] = left[A[i]];
        left[A[i]]++;
    }
  
    // Construct right cumulative
    // frequency table
    for(let i = N - 1; i >= 0; i--)
    {
        Y[i] = right[A[i]];
        right[A[i]]++;
    }
  
    // Print the result
    for(let i = 0; i < N; i++)
    {
        document.write(Y[i] - X[i] + " ");
    }
}
 
// Driver Code
let A = [ 3, 2, 1, 2, 3 ];
let N = A.length;
 
// Function Call
constructArray(A, N);
 
// This code is contribute by target_2
 
</script>
Output: 
1 1 0 -1 -1

 

Time Complexity: O(N)
Auxiliary Space: O(N)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :