-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajority_element.py
More file actions
42 lines (34 loc) · 1.05 KB
/
majority_element.py
File metadata and controls
42 lines (34 loc) · 1.05 KB
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
# Uses python3
import sys
def get_majority_element(a, left, right):
if left == right:
return -1
if left + 1 == right:
return a[left]
#write your code here
mid = left + int((right - left) // 2)
left_winner = get_majority_element(a, left, mid)
right_winner = get_majority_element(a, mid, right)
#print(a[left:right], mid, left_winner, right_winner)
if not (left_winner == -1):
count = 0
for item in a[left:right]:
if item == left_winner:
count = count + 1
if count > (right - left) // 2:
return left_winner
if not (right_winner == -1):
count = 0
for item in a[left:right]:
if item == right_winner:
count = count + 1
if count > (right - left) // 2:
return right_winner
return -1
if __name__ == '__main__':
input = sys.stdin.read()
n, *a = list(map(int, input.split()))
if get_majority_element(a, 0, n) != -1:
print(1)
else:
print(0)