-
Notifications
You must be signed in to change notification settings - Fork 1
/
majority_element.py
54 lines (39 loc) · 1.08 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
49
50
51
52
53
54
# Majority Element
# Author: jerrybelmonte
import sys
def get_majority_element(seq, left, right):
"""
Gets the element in the sequence that makes up the majority if it exists.
:param seq: sequence of elements
:param left: starting index in the sequence
:param right: end index in the sequence
:return: the majority element count, otherwise -1 if DNE
>>> get_majority_element([2, 3, 9, 2, 2], 0, 5)
3
>>> get_majority_element([1, 2, 3, 4], 0, 4)
-1
>>> get_majority_element([1, 2, 3, 1], 0, 4)
-1
"""
if left == right:
return -1
if left + 1 == right:
return seq[left]
seq.sort()
cur = left
ndx = left
while ndx < right:
count = 0
while ndx < right and seq[ndx] == seq[cur]:
ndx += 1
count += 1
if count > left + ((right - left)//2):
return ndx
cur = ndx
return -1
if __name__ == '__main__':
n, *a = list(map(int, sys.stdin.read().split()))
if get_majority_element(a, 0, n) != -1:
print(1)
else:
print(0)