Check if Array is Good
EasyUpdated: Jul 3, 2025
Practice on:
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.
Examples
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] <= 200
Solution
Method 1 – Frequency Counting
Intuition
The array is good if it is a permutation of [1, 2, ..., n-1, n, n], i.e., it contains all numbers from 1 to n-1 exactly once, and n exactly twice. We can check this by counting the frequency of each number and comparing with the expected pattern.
Approach
- Find the maximum value
nin the array. - Check if the array length is
n + 1(since base[n] has n+1 elements). - Count the frequency of each number in the array.
- For numbers from 1 to n-1, each should appear exactly once.
- The number n should appear exactly twice.
- If all conditions are satisfied, return true; otherwise, return false.
Code
C++
class Solution {
public:
bool isGood(vector<int>& nums) {
int n = *max_element(nums.begin(), nums.end());
if (nums.size() != n + 1) return false;
vector<int> cnt(n + 1);
for (int x : nums) cnt[x]++;
for (int i = 1; i < n; ++i) if (cnt[i] != 1) return false;
return cnt[n] == 2;
}
};
Go
func isGood(nums []int) bool {
n := 0
for _, v := range nums {
if v > n { n = v }
}
if len(nums) != n+1 { return false }
cnt := make([]int, n+1)
for _, v := range nums { cnt[v]++ }
for i := 1; i < n; i++ { if cnt[i] != 1 { return false } }
return cnt[n] == 2
}
Java
class Solution {
public boolean isGood(int[] nums) {
int n = 0;
for (int x : nums) n = Math.max(n, x);
if (nums.length != n + 1) return false;
int[] cnt = new int[n + 1];
for (int x : nums) cnt[x]++;
for (int i = 1; i < n; ++i) if (cnt[i] != 1) return false;
return cnt[n] == 2;
}
}
Kotlin
class Solution {
fun isGood(nums: IntArray): Boolean {
val n = nums.maxOrNull() ?: 0
if (nums.size != n + 1) return false
val cnt = IntArray(n + 1)
for (x in nums) cnt[x]++
for (i in 1 until n) if (cnt[i] != 1) return false
return cnt[n] == 2
}
}
Python
class Solution:
def isGood(self, nums: list[int]) -> bool:
n = max(nums)
if len(nums) != n + 1:
return False
cnt = [0] * (n + 1)
for x in nums:
cnt[x] += 1
for i in range(1, n):
if cnt[i] != 1:
return False
return cnt[n] == 2
Rust
impl Solution {
pub fn is_good(nums: Vec<i32>) -> bool {
let n = *nums.iter().max().unwrap();
if nums.len() as i32 != n + 1 { return false; }
let mut cnt = vec![0; (n + 1) as usize];
for &x in &nums { cnt[x as usize] += 1; }
for i in 1..n as usize { if cnt[i] != 1 { return false; } }
cnt[n as usize] == 2
}
}
Complexity
- ⏰ Time complexity:
O(n), wherenis the length of the array. - 🧺 Space complexity:
O(n)for the frequency map.