Class Solution
-
- All Implemented Interfaces:
public final class Solution
2784 - Check if Array is Good\.
Easy
You are given an integer array
nums
. We consider an array good if it is a permutation of an arraybase[n]
.base[n] = [1, 2, ..., n - 1, n, n]
(in other words, it is an array of lengthn + 1
which contains1
ton - 1
exactly once, plus two occurrences ofn
). For example,base[1] = [1, 1]
andbase[3] = [1, 2, 3, 3]
.Return
true
if the given array is good, otherwise returnfalse
.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 basen, is n = 3. However, base3 has four elements but array nums has three. Therefore, it can not be a permutation of base3 = 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 basen, is n = 3. It can be seen that nums is a permutation of base3 = 1, 2, 3, 3 (by swapping the second and fourth elements in nums, we reach base3). 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 basen, is n = 1. It can be seen that nums is a permutation of base1 = 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 basen, is n = 4. However, base4 has five elements but array nums has six. Therefore, it can not be a permutation of base4 = 1, 2, 3, 4, 4. So the answer is false.
Constraints:
1 <= nums.length <= 100
1 <= num[i] <= 200
-
-
Constructor Summary
Constructors Constructor Description Solution()
-