Generate array having differences between count of occurrences of every array element on its left and right
Last Updated :
15 Mar, 2023
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 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 count 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;
}
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 contributed by target_2
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Count of Array elements greater than all elements on its left and next K elements on its right Given an array arr[], the task is to print the number of elements which are greater than all the elements on its left as well as greater than the next K elements on its right. Examples: Input: arr[] = { 4, 2, 3, 6, 4, 3, 2}, K = 2 Output: 2 Explanation: arr[0](= 4): arr[0] is the 1st element in the
14 min read
Count of Arrays of size N having absolute difference between adjacent elements at most 1 Given a positive integer M and an array arr[] of size N and a few integers are missing in the array represented as -1, the task is to find the count of distinct arrays after replacing all -1 with the elements over the range [1, M] such that the absolute difference between any pair of adjacent elemen
15+ min read
Count array elements having at least one smaller element on its left and right side Given an array arr[] of length N, the task is to find the number of elements in array arr[] which contains at least one smaller element on its left and right. Examples: Input: arr[] = {3, 9, 4, 6, 7, 5}Output: 3Explanation: Following 3 array elements satisfy the necessary conditions: arr[1] (= 9) ha
6 min read
Count of Array elements greater than all elements on its left and at least K elements on its right Given an array A[ ] consisting of N distinct integers, the task is to find the number of elements which are strictly greater than all the elements preceding it and strictly greater than at least K elements on its right. Examples: Input: A[] = {2, 5, 1, 7, 3, 4, 0}, K = 3 Output: 2 Explanation: The o
15+ min read
Count ways to generate an array having distinct elements at M consecutive indices Given an array arr[] consisting of N integers in the range [0, M] and an integer M, the task is to count the number of ways to replace all array elements whose value is 0 with non-zero values from the range [0, M] such that all possible M consecutive elements are distinct. Examples: Input: arr[] = {
10 min read
Difference of count of distinct elements present to left and right for each array element Given an array arr[] consisting of N integers, the task for each array element is to find the absolute difference between the count of distinct elements to the left and the right of it in the given array arr[]. Examples: Input: arr[] = {7, 7, 3, 2, 3}Output: 2 2 0 1 2Explanation:Count of distinct el
12 min read
Count of larger elements on right side of each element in an array Given an array arr[] consisting of N integers, the task is to count the number of greater elements on the right side of each array element. Examples: Input: arr[] = {3, 7, 1, 5, 9, 2} Output: {3, 1, 3, 1, 0, 0} Explanation: For arr[0], the elements greater than it on the right are {7, 5, 9}. For arr
15+ min read
Count of array elements which are greater than all elements on its left Given an array arr[] of size n, the task is to count the number of array elements such that all the elements to its left are strictly smaller than it.Note: The first element of the array will be considered to be always satisfying the condition.Examples :Input: arr[] = [2, 4, 5, 6]Output: 4Explanatio
8 min read
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 F
10 min read
Generate an array consisting of most frequent greater elements present on the right side of each array element Given an array A[] of size N, the task is to generate an array B[] based on the following conditions: For every array element A[i], find the most frequent element greater than A[i] present on the right of A[i]. Insert that element into B[].If more than one such element is present on the right, choos
9 min read