Check if Array is Good - Problem
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.
Input & Output
Example 1 — Valid Good Array
$
Input:
nums = [2,1,3,3]
›
Output:
true
💡 Note:
This array has length 4, so we expect base[3] = [1,2,3,3]. The input is a permutation of base[3], containing: 1 once, 2 once, and 3 twice.
Example 2 — Invalid Array
$
Input:
nums = [1,3,3,2]
›
Output:
true
💡 Note:
This array has length 4, so we expect base[3] = [1,2,3,3]. The input is a permutation of [1,2,3,3], just in different order.
Example 3 — Wrong Pattern
$
Input:
nums = [1,1]
›
Output:
true
💡 Note:
This array has length 2, so we expect base[1] = [1,1]. The input matches exactly.
Constraints
- 1 ≤ nums.length ≤ 100
- 1 ≤ nums[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [2,1,3,3] with length 4
2
Expected Pattern
base[3] = [1,2,3,3] (any permutation)
3
Verification
Check if frequencies match: 1→1, 2→1, 3→2
Key Takeaway
🎯 Key Insight: A good array must contain numbers 1 to n-1 exactly once, plus n exactly twice
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code