Check if Array is Good (LeetCode 2784): Solution and Explanation

Easy
Array
Hash Table
Sorting

Decide whether an array is a reordering of base[n] = [1, 2, ..., n-1, n, n], the array that holds 1 to n-1 once each and n twice.

Intuition

There is no search over n. base[n] has n + 1 elements, so if nums is good at all, it must match base[len(nums) - 1].

Think of base[n] as a row of seats: one seat for each value from 1 to n - 1 and two seats for n. That is n + 1 seats, the same as the number of elements.

Walk through nums and sit each value down. A value larger than n has no seat, and a value that finds its seats already full has nowhere to go. Either one proves the array is not good.

If every element finds a free seat, then n + 1 numbers have filled n + 1 seats, so every seat is full and the multiset matches base[n] exactly.

Approach: Counting with fixed limits

  1. Set n = len(nums) - 1 and make a count array of size n + 1.
  2. For each x in nums, return false if x > n.
  3. Increment count[x]. Return false if it is now above its limit: 2 when x == n, 1 otherwise.
  4. Return true after the loop.

Step-by-step example

nums = [2,1,3]
  1. There are 3 elements, so n = 2 and the target is base[2] = [1, 2, 2].
  2. x = 2 equals n, which has two seats. count[2] = 1, fine.
  3. x = 1 takes the single seat for 1. count[1] = 1, fine.
  4. x = 3 is larger than n = 2, so there is no seat for it. Return false.

Code: Python, Java, C++

1class Solution:
2    def isGood(self, nums: List[int]) -> bool:
3        n = len(nums) - 1
4        count = [0] * (n + 1)
5        for x in nums:
6            if x > n:
7                return False
8            count[x] += 1
9            if count[x] > (2 if x == n else 1):
10                return False
11        return True
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Counting with fixed limits O(n)
Counter equality O(n)
Sort and compare O(n log n)
Time

O(n)

Each element is checked and counted once.

Space

O(n)

The count array has n + 1 slots.

Other approaches

Sort and compare (time O(n log n), space O(1))

Sort nums and compare it element by element with [1, 2, ..., n, n]. Short to write, but it pays for a sort.

Counter equality (time O(n), space O(n))

Build a Counter of nums and compare it with a Counter of base[n]. Same linear time, but it builds a second table.

Common mistakes

  • Taking n as the maximum of nums instead of the length minus one. With [2,1,3] the maximum is 3, but base[3] needs four elements.
  • Indexing count[x] before checking x > n, which reads outside the array for values up to 200.
  • Giving every value a limit of one, which rejects the required second copy of n.
  • Forgetting that a single-element array can never be good: base[0] does not exist because values start at 1.

Frequently asked questions

Why does the length decide n?

base[n] always has n + 1 elements, and a permutation keeps the length, so n must be len(nums) - 1.

Why is it enough to check for too many copies?

There are exactly n + 1 seats and n + 1 elements. If no seat overflows and no value is out of range, the elements fill every seat, so nothing can be missing.

Can a one-element array be good?

No. It would have to match base[0], which would hold the value 0, and every element is at least 1.