From 8b716f9f71fadbef5060db8f0eadd734781f2ec6 Mon Sep 17 00:00:00 2001 From: Harsha Ramesh Naik Date: Fri, 29 Aug 2025 14:51:40 -0700 Subject: [PATCH] Create rotated_sorted_arr_search --- rotated_sorted_arr_search | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 rotated_sorted_arr_search diff --git a/rotated_sorted_arr_search b/rotated_sorted_arr_search new file mode 100644 index 00000000..fe98082f --- /dev/null +++ b/rotated_sorted_arr_search @@ -0,0 +1,21 @@ +class Solution: + def search(self, nums, target): + low, high = 0, len(nums) - 1 + while low <= high: + mid = low + (high - low) // 2 + if nums[mid] == target: + return mid + + # left half is sorted + if nums[low] <= nums[mid]: + if nums[low] <= target and target < nums[mid]: + high = mid - 1 + else: + low = mid + 1 + # right half is sorted + else: + if nums[mid] < target and target <= nums[high]: + low = mid + 1 + else: + high = mid - 1 + return -1