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
Check if Array is Good: Pattern RecognitionInput Array2133Length = 4, so check base[3] patternExpected base[3]1233[1,2,3,3] in any orderFrequency Analysis1 appears 1×2 appears 1×3 appears 2×✓ Pattern matches base[3] → Result: true
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
Asked in
Google 15 Amazon 12 Microsoft 8
18.5K Views
Medium Frequency
~15 min Avg. Time
340 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen