#1752

Check if Array Is Sorted and Rotated

Easy Count drops

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether the array could be a non-decreasing array that was rotated some number of positions (duplicates allowed).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: could this be a sorted array that was rotated?

Why this works: walk around the array in a circle and count the "drops" (a value bigger than the next). A rotated sorted array has at most one drop.

Example: [3, 4, 5, 1, 2]

3→4 ok, 4→5 ok, 5→1 drop, 1→2 ok, 2→3 (wrap) ok1 drop → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Forgetting the wrap-around comparison between the last and first element.