Company: Barclays
Difficulty: medium
Frequency Sort C++14 GCC 5.5.0 Design a way to sort the list of positive integers in the descending order according to frequency of the elements. The elements with higher frequency come before those with lower frequency. Elements with the same frequency come in the same order as they appear in the given list. Input Format First line: Integer num , representing the number of elements in the list (N) Second line: N space-separated integers representing the elements in the list Output Format Print N space-separated integers representing the elements of the list sorted according to the frequency of elements present in the given list. Example Input: 19 1 2 2 3 3 3 4 4 5 5 5 5 5 6 6 6 7 8 9 10 Output: 5 5 5 5 5 3 3 3 6 6 6 2 2 4 4 1 7 8 9 10 Explanation: The element 5 has highest frequency. The elements 3 and 6 have same frequencies. So, their original order has been maintained in the output. Similarly the frequencies of rest of elements will be found and arranged. So, the output will be: 5