An array having number up to n natural numbers only k swaps are allowed
Answers
Answered by
0
A simple method is to pick all elements one by one. For every picked element, count its occurrences by traversing the array, if count becomes more than n/k, then print the element. Time Complexity of this method would be O(n2).
A better solution is to use sorting. First, sort all elements using a O(nLogn) algorithm. Once the array is sorted, we can find all required elements in a linear scan of array. So overall time complexity of this method is O(nLogn) + O(n) which is O(nLogn).
Similar questions
History,
7 months ago
English,
7 months ago
Hindi,
7 months ago
Biology,
1 year ago
Business Studies,
1 year ago