Sort the given array first. : O(n logn)
You'll have:
{2,3,3,5,5,5}
First parse all the elements in the array and identify the unique ones and
store the count in N
Now create a two dimensional array A[N][2];
Store the number in A[0][0] and store its frequency in A[0][1] etc.
: O (n)
Now, reverse sort this array with respect to the second column. i.e.
frequency. : O(m logm) where m <= n
Modify the given array wrt the elements in the new sorted array: O(n)
Total Complexity.
Time Complexity: O(n logn)
Space Complexity: O(2m) m <= n
Anyone have a better idea?
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.