From 017acc810d54940ee0a0e59acfbb51074494932f Mon Sep 17 00:00:00 2001 From: Avinash0935 Date: Sat, 11 Oct 2025 12:16:06 +0530 Subject: [PATCH 1/2] Add Python solutions for Search Insert Position and Search Range --- .../search_insert_position.py | 27 +++++++++++++ .../Binary search algo/search_range.py | 39 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 data_structures/Binary search algo/search_insert_position.py create mode 100644 data_structures/Binary search algo/search_range.py diff --git a/data_structures/Binary search algo/search_insert_position.py b/data_structures/Binary search algo/search_insert_position.py new file mode 100644 index 000000000000..d9ccc71ca88d --- /dev/null +++ b/data_structures/Binary search algo/search_insert_position.py @@ -0,0 +1,27 @@ +from typing import List + +def search_insert(nums: List[int], target: int) -> int: + """ + Finds the index to insert a target value into a sorted list. + + If the target is found, its index is returned. Otherwise, returns the + index where it would be if it were inserted in order. + + Args: + nums: A sorted list of integers. + target: The integer to search for. + + Returns: + The index for the target value. + """ + left, right = 0, len(nums) - 1 + + while left <= right: + mid = (left + right) // 2 + if nums[mid] == target: + return mid + elif nums[mid] < target: + left = mid + 1 + else: + right = mid - 1 + return left \ No newline at end of file diff --git a/data_structures/Binary search algo/search_range.py b/data_structures/Binary search algo/search_range.py new file mode 100644 index 000000000000..cd48fa3cdf20 --- /dev/null +++ b/data_structures/Binary search algo/search_range.py @@ -0,0 +1,39 @@ +from typing import List + +def search_range(nums: List[int], target: int) -> List[int]: + """ + Finds the first and last position of a given target value in a sorted array. + + Args: + nums: A sorted list of integers. + target: The integer to search for. + + Returns: + A list containing the starting and ending index of the target. + Returns [-1, -1] if the target is not found. + """ + + def find_bound(is_first: bool) -> int: + """Helper to find the leftmost or rightmost occurrence of the target.""" + left, right = 0, len(nums) - 1 + bound = -1 + while left <= right: + mid = (left + right) // 2 + if nums[mid] == target: + bound = mid + if is_first: + right = mid - 1 # Continue searching on the left + else: + left = mid + 1 # Continue searching on the right + elif nums[mid] < target: + left = mid + 1 + else: + right = mid - 1 + return bound + + first_bound = find_bound(True) + if first_bound == -1: + return [-1, -1] + + last_bound = find_bound(False) + return [first_bound, last_bound] \ No newline at end of file From a6fa7c67e098ac3de6abaf3c10992b6b18450e0e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 11 Oct 2025 07:02:53 +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/Binary search algo/search_insert_position.py | 3 ++- data_structures/Binary search algo/search_range.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/data_structures/Binary search algo/search_insert_position.py b/data_structures/Binary search algo/search_insert_position.py index d9ccc71ca88d..7ada2f4e33bb 100644 --- a/data_structures/Binary search algo/search_insert_position.py +++ b/data_structures/Binary search algo/search_insert_position.py @@ -1,5 +1,6 @@ from typing import List + def search_insert(nums: List[int], target: int) -> int: """ Finds the index to insert a target value into a sorted list. @@ -24,4 +25,4 @@ def search_insert(nums: List[int], target: int) -> int: left = mid + 1 else: right = mid - 1 - return left \ No newline at end of file + return left diff --git a/data_structures/Binary search algo/search_range.py b/data_structures/Binary search algo/search_range.py index cd48fa3cdf20..8234dc66a830 100644 --- a/data_structures/Binary search algo/search_range.py +++ b/data_structures/Binary search algo/search_range.py @@ -1,5 +1,6 @@ from typing import List + def search_range(nums: List[int], target: int) -> List[int]: """ Finds the first and last position of a given target value in a sorted array. @@ -36,4 +37,4 @@ def find_bound(is_first: bool) -> int: return [-1, -1] last_bound = find_bound(False) - return [first_bound, last_bound] \ No newline at end of file + return [first_bound, last_bound]