Menu Close

Find all elements that appear more than n/3 times.

Given an integer array of size n, find all elements that appear more than [n/3] times. If no such element exists, return -1.

Array_nums = list(map(int,input('Array : ').split(',')))
N=len(Array_nums)

L=[]
for i in range(N):
	Occ=Array_nums.count(Array_nums[i])
	if Occ > N/3 :
		L.append(Array_nums[i])

print(-1) if not L else print(list(set(L)))	

Input_1:
Array : 20,30,10,10,5,4,20,1,2

Output:
-1


Input_2:
Array : 3,2,3

Output:
[3]


Input_3:
Array : 1

Output:
[1]


Input_4:
Array : 1,2

Output:
[1, 2]


Input_5:
Array : 10,10,20,30,10,10

Output:
[10]



Illustration of the executed Output

Executed using python3 in terminal(Ubuntu) Linux

More Q