forked from super30admin/Binary-Search-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearchRotatedArray.cs
41 lines (32 loc) · 1.15 KB
/
SearchRotatedArray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Time Complexity : O(logn)
// Space Complexity : O(1)
// Did this code successfully run on Leetcode : Yes
// Any problem you faced while coding this : No
// Your code here along with comments explaining your approach
public int SearchRotatedArray(int[] nums, int target) {
if(nums == null || nums.Length == 0)
return -1;
int low = 0;
int high = nums.Length - 1;
while(low <= high)
{
int mid = low + (high - low)/2;
if(nums[mid] == target)
return mid;
if(nums[low] <= nums[mid]) //left portion of array is sorted
{
if(nums[low] <= target && target < nums[mid]) // target lies in low and mid (left side)
high = mid - 1;
else // target lies in mid and high (right side)
low = mid + 1;
}
else //right portion of array is sorted
{
if(nums[mid] < target && target <= nums[high]) //target lies in mid and high
low = mid + 1;
else //target lies in low and mid
high = mid - 1;
}
}
return -1;
}