Skip to content

Commit 6ef605c

Browse files
authored
Create 0033_Search_in_rotatedarr.java
1 parent 1ab2686 commit 6ef605c

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

0033_Search_in_rotatedarr.java

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

0 commit comments

Comments
 (0)