Given a sorted and rotated array numbers
containing unique elements and an integer target
, return the index of target
if it is in numbers
, or -1
if it is not.
Rotating an array [a[0], a[1], a[2], ..., a[n-1]]
once results in [a[n-1], a[0], a[1], a[2], ..., a[n-2]]
. Rotating it a second time results in [a[n-2], a[n-1], a[0], a[1], ..., a[n-3]]
.
Develop an algorithm that runs in O(log n) time complexity.
numbers: number[]
: An array of integerstarget: number
: An integerInput: numbers = [0,1,2,3,4], target = 2Output: 2Explanation: The original array [0,1,2,3,4] was rotated 0 times and 2 is present at 2nd index in given array
Input: numbers = [2,3,4,0,1], target = 0Output: 3Explanation: The original array [0,1,2,3,4] was rotated 3 times and became [2,3,4,0,1]. 0 is present at 3rd index in given array
Input: numbers = [4], target = 2Output: -1Explanation: The original array [4] was rotated 0 times and became [4]. 2 is not present in the given array
numbers.length
<= 1000numbers[i]
<= 10,000target
<= 10,000Given a sorted and rotated array numbers
containing unique elements and an integer target
, return the index of target
if it is in numbers
, or -1
if it is not.
Rotating an array [a[0], a[1], a[2], ..., a[n-1]]
once results in [a[n-1], a[0], a[1], a[2], ..., a[n-2]]
. Rotating it a second time results in [a[n-2], a[n-1], a[0], a[1], ..., a[n-3]]
.
Develop an algorithm that runs in O(log n) time complexity.
numbers: number[]
: An array of integerstarget: number
: An integerInput: numbers = [0,1,2,3,4], target = 2Output: 2Explanation: The original array [0,1,2,3,4] was rotated 0 times and 2 is present at 2nd index in given array
Input: numbers = [2,3,4,0,1], target = 0Output: 3Explanation: The original array [0,1,2,3,4] was rotated 3 times and became [2,3,4,0,1]. 0 is present at 3rd index in given array
Input: numbers = [4], target = 2Output: -1Explanation: The original array [4] was rotated 0 times and became [4]. 2 is not present in the given array
numbers.length
<= 1000numbers[i]
<= 10,000target
<= 10,000console.log()
statements will appear here.