Update appNew update is available. Click here to update.

Majority Element lll

profile
Contributed by
Shrey Pansuria
Medium
yellow-spark
0/80
10 mins
90 %
36 upvotes
Paytm (One97 Communications Limited)SamsungCapgemini
+5 more

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 )
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’.
Full screen
Reset Code
Full screen
Autocomplete
copy-code
Console