Skip to content

Commit 9548e34

Browse files
authored
Create 0033_Search_in_rotatedarr.java
1 parent ea13071 commit 9548e34

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

0033_Search_in_rotatedarr.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public static int search(int[] ar, int target) {
3+
int a=BinarySearch(ar,target,0,Pivot(ar));
4+
int b=BinarySearch(ar,target,Pivot(ar)+1,ar.length-1);
5+
if(a==-1&&b==-1)
6+
return -1;
7+
if(a==-1&&b!=-1)
8+
return b;
9+
else
10+
return a;
11+
}
12+
static int Pivot(int[] ar) {
13+
int i;
14+
for (i = 0; i < ar.length - 1; i++) {
15+
if (ar[i] > ar[i + 1])
16+
break;
17+
}
18+
return i;
19+
}
20+
static int BinarySearch(int[] ar,int target,int low,int high)
21+
{
22+
if(high>=low) {
23+
int mid = low + (high - low) / 2;
24+
if (ar[mid] == target)
25+
return mid;
26+
if (ar[mid] > target)
27+
return BinarySearch(ar, target,low, mid - 1);
28+
else
29+
return BinarySearch(ar, target, mid + 1, high);
30+
}
31+
return -1;
32+
33+
}
34+
}

0 commit comments

Comments
 (0)