From e9f7dd9d71b60dd0c7958a142a609d595e362cf1 Mon Sep 17 00:00:00 2001 From: Taha Zulfiquar Date: Sat, 4 Oct 2025 15:19:30 +0530 Subject: [PATCH 1/4] 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/4] [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) From cbcf2607e5f36f251f9a1c97e82e78f131f6aec1 Mon Sep 17 00:00:00 2001 From: Taha Zulfiquar Date: Sat, 4 Oct 2025 15:35:09 +0530 Subject: [PATCH 3/4] Add binary search to find peak index in a mountain array --- data_structures/arrays/peak_mountain_array.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/data_structures/arrays/peak_mountain_array.py b/data_structures/arrays/peak_mountain_array.py index aef067dc043d..c403e8d36b04 100644 --- a/data_structures/arrays/peak_mountain_array.py +++ b/data_structures/arrays/peak_mountain_array.py @@ -4,10 +4,7 @@ Description: Finds the peak index in a mountain array using binary search. """ -from typing import List - - -def peak_mountain(arr: List[int]) -> int: +def peak_mountain(arr): start, end = 0, len(arr) - 1 while start < end: @@ -22,4 +19,4 @@ def peak_mountain(arr: List[int]) -> int: 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}") + print("The peak index is:", peak_index) From 6d6632e750f1c76f990deb16daec16255028c5a5 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 10:11:51 +0000 Subject: [PATCH 4/4] [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 | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/peak_mountain_array.py b/data_structures/arrays/peak_mountain_array.py index c403e8d36b04..ca9d998be79a 100644 --- a/data_structures/arrays/peak_mountain_array.py +++ b/data_structures/arrays/peak_mountain_array.py @@ -4,6 +4,7 @@ Description: Finds the peak index in a mountain array using binary search. """ + def peak_mountain(arr): start, end = 0, len(arr) - 1