DEV Community

Discussion on: 6-10PM challenge problem #006

Collapse
 
vidit1999 profile image
Vidit Sarkar

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
Collapse
 
akbhairwal profile image
akbhairwal

if(num >= arr[low] and num < arr[mid]):

In equal case, you should return 'low' , there is no need to do anything.