Show problems as
Classic problem #81MediumArrayBinary Search

Search in Rotated Sorted Array II

Real-world version: Reading Present in the Rotated Log
Tracked in this browser only
Read Search in Rotated Sorted Array II on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A weather station files temperatures into a ring of fixed size. It files them in non-decreasing order and carries on around the ring, so a reading may equal the one filed before it.

A download starts at whichever slot the reader happens to open at and then walks the ring all the way round. What comes out is log: the non-decreasing run of temperatures shifted round by some number of slots. The shift may be zero, in which case log is simply the run itself.

Return true when the temperature wanted sits in some slot of log, and false otherwise.

Examples

Example 1

Input
log = [8, 9, 12, -3, -1, 3], wanted = -1
Output
true

The download opened three slots along, so the run 8, 9, 12 comes out first and -3, -1, 3 follows. The temperature -1 sits at index 4.

Example 2

Input
log = [4, -1, 4, 4, 4], wanted = -1
Output
true

The temperature -1 sits at index 1 of the download.

Example 3

Input
log = [8, 9, 12, -3, -1, 3], wanted = 7
Output
false

No slot of the download holds 7.

Constraints

  • 1 <= log.length <= 5000
  • -10^4 <= log[i] <= 10^4
  • log is guaranteed to be shifted round at some slot.
  • -10^4 <= wanted <= 10^4

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def reading_present(log: list[int], wanted: int) -> bool:
Java
public boolean readingPresent(int[] log, int wanted)