-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathSearchRotatedSortedArray.java
More file actions
38 lines (36 loc) · 1.41 KB
/
Copy pathSearchRotatedSortedArray.java
File metadata and controls
38 lines (36 loc) · 1.41 KB
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
class SearchRotatedSortedArray {
/*
Time complexity - O(log n)
Space complexity = O(1)
to search in a rotated sorted array using binary search, we need to determine in which
part of the array the target lies in. If mid element >= low element, it means the array
is in sorted order. Check if mid element > target and low element <= target, if so we
search in the left side lese we search in the right side of the array. When searching in
right side if mid element < target and high ele is >= target then search in the right side of mid
else search on the left side. If mid element equals target we return index. Else we return -1.
*/
public int search(int[] nums, int target) {
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[mid] >= nums[low]) {
if (nums[mid] > target && nums[low] <= target) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if (nums[mid] < target && nums[high] >= target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
return -1;
}
}