Skip to content

Commit

Permalink
Merge pull request #14 from shwetacharde/patch-9
Browse files Browse the repository at this point in the history
Create Search-in-rotated-sorted-array.java
  • Loading branch information
kamleshcharde committed Oct 10, 2022
2 parents 77ebef5 + 6a2f5c6 commit bed884f
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Search-in-rotated-sorted-array.java
@@ -0,0 +1,30 @@
class Solution {
public int search(int[] arr, int target) {
int start=0;
int end=arr.length-1;

while(start<=end){
int mid=(start+end)>>1;
if(arr[mid]==target){
return mid;
}
if(arr[start]<=arr[mid]){
if(arr[start]<=target && arr[mid]>=target){
end=mid-1;
}
else{
start=mid+1;
}
}
else{
if(arr[mid]<=target && target<=arr[end]){
start=mid+1;
}else{
end=mid-1;
}
}
}

return -1;
}
}

0 comments on commit bed884f

Please sign in to comment.