Group of numbers

0

0 votes
Merge Sort, Sorting, Algorithms, Merge sort
Problem

You are given an array a 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

  • If ri>rj, then ai must be printed before aj
  • If ri==rj, then among ai and aj whichever has a larger value, is printed first.

Here, riand rj are the number of repetitions of integers ai and aj in the array.

Example

Consider an array = [4,4,4,,2,2,1,3,3,1,5] . Now frequencies are : 

1 -> 2                                                              

2 -> 2

3 -> 2

4 -> 3

5 -> 1

So we have to arrange in the decreasing order of their frequencies . So 4 (1,2,3) 5 . Now we have to take care of numbers having same frequencies . So answer is 4 1 2 3 5 . 

Note: Please read the sample explanation carefully to understand the task that you have to perform. 

Function description

Complete the solve function provided in the editor. This function takes the following 2 parameters and returns the answer vector sequence : 

  • N: Represents the size of the array . 
  • a: Represents the array .

Input format

  • The first line contains an integer N.
  • The second line contains N space-separated integers representing the elements of array a.

Output format

Print the space-separated integers in the decreasing order of their occurrence in the array. The output must be printed in a single line.

Constraints

1N1000

1ai1000

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the given sample there are 3 distinct numbers 1 , 2 and 6 with ri values as 2 , 2 and 1. So as per the task given, 2 comes before 1 as it has same ri value but its larger than 1. 1 comes before 6 as its ri value is greater than 6. The output will be 

2 1 6

Editor Image

?