Search in Rotated Sorted Array II

An array sorted in non-decreasing order, possibly with repeated values, has been rotated at some unknown pivot k, so it reads [nums[k], ..., nums[n-1], nums[0], ..., nums[k-1]]. Given the rotated array nums and an integer target, return true if target appears in nums and false otherwise. Aim to do as few operations as possible.

30:00

Search in Rotated Sorted Array II
medium
Free written solution
Topics
Companies

An array sorted in non-decreasing order, possibly with repeated values, has been rotated at some unknown pivot k, so it reads [nums[k], ..., nums[n-1], nums[0], ..., nums[k-1]]. Given the rotated array nums and an integer target, return true if target appears in nums and false otherwise. Aim to do as few operations as possible.

Example 1:
Input: {"nums":[2,5,6,0,0,1,2],"target":0}
Output: true
Constraints:
  • 1≤nums.length≤50001 \leq \text{nums.length} \leq 5000

  • −104≤nums[i]≤104-10^4 \leq \text{nums}[i] \leq 10^4

  • nums is guaranteed to be rotated at some pivot.

  • −104≤target≤104-10^4 \leq \text{target} \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"nums":[2,5,6,0,0,1,2],"target":0}

Search for 0 in a rotated array of 7 values. lo and hi start at the two ends.

Two sorted runs, one cliff

--- target 0

sorted half

undecidable

20516203041526lohi
lo = 0 (2)
mid = -
hi = 6 (2)
round 0
Variables
VariableValue
lo0
hi6
target0
DepthFunction Call
Stack empty
0/3