From e9f7dd9d71b60dd0c7958a142a609d595e362cf1 Mon Sep 17 00:00:00 2001 From: Taha Zulfiquar Date: Sat, 4 Oct 2025 15:19:30 +0530 Subject: [PATCH 1/2] Add binary search to find peak index in a mountain array --- data_structures/arrays/peak_mountain_array.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 data_structures/arrays/peak_mountain_array.py diff --git a/data_structures/arrays/peak_mountain_array.py b/data_structures/arrays/peak_mountain_array.py new file mode 100644 index 000000000000..1878e941ce15 --- /dev/null +++ b/data_structures/arrays/peak_mountain_array.py @@ -0,0 +1,23 @@ +""" +peak_mountain_array.py +Author: Taha Zulfiquar +Description: Finds the peak index in a mountain array using binary search. +""" + +from typing import List + +def peak_mountain(arr: List[int]) -> int: + start, end = 0, len(arr) - 1 + + while start < end: + mid = start + (end - start) // 2 + if arr[mid] > arr[mid + 1]: + end = mid + else: + start = mid + 1 + return start + +if __name__ == "__main__": + arr = [1, 2, 3, 5, 7, 8, 6, 3, 2] + peak_index = peak_mountain(arr) + print(f"The peak index is: {peak_index}") From 5808e7c060b50d836daa66dbe9a28178c9cdb9f7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 4 Oct 2025 09:59:57 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/peak_mountain_array.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/arrays/peak_mountain_array.py b/data_structures/arrays/peak_mountain_array.py index 1878e941ce15..aef067dc043d 100644 --- a/data_structures/arrays/peak_mountain_array.py +++ b/data_structures/arrays/peak_mountain_array.py @@ -6,6 +6,7 @@ from typing import List + def peak_mountain(arr: List[int]) -> int: start, end = 0, len(arr) - 1 @@ -17,6 +18,7 @@ def peak_mountain(arr: List[int]) -> int: start = mid + 1 return start + if __name__ == "__main__": arr = [1, 2, 3, 5, 7, 8, 6, 3, 2] peak_index = peak_mountain(arr)