Why its not getting accepted?


whats wrong with this code?

@darkshadow2708 the expected output is
2 1 4 5
whereas your output is
2 1 5 4

@Ishita,Does order matters in such kind of questions?,You can verify the same question with the exact same code get accepted on leetcode. https://leetcode.com/problems/top-k-frequent-elements/submissions/

@darkshadow2708 hackerblocks does not have the feature yet, to accept the answer in any order so to get AC here you’d have to take care of the order. Many websites like leetcode do offer that feature, so it is not mandatory there.

@Ishitagambhir, Can you suggest some possible changes in this code to get AC here?

@darkshadow2708 try sorting the elements in ascending order if they have the same frequency.

@darkshadow2708 if that does not work, you may also try using an ordered map, so that the original order of the elements would stay intact.