Skip to main content
Back to problems
#2784
Easy Algorithms

Check if array is good

Array Hash Table Sorting
48.7% acceptance
Feb 25, 2026
312
54
You are given an integer array nums. We consider an array good if it is a permutation of an array base[n]. base[n] = [1, 2, ..., n - 1, n, n] (in other words, it is an array of length n + 1 which contains 1 to n - 1 exactly once, plus two occurrences of n). For example, base[1] = [1, 1] and base[3] = [1, 2, 3, 3]. Return true if the given array is good, otherwise return false. Note: A permutation of integers represents an arrangement of these numbers.

Solution

Rust
Time O(n log n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn is_good(nums: Vec<i32>) -> bool {
    let mut nums = nums;
    nums.sort_unstable();
    let n = nums.len();
    let max_val = nums[n - 1];
    // base[max_val] has max_val+1 elements
    if n as i32 != max_val + 1 { return false; }
    // Check sorted nums == [1, 2, ..., max_val-1, max_val, max_val]
    for i in 0..n - 1 {
      if nums[i] != i as i32 + 1 { return false; }
    }
    true // last element equals max_val (guaranteed since it's the maximum)
  }
}