fork(18) download
  1. import sys
  2.  
  3. def get_majority_element(a, left, right):
  4. if left == right:
  5. return -1
  6. if left + 1 == right:
  7. return a[left]
  8.  
  9. left_m = get_majority_element(a, left, (left + right - 1)//2 + 1)
  10. right_m = get_majority_element(a, (left + right - 1)//2 + 1, right)
  11. left_count = 0
  12. for i in range(left, right):
  13. if a[i] == left_m:
  14. left_count += 1
  15. if left_count > (right-left)//2:
  16. return left_m
  17.  
  18. right_count = 0
  19. for i in range(left, right):
  20. if a[i] == right_m:
  21. right_count += 1
  22. if right_count > (right-left)//2:
  23. return right_m
  24.  
  25. return -1
  26.  
  27. if __name__ == '__main__':
  28. input = sys.stdin.read()
  29. n, *a = list(map(int, input.split()))
  30. if get_majority_element(a, 0, n) != -1:
  31. print(1)
  32. else:
  33. print(0)
Success #stdin #stdout 0.02s 9984KB
stdin
10
2 124554847 2 941795895 2 2 2 2 792755190 756617003
stdout
1