From f05d888a0b621ca4e99e2b0fb6e23c097006fe41 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Tue, 28 May 2024 10:29:13 +0200 Subject: Init --- progs/a586.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 progs/a586.py (limited to 'progs/a586.py') diff --git a/progs/a586.py b/progs/a586.py new file mode 100644 index 0000000..d22252b --- /dev/null +++ b/progs/a586.py @@ -0,0 +1,18 @@ +def is_majority(arr, n, x): + i = binary_search(arr, 0, n-1, x) + if i == -1: + return False + if ((i + n//2) <= (n -1)) and arr[i + n//2] == x: + return True + else: + return False +def binary_search(arr, low, high, x): + if high >= low: + mid = (low + high)//2 + if (mid == 0 or x > arr[mid-1]) and (arr[mid] == x): + return mid + elif x > arr[mid]: + return binary_search(arr, (mid + 1), high, x) + else: + return binary_search(arr, low, (mid -1), x) + return -1 \ No newline at end of file -- cgit v1.2.3-18-g5258