You are given an array/list 'ARR' of integers of length ‘N’. You are supposed to find all the elements that occur strictly more than floor(N/3) times in the given array/list.
Input Format :
The first line contains a single integer ‘T’ denoting the number of test cases. The test cases follow.
The first line of each test case contains a single integer ‘N’ denoting the number of elements in the array.
The second line contains ‘N’ single space-separated integers denoting the elements of the array/list.
Output Format :
For each test case, return all the majority elements separated by a single space.
The output of every test case will be printed in a separate line.
You may return the majority of elements in any order.
Note :
You don’t need to print anything; It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 100
3 <= N <= 5000
1 <= ARR[i] <= 10^5
Time Limit: 1 sec
I quickly implemented a brute force solution in 2-3 minutes to pass partially. Then implemented a time-efficient solution with 7^3 time complexity.
The idea is to count the number of times each element occurs and if any element occurs more than N/3 times, then include it in our answer.
The steps are as follows:
- Iterate through the array...read more
The idea is to sort the array and then count the number of occurrences of each element in a single traversal.
The steps are as follows:
- Sort the array in non-decreasing order.
- Iterate th...read more
The idea is to store the frequency of each element from the given array/list into the HashMap and then include all the elements with frequencies greater than N/3 in the answer.
Steps a...read more
The idea is to use a modification of Moore's voting algorithm to find candidate elements that may occur more than N/3 times in the given array. We will use the fact th...read more
Top DE Shaw Associate Developer Intern interview questions & answers
Top HR questions asked in DE Shaw Associate Developer Intern
Reviews
Interviews
Salaries
Users/Month