Find integers such that A contains exactly X distinct integers greater than A{i]
Given an array A[] of N integers, the task is to print the number of integers i from 1 to N, for which A contains exactly X distinct integers (for X = 0 to N-1) greater than Ai
Examples:
Input: N = 6, A[] = {2, 7, 1, 8, 2, 8}
Output: {2, 1, 2, 1, 0, 0}
Explanation: Let us consider X = 2.
A[1] = 2: A contains 2 distinct integers greater than 2 (7 and 8)
A[2] = 7: A contains 1 distinct integer greater than 7 (8)
A[3] = 1: A contains 3 distinct integers greater than 1 (2, 7 and 8)
A[4] = 8: A doesn’t contain any distinct integers greater than 8
A[5] = 2: A contains 2 distinct integers greater than 2 (7 and 8)
A[6] = 8: A doesn’t contain any distinct integers greater than 8
So, the given condition is satisfied for i=1 and 5 in case of X=2.
Input: N = 1, A[] = {1}
Output: 1
Approach: To solve the problem follow the below observations:
Observations:
If we store frequency of each element of array in a hashmap and sort it in decreasing order by the keys, then:
Let the hashmap be – ((u1, f1), (u2, f2)….(un, fn)) where fi is the frequency of element ui and u1 > u2 > ……un.
- For each i = 1 to n, there are i – 1 distinct integers that is greater than ui. So, for each X=0 to n-1 the answer for X would be fX+1.
- Answer for X = n to N – 1 would be 0.
Based on the above observation following approach can be used to solve the problem:
- Declare a map (say mp) and insert the elements of array A into it.
- Iterate the map from the end and print the frequencies of the elements.
- For the remaining elements (i.e. N-n), print N-n zeroes.
Following is the code based on the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
void Solve( int N, int A[])
{
map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[A[i]]++;
}
int n = ( int )mp.size();
for ( auto it = mp.rbegin(); it != mp.rend(); it++) {
cout << it->second << " " ;
}
for ( int i = 1; i <= N - n; i++) {
cout << 0 << " " ;
}
}
int32_t main()
{
int N = 6;
int A[] = { 2, 7, 1, 8, 2, 8 };
Solve(N, A);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void Solve( int N, int A[])
{
Map<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < N; i++)
{
if (mp.containsKey(A[i]))
{
mp.put(A[i], mp.get(A[i]) + 1 );
}
else
{
mp.put(A[i], 1 );
}
}
ArrayList<Integer> keys = new ArrayList<Integer>(mp.keySet());
int n = keys.size();
for ( int i=n- 1 ; i >= 0 ; i--){
System.out.print(mp.get(keys.get(i)) + " " );
}
for ( int i = 1 ; i <= N - n; i++) {
System.out.print( "0 " );
}
}
public static void main (String[] args) {
int N = 6 ;
int A[] = { 2 , 7 , 1 , 8 , 2 , 8 };
Solve(N, A);
}
}
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void Solve( int N, int [] A)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (mp.ContainsKey(A[i])) {
mp[A[i]]++;
}
else {
mp[A[i]] = 1;
}
}
int n = mp.Count;
List< int > keys = new List< int >(mp.Keys);
keys.Sort();
keys.Reverse();
foreach ( int key in keys) {
Console.Write(mp[key] + " " );
}
for ( int i = 1; i <= N - n; i++) {
Console.Write( "0 " );
}
}
public static void Main()
{
int N = 6;
int [] A = { 2, 7, 1, 8, 2, 8 };
Solve(N, A);
}
}
|
Javascript
function Solve(N, A){
let mp = {};
for (let i = 0; i < N; i++) {
if (mp[A[i]]) {
mp[A[i]]++;
} else {
mp[A[i]] = 1;
}
}
let n = Object.keys(mp).length;
let freqArr = [];
for (let key in mp) {
freqArr.push(mp[key]);
}
freqArr.reverse();
console.log(freqArr.join( ' ' )+ " " );
for (let i = 1; i <= N - n; i++) {
console.log(0+ " " );
}
}
let N = 6;
let A = [2, 7, 1, 8, 2, 8];
Solve(N, A);
|
Python
def solve(N, A):
mp = {}
for i in range (N):
if A[i] in mp:
mp[A[i]] + = 1
else :
mp[A[i]] = 1
n = len (mp)
freqArr = []
for key in mp:
freqArr.append(mp[key])
print (( map ( str , freqArr)))
for i in range (N - n):
print ( '0 ' )
N = 6
A = [ 2 , 7 , 1 , 8 , 2 , 8 ]
solve(N, A)
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Last Updated :
21 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...