Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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.
Example 1:
Input: nums = [2, 1, 3] Output: false Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. However, base[3] has four elements but array nums has three. Therefore, it can not be a permutation of base[3] = [1, 2, 3, 3]. So the answer is false.
Example 2:
Input: nums = [1, 3, 3, 2] Output: true Explanation: Since the maximum element of the array is 3, the only candidate n for which this array could be a permutation of base[n], is n = 3. It can be seen that nums is a permutation of base[3] = [1, 2, 3, 3] (by swapping the second and fourth elements in nums, we reach base[3]). Therefore, the answer is true.
Example 3:
Input: nums = [1, 1] Output: true Explanation: Since the maximum element of the array is 1, the only candidate n for which this array could be a permutation of base[n], is n = 1. It can be seen that nums is a permutation of base[1] = [1, 1]. Therefore, the answer is true.
Example 4:
Input: nums = [3, 4, 4, 1, 2, 1] Output: false Explanation: Since the maximum element of the array is 4, the only candidate n for which this array could be a permutation of base[n], is n = 4. However, base[4] has five elements but array nums has six. Therefore, it can not be a permutation of base[4] = [1, 2, 3, 4, 4]. So the answer is false.
Constraints:
1 <= nums.length <= 1001 <= num[i] <= 200Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[2, 1, 3]
[1, 3, 3, 2]
[1, 1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2784: Check if Array is Good
class Solution {
public boolean isGood(int[] nums) {
int n = nums.length - 1;
int[] cnt = new int[201];
for (int x : nums) {
++cnt[x];
}
if (cnt[n] != 2) {
return false;
}
for (int i = 1; i < n; ++i) {
if (cnt[i] != 1) {
return false;
}
}
return true;
}
}
// Accepted solution for LeetCode #2784: Check if Array is Good
func isGood(nums []int) bool {
n := len(nums) - 1
cnt := [201]int{}
for _, x := range nums {
cnt[x]++
}
if cnt[n] != 2 {
return false
}
for i := 1; i < n; i++ {
if cnt[i] != 1 {
return false
}
}
return true
}
# Accepted solution for LeetCode #2784: Check if Array is Good
class Solution:
def isGood(self, nums: List[int]) -> bool:
cnt = Counter(nums)
n = len(nums) - 1
return cnt[n] == 2 and all(cnt[i] for i in range(1, n))
// Accepted solution for LeetCode #2784: Check if Array is Good
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2784: Check if Array is Good
// class Solution {
// public boolean isGood(int[] nums) {
// int n = nums.length - 1;
// int[] cnt = new int[201];
// for (int x : nums) {
// ++cnt[x];
// }
// if (cnt[n] != 2) {
// return false;
// }
// for (int i = 1; i < n; ++i) {
// if (cnt[i] != 1) {
// return false;
// }
// }
// return true;
// }
// }
// Accepted solution for LeetCode #2784: Check if Array is Good
function isGood(nums: number[]): boolean {
const n = nums.length - 1;
const cnt: number[] = Array(201).fill(0);
for (const x of nums) {
++cnt[x];
}
if (cnt[n] !== 2) {
return false;
}
for (let i = 1; i < n; ++i) {
if (cnt[i] !== 1) {
return false;
}
}
return true;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.