-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchInRotatedArray.java
More file actions
117 lines (115 loc) · 3.09 KB
/
searchInRotatedArray.java
File metadata and controls
117 lines (115 loc) · 3.09 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
public class searchInRotatedArray {
// https://leetcode.com/problems/search-in-rotated-sorted-array/
public static void main(String[] args) {
int[] num = {4, 5, 6, 7, 0, 1, 2};
int target = 0;
System.out.println(search(num, target));
}
public static int search(int[] nums, int target) {
int peak = pivot(nums);
if(peak==-1)
{
//it means peak does not exist
return binarySearch(nums, target, 0, nums.length - 1);
}
int first = binarySearch(nums, target, 0, peak);
if(first != -1)
{
return first;
}
else
{
return binarySearch(nums, target, peak+1, nums.length-1);
}
}
static int binarySearch(int[] nums, int target, int start, int end)
{
while(start <= end)
{
int mid = start + (end - start)/2;
if(nums[mid] > target)
{
end = mid-1;
}
else if(nums[mid] < target)
{
start = mid + 1;
}
else
{
return mid;
}
}
return -1;
}
//this will not work with duplicate values in the array
static int pivot(int[] nums)
{
int start = 0;
int end = nums.length - 1;
while(start<=end)
{
int mid = start + (end - start) / 2;
if(mid < end && nums[mid] > nums[mid+1])
{
return mid;
}
if(start < mid && nums[mid-1] > nums[mid])
{
return mid-1;
}
if(nums[mid]<=nums[start])
{
end = mid-1;
}
else
{
start = mid+1;
}
}
return -1;
}
static int pivotWithDuplicates(int[] nums)
{
int start = 0;
int end = nums.length - 1;
while(start<=end)
{
int mid = start + (end - start) / 2;
if(mid < end && nums[mid] > nums[mid+1])
{
return mid;
}
if(start < mid && nums[mid-1] > nums[mid])
{
return mid-1;
}
if(nums[mid] == nums[start] && nums[mid] == nums[end])
{
//skip these elements
//check if start is the pivot
if(nums[start] > nums[start+1])
{
return start;
}
start++;
//check if end is the pivot
if(nums[end] < nums[end-1])
{
return end-1;
}
end--;
}
//if not found till now, the pivot is in right side and left side is sorted
else if(nums[start] < nums[mid] || (nums[start] == nums[mid] && nums[mid] > nums[end]))
{
start = mid + 1;
}
else
{
end = mid - 1;
}
}
return -1;
}
}