-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsearchRSA.java
More file actions
executable file
·65 lines (60 loc) · 1.44 KB
/
searchRSA.java
File metadata and controls
executable file
·65 lines (60 loc) · 1.44 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
/*
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
*/
import java.util.*;
public class searchRSA{
public static void main(String[] args) {
int[] A = {4,5,6,7,0,1,2};
System.out.println(search(A,0));
int[] B={1,1,1,2,3,4,1,1,1,1,1,1,1};
System.out.println(searchDuplicates(B,2));
}
public static int search(int[] A, int target){
int L = 0;
int R = A.length-1;
while(L <= R){
int M = (R - L)/2 + L;
if(A[M] == target) return M;
if(A[L] <= A[M]){
if(A[L] <= target && target < A[M])
R = M - 1;
else
L = M + 1;
}else{
if(A[R] >= target && target > A[M])
L = M + 1;
else
R = M - 1;
}
}
return -1;
}
public static boolean searchDuplicates(int[] A, int target){
int L = 0;
int R = A.length-1;
while(L <= R){
int M = (R - L)/2 + L;
if(A[M] == target) return true;
if(A[L] < A[M]){
if(A[L] <= target && target < A[M])
R = M - 1;
else
L = M + 1;
}else if(A[R] > A[M]){
if(A[R] >= target && target > A[M])
L = M + 1;
else
R = M - 1;
}else{
while(L <= M && A[L] == A[M])
L ++;
while(M <= R && A[R] == A[M])
R --;
}
}
return false;
}
}