Given an array arr[] of size N and an integer K, the task is to find all the array elements that appear more than (N /K) times.

0

Given an array arr[] of size N and an integer K, the task is to find all the array elements that appear more than (N /K) times.


Cpp Program Code:


In this code, we first create an unordered map freqMap to store the frequency of each element in the array. Then we iterate through the map and print all the elements whose frequency is greater than N/K, which is 6 in this case.

Python Program code:

In this code, we first calculate the frequency (freq) of each element that should be present in the array for an element to occur more than (N/K) times. Then we use the collections.Counter class to count the frequency of each element in the given array. Finally, we iterate over the items in the Counter object and append the keys to the result list if their values are greater than freq.




Post a Comment

0Comments
Post a Comment (0)