forked from super30admin/Binary-Search-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2.java
More file actions
61 lines (50 loc) · 1.76 KB
/
Problem2.java
File metadata and controls
61 lines (50 loc) · 1.76 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 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 in three sentences only
/*
* First check which part is sorted, is it left-sorted or right-sorted.
* Based on the result above, check if the target fits in the range i.e.
* If left sorted, should fit between low and mid. If right sorted, should fit mid and high. Compute further accordingly
*/
public class Problem2 {
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;
} else if(nums[low] <= nums[mid]) {
if(nums[low] <= target && nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
} else {
if(target > nums[mid] && target <= nums[high]) {
low = mid + 1;
} else {
high = mid - 1;
}
}
}
return -1;
}
public static void main(String[] args) {
Problem2 obj = new Problem2();
int[] nums = {4,5,6,7,0,1,2};
int target = 0;
System.out.println(obj.search(nums, target));
nums = new int[]{6,7,0,1,2,4,5};
target = 7;
System.out.println(obj.search(nums, target));
nums = new int[]{1,2,5,3};
target = 7;
System.out.println(obj.search(nums, target));
nums = new int[]{3,1};
target = 1;
System.out.println(obj.search(nums, target));
}
}