Here is a Python solution,
def shiftArr(arr , num): low, high = 0, len(arr)-1 while(low <= high): mid = (low + high)//2 if(arr[mid] == num): return mid if(arr[low] < arr[mid]): if(num >= arr[low] and num < arr[mid]): high = mid - 1 else: low = mid + 1 else: if(num > arr[mid] and num <= arr[high]): low = mid + 1 else: high = mid - 1 return -1
Test cases,
print(shiftArr([9, 12, 17, 2, 4, 5], 2)) # output -> 3 print(shiftArr([9, 12, 17, 2, 4, 5], 3)) # output -> -1 print(shiftArr([9, 12, 17, 2, 4, 5], 17)) # output -> 2 print(shiftArr([1],1)) # output -> 0 print(shiftArr([1],2)) # output -> -1 print(shiftArr([],3)) # output -> -1
if(num >= arr[low] and num < arr[mid]):
In equal case, you should return 'low' , there is no need to do anything.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Here is a Python solution,
Test cases,
if(num >= arr[low] and num < arr[mid]):
In equal case, you should return 'low' , there is no need to do anything.