-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchInRotatedArrayWithDuplicate.java
45 lines (42 loc) · 1.52 KB
/
SearchInRotatedArrayWithDuplicate.java
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
class SearchInRotatedArrayWithDuplicate {
public static void main(String[] args) {
int[] nums = {1,0,1,1,1};
boolean ans = search(nums, 0);
System.out.println("The first occurence are: "
+ ans);
}
public static boolean search(int[] nums, int target) {
int low = 0, high = nums.length-1, mid;
boolean answer = false;
while (low <= high) {
mid = (low + high) / 2;
if (target == nums[mid]) {
answer = true;
return answer;
}
if (nums[low] == nums[mid] && nums[mid] == nums[high]) {
low = low + 1;
high = high - 1;
continue;
}
if (nums[low] <= nums[mid]) {//sorted Array
if (target > nums[mid]) {// element does not exist
low = mid + 1;
} else if (target < nums[mid] && target < nums[low]) {// element does not exist
low = mid + 1;
} else if (target < nums[mid] && target >= nums[low]) {
high = mid - 1;
}
}else if(nums[low] > nums[mid]){
if(target<nums[mid] && target <= nums[low]){
high=mid-1;
}else if(target>nums[mid] && target <nums[low]){
low=mid+1;
}else if(target>nums[mid] && target >= nums[low]){
high=mid-1;
}
}
}
return answer;
}
}