From 905626dbd57ac112a3d558433a4dfb7af2c850b3 Mon Sep 17 00:00:00 2001 From: KushDutta23 Date: Fri, 9 Jan 2026 19:54:03 +0530 Subject: [PATCH 1/2] Add iterative binary search implementation Implement iterative binary search algorithm. --- maths/binary_search_iterative.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 maths/binary_search_iterative.py diff --git a/maths/binary_search_iterative.py b/maths/binary_search_iterative.py new file mode 100644 index 000000000000..8a633950fa97 --- /dev/null +++ b/maths/binary_search_iterative.py @@ -0,0 +1,25 @@ +""" +Binary Search (Iterative) +Time Complexity: O(log n) +Space Complexity: O(1) +""" + +def binary_search(arr, target): + left, right = 0, len(arr) - 1 + + while left <= right: + mid = (left + right) // 2 + if arr[mid] == target: + return mid + elif arr[mid] < target: + left = mid + 1 + else: + right = mid - 1 + + return -1 + + +if __name__ == "__main__": + arr = [1, 3, 5, 7, 9, 11] + target = 7 + print(binary_search(arr, target)) From 29a2a0dbd57ae30dd29d8e79398d0968c92d3764 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 9 Jan 2026 14:26:05 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/binary_search_iterative.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/binary_search_iterative.py b/maths/binary_search_iterative.py index 8a633950fa97..b4b82776b3bb 100644 --- a/maths/binary_search_iterative.py +++ b/maths/binary_search_iterative.py @@ -4,6 +4,7 @@ Space Complexity: O(1) """ + def binary_search(arr, target): left, right = 0, len(arr) - 1