Last Updated: 30 Jan, 2021

Majority Element lll

Moderate
Asked in companies
SamsungAdobeHCL Technologies

Problem statement

You are given an array ‘ARR’ and another integer number ‘K’. Your task is to find the all elements of ‘ARR’ which occur more than or equals to ‘N/K’ times in ‘ARR’ and ‘N’ is the length of array ‘ARR’.

For example:

Given array ‘ARR = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 1, 2 ,0}’ and ‘K = 4’

Answer is {3, 4} because ‘3’ occurs ‘4’ times and ‘4’ occurs ‘3’ times which is more than or equals to ‘12/ 4 =3’.

Input Format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘T’ lines represent the ‘T’ test cases.

The first line of input contains two space-separated integers ‘N’ and ‘K’. Where ‘N’ denotes the number of elements in array ‘ARR’ and ‘K’ is given integer number.

The second line of input contains the ‘N’ space-separated integer which denotes the element of array ‘ARR’ and ‘K’ is given a second integer number.
Output Format:
For every test case, print all elements of ‘ARR’ which occur more than or equals to ‘N/K’ times in ‘ARR’.

Output for each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraint :
1 <= T <= 10^2
1 <= K <= N <= 5*10^3 ( N is multiple of K)
-10^9 <= ARR[i] <= 10^9

Where ‘T’ represents the number of test cases, ‘N’ is the number of elements in array ‘ARR’ , ‘K’ denotes an integer. ‘ARR[i]’ represents the value of the number at ‘i’ position in ‘ARR’.

Time Limit: 1 sec

Approaches

01 Approach

Approach: The basic idea is that to find the occurrence of ‘x’, and if the occurrence of ‘x’ is more than or equals to the ‘n/k’ then it should be a part of the answer.

 

Algorithm:

  1. Create a ‘ANSWER’ list/vector/arraylist.
  2. Iterate a loop ‘i’ from ‘0’ to ‘N - 1’ both (0, and N - 1) are inclusive
    • ‘COUNT' = 1, store the frequency of ‘ARR[i]
    • Iterate a loop ‘j’ from ‘i + 1’ to ‘N - 1’ both (i + 1 and N - 1) are inclusive
      • If ‘ARR[j] == ARR[i]’ then ‘COUNT' = 'COUNT + 1’
    • After the count of ‘ARR [i]’,
      • If ‘COUNT >= N/K then add ‘ARR[i]’ in ‘answer’.
  3. At the end return ‘ANSWER’.

 

Be careful:- 

For some value of ‘K' and 'N’, it is possible that we add the same value in the answer multiple times.

So to avoid duplication we use a data structure like ‘SET’.

02 Approach

Approach: The basic idea is that, store the frequency of each element of ‘ARR’ in a hashmap/dictionary, then find which element has a frequency greater or equals to the ‘N/K’.

 

The algorithm will be -

  1. Create a hashmap ‘HASHMAP’, to store the frequency of every element, and an ‘answer’ list/vector/array to return.
  2. Iterate a loop ‘i’ from ‘0’ to ‘N - 1’
    • If ‘ARR[i]’ is already present in ‘HASHMAP’, then increase the frequency of HASHMAP[ARR[i]] else initialize with frequency ‘1’.
  3. Now we iterate over every key of ‘HASHMAP’.
    • If the value stored in that key is greater than n/k we insert the key into the ‘ANSWER’.
  4. At the end return ‘ANSWER’.