-
Notifications
You must be signed in to change notification settings - Fork 13
/
SearchInRotatedArray.java
98 lines (88 loc) · 2.81 KB
/
SearchInRotatedArray.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
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
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
// https://www.youtube.com/watch?v=oTfPJKGEHcc (Best Explanation)
// https://leetcode.com/problems/search-in-rotated-sorted-array/
// https://practice.geeksforgeeks.org/problems/search-in-a-rotated-array/0
// https://www.youtube.com/watch?v=5BI0Rdm9Yhk
class SearchInRotatedArray {
// time complexity -> O(log(n)) : this solution find the target element without finding the pivot element.
// Intuition : If we divide the sorted rotated array into mid, we always get AT LEAST one strictly increasing
// sub array which is uniform. The pivot part won't in that part. We can use this uniform part and apply
// binary search on that half.
private static int findKInRotatedArray(int[] ip, int target) {
int left = 0, right = ip.length - 1;
while(left <= right) {
int mid = left + (right - left)/2;
if(ip[mid] == target) {
return mid;
}
else if(ip[left] <= ip[mid]) {
// left to middle is uniform
if(ip[left] <= target && target <= ip[mid]) {
right = mid - 1;
}
else {
left = mid + 1;
}
}
else {
// middle to right is uniform
if(ip[mid] <= target && target <= ip[right]) {
left = mid + 1;
}
else {
right = mid - 1;
}
}
}
return -1;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0) {
int n = Integer.parseInt(br.readLine().trim());
String[] str = br.readLine().trim().split("\\s+");
int[] ip = new int[n];
for(int i = 0; i < n; ++i) {
ip[i] = Integer.parseInt(str[i]);
}
int k = Integer.parseInt(br.readLine().trim());
System.out.println(findKInRotatedArray(ip, k));
}
}
// O(2log(n)) time complexity
/*public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0) {
int n = Integer.parseInt(br.readLine().trim()),max = Integer.MIN_VALUE, rPoint = 0,pos=-1;
String s[] = br.readLine().trim().split("\\s+");
int ip[] = new int[n];
int k = Integer.parseInt(br.readLine().trim());
for (int i = 0; i < ip.length; i++) {
ip[i] = Integer.parseInt(s[i]);
if(ip[i]>max) {
max = ip[i];
rPoint = i;
}
}
//System.out.println(max+" "+rPoint);
pos = bSearch(ip,k,0,rPoint);
if(pos==-1) {
pos = bSearch(ip,k,rPoint+1,ip.length-1);
}
System.out.println(pos);
}
}
static int bSearch(int ip[],int k,int l,int r) {
if(l<=r) {
int mid = l + (r-l)/2;
if(ip[mid]==k) return mid;
else if(ip[mid] > k) return bSearch(ip,k,l,mid-1);
else if(ip[mid] < k) return bSearch(ip,k,mid+1,r);
}
return -1;
}*/
}