Majority Element lll

Moderate
0/80
Average time to solve is 10m
profile
Contributed by
43 upvotes
Asked in companies
HCL TechnologiesPaytm (One97 Communications Limited)Adobe

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’.

Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1:
2
8 4
1 1 2 1 2 4 3 4
6 6
1 1 1 2 2 2
Sample Output 1:
1 2 4
1 2
Explanation For Sample Input 1:
Test Case 1:
Given array ‘ARR = { 1, 1, 2, 1, 2, 4, 3, 4 }’ and ‘K = 2’.

Only 1, 2, 4 has frequency more than or equal to ‘N/K' = 8/4= 2.

Test Case 2:
Given array ‘ARR = { 1, 1, 1, 2, 2, 2 }’ and ‘K = 6’.
‘N/K = 6/6 = 1’ so ‘1’ and ‘2’ both have frequency more than ‘1’.
Sample Input 2:
2
9 3
1 1 1 2 2 2 2 2 2
6 6
1 2 1 2 3 4
Sample Output 2:
1 2
1 2 3 4
Explanation For Sample Input 2:
Test Case 1:
Given array ‘ARR = { 1, 1, 1, 2, 2, 2, 2, 2, 2 }’ and ‘K = 3’.

Both 1, 2 has frequency more than or equal to ‘N/K' = 9/3= 3.

Test Case 2:
Given array ‘ARR = { 1, 2, 1, 2, 3, 4 }’ and ‘K = 6’.
‘N/K = 6/6 = 1’ so ‘1’, '2', '3' and ‘4' all have frequency more than ‘1’.
Hint

Try to count occurrences of every number.

Approaches (2)
Brute Force

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’.

Time Complexity

O(N^2), Where ‘N’ is the size of the given array ‘ARR’.

 

For each ‘i’ from 0 to N - 1 we are iterating for N - i - 1 times.  Thus our total number of iterations will be N + (N - 1) + (N - 2) + …. + 1 = N*(N + 1)/2. Hence our time complexity will be O(N^2).

Space Complexity

O(N), Where ‘N’ is the size of the given array ‘ARR’.

 

The space complexity due to the ‘answer’ will be O(N).

Code Solution
(100% EXP penalty)
Majority Element lll
Full screen
Console