All problems
0285MediumArrayBinary Search

Reading Present in the Rotated Log

Tracked in this browser only
Write code

Trains the technique from

LeetCode 81Search in Rotated Sorted Array II

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

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 is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

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