Group of numbers
You are given an array o that contains N Integers. All the Integers in the array may not be distinct
The number of repetitions of each Integer In the array is represented by ri. Your task is to print the Integers in the decreasing order of their occurrence in the array.
Note
lfri > j. then o; must be printed before
"j
then among aż and a; whichever has a larger value, is printed first,
Here, rzandi
are the number of repetitions of Integers
andaj
In the array
Note: Please read the sample explanation carefully to understand the task that you have to perform
Input format
The first line contains an Integer N.
The second line contains N space-separated Integers representing the elements of array e.
Output format
Print the space-separated Integers in the decreasing order of thelrocourrence in the array. The output must be printed in a single line.
Constraints
1<N<1000
i1<=ai<=1000
Answers
Program in C++:
#include<iostream>
using namespace std;
int main()
{
int N;
cout<<"Enter the number of elements in the array: ";
cin>>N;
int A[N];
cout<<"Enter the elements : ";
for(int i = 0; i < N; i++)
{
cin>>A[i];
}
int B[N];
for(int i = 0; i < N; i++)
{
int count = 1;
for(int j = i+1; j < N; j++)
{
if(A[i] == A[j])
{
count++;
for(int k = j; k < N; k++)
{
A[k] = A[k+1];
}
j--;
N--;
}
}
B[i] = count;
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N-i-1; j++)
{
if(B[j] < B[j+1])
{
int temp = B[j];
B[j] = B[j+1];
B[j+1] = temp;
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
for(int i = 0; i < N; i++)
{
for(int j = 0; j < B[i]; j++)
{
cout<<A[i]<<" ";
}
}
return 0;
}
Output 1:
Enter the number of elements in the array: 10
Enter the elements : 1 7 2 6 2 8 3 0 0 5
2 2 0 0 1 7 6 8 3 5
Output 2:
Enter the number of elements in the array: 15
Enter the elements : 5 2 2 1 7 0 7 2 0 0 0 2 6 3 0
0 0 0 0 0 2 2 2 2 7 7 5 1 6 3
Answer:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++)
arr[i] = sc.nextInt();
int maxE = -1;
for (int i = 0; i < n; i++) {
maxE = Math.max(maxE, arr[i]);
}
int[] freq = new int[maxE + 1];
for (int i = 0; i < n; i++) {
freq[arr[i]]++;
}
int maxF = -1;
for (int i = 0; i <= maxE; i++) {
maxF = Math.max(maxF, freq[i]);
}
while(maxF > 0){
for(int i=maxE; i>=0;i--){
if(maxF == freq[i])
System.out.print(i + " ");
}
maxF--;
}
}
}