Skip to main content
All questions
MediumArrays & Hashing · Cyclic Sort

Verify Permutation In-Place

Written by a community member · · 1 attempt · 0% pass

0

You are given an integer list A of length n. Determine whether A contains every value in [0, 1, ..., n-1] exactly once. You must do this in O(n) time and O(1) extra space. You may modify the list in place. Do not use sorting, sets, dictionaries, or auxiliary boolean arrays. Return True if A is a permutation of [0, 1, ..., n-1], otherwise return False. The empty list counts as a valid permutation of the empty range.

Elements may be negative or larger than n-1. 0 <= len(A) <= 200000.

Examples:

  • is_permutation([2, 0, 1, 3]) -> True
  • is_permutation([1, 1, 0]) -> False
  • is_permutation([]) -> True