Check if Array is Good

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
Inputnums = [2,1,3]
Outputfalse
Since the maximum element is 3, the only candidate is n = 3, but base[3] has four elements while nums has three, so it is not a permutation of base[3].
Example 2
Inputnums = [1,3,3,2]
Outputtrue
Since the maximum element is 3, the only candidate is n = 3, and nums is a permutation of base[3] = [1, 2, 3, 3].

Constraints

  • 1 <= nums.length <= 100
  • 1 <= num[i] <= 200

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate