Shifted Array Search
Solution for the Problem#006 is provided in Java language.
Test cases :
Test case #1
input: [2], 2
Test case #2
input:[1,2], 2
Test case #3
input: [0,1,2,3,4,5], 1
Test case #4
input : [1,2,3,4,5,0], 0
Test case #5
input : [9,12,17,2,4,5], 17
Test case #6
input : [9,12,17,2,4,5,6], 4
Solution
static int shiftedArrSearch(int[] shiftArr, int num) {
if(shiftArr.length>0){
return find(shiftArr,0,shiftArr.length-1,num);
}else return -1;
}
static int find(int[] arr, int l, int h, int num){
int median = (l+h)/2;
if(arr[median]==num) return median;
if(l==h) return -1;
return Integer.max(find(arr,l,median,num), find(arr,median+1,h,num));
}
Top comments (0)