-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmajority_element.py
48 lines (46 loc) · 1.04 KB
/
majority_element.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
# Time Complexity : O(nlogn)
# Auxilary Space : O(1)/Constant
"""
def get_majority_element(a, left, right):
if left == right:
return -1
if left + 1 == right:
return a[left]
a.sort()
count = j = 0
for i in range(1, len(a)):
if a[i] != a[i-1]:
if count < i-j:
count = i-j
j = i
if count > len(a)/2:
return count
return -1
"""
# Description : SPACE TIME TRADEOFF using dictionary
# Time Complexity : O(n)
# Auxilary Space : O(n)
"""
def get_majority(a, left, right):
if left == right:
return -1
if left + 1 == right:
return a[left]
lookup = {}
for i in a:
if i not in lookup:
lookup[i] = 1
else:
lookup[i] += 1
maximum = max(lookup.values())
if maximum > len(a)/2:
return maximum
return -1
if __name__ == '__main__':
n = int(input())
a = [int(x) for x in input().split()][:n]
if get_majority(a, 0, n, 0) != -1:
print(1)
else:
print(0)