-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchElementInRotatedSortedArray.java
More file actions
42 lines (36 loc) · 1.22 KB
/
SearchElementInRotatedSortedArray.java
File metadata and controls
42 lines (36 loc) · 1.22 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
39
40
41
42
class Solution {
public int search(int[] nums, int target) {
if (nums.length == 0) return -1;
if (nums.length == 1) return (nums[0] == target) ? 0 : -1;
int start = 0;
int end = nums.length - 1;
while (start <= end) {
int mid = start + (end - start) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[mid] < nums[end]) {
if (nums[mid] < target && target <= nums[end]) {
start = mid + 1;
} else {
end = mid - 1;
}
} else {
if (nums[start] <= target && target < nums[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
return -1;
}
}
/*
Approach: We use binary search to find element by first checking which part of array is sorted
and then by checking if target could be found in that part by logic
nums[start] <= target <= nums[end]
if this codition is not satisfied then it means that target is for sure in another unsorted half of array
Time complexity: O(n)
Space complexity: O(1)
*/