Asked inDE Shaw,Associate Developer Intern
Majority Element - II

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
CodingNinjas
author
2y

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. 

CodingNinjas
author
2y
Brute Force

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:

  1. Iterate through the array...read more
CodingNinjas
author
2y
Sorting Approach

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:

  1. Sort the array in non-decreasing order.
  2. Iterate th...read more
CodingNinjas
author
2y
HashMap Approach

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

CodingNinjas
author
2y
Moore's Voting Algorithm Approach

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

Add answer anonymously...
DE Shaw Associate Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter