Check an Array Against the Good-Array Definition

Quick Overview

Check whether an array can be rearranged into the positive consecutive values ending with exactly two copies of its largest required value.

Check an Array Against the Good-Array Definition

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Check an Array Against the Good-Array Definition You are given an integer array `nums`. For a positive integer `k`, define `base[k] = [1, 2, ..., k - 1, k, k]`. This array has length `k + 1`: each integer from `1` through `k - 1` occurs once, and `k` occurs twice. For example, `base[1] = [1,1]` and `base[3] = [1,2,3,3]`. An array is good if it is a permutation of `base[k]` for some `k`. A permutation is an arrangement of the same values with the same multiplicities. Return `true` if `nums` is good, or `false` otherwise. ### Examples **Example 1** ```text Input: nums = [4,2,1,4,3] Output: true ``` These values can be arranged as `[1,2,3,4,4]`, which is `base[4]`. **Example 2** ```text Input: nums = [1,2,2,4,4] Output: false ``` The array has five elements, but it cannot be arranged as `[1,2,3,4,4]`: it has a second `2` and no `3`. ### Constraints - `1 <= nums.length <= 100`. - `1 <= nums[i] <= 200`. ### Clarifying Questions - Does the order of the input values matter? No; any arrangement with the required multiplicities qualifies. - Can values be inserted or removed? No; the full input must be a permutation of the required array. - Which value may occur twice? Only the largest value in the corresponding `base[k]`; every smaller positive value must occur exactly once. - Is a one-element input good? No; even `base[1]` has two elements. ### C++ Interface ```cpp class Solution { public: bool isGood(vector<int>& nums) { } }; ``` ### Reasoning Checkpoints - Check the complete multiplicity requirement rather than just the largest value. - Account for input values outside the candidate pattern. - Preserve the input's elements when checking whether a rearrangement exists. ```hint Candidate pattern How does the length of base[k] constrain which value of k could match a given input length? ```

Overview: Check whether an array can be rearranged into the positive consecutive values ending with exactly two copies of its largest required value.

Google logo
Google
Source window: 0–6 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Check an Array Against the Good-Array Definition

You are given an integer array nums. For a positive integer k, define

base[k] = [1, 2, ..., k - 1, k, k].

This array has length k + 1: each integer from 1 through k - 1 occurs once, and k occurs twice. For example, base[1] = [1,1] and base[3] = [1,2,3,3].

An array is good if it is a permutation of base[k] for some k. A permutation is an arrangement of the same values with the same multiplicities. Return true if nums is good, or false otherwise.

Examples

Example 1

Input: nums = [4,2,1,4,3]
Output: true

These values can be arranged as [1,2,3,4,4], which is base[4].

Example 2

Input: nums = [1,2,2,4,4]
Output: false

The array has five elements, but it cannot be arranged as [1,2,3,4,4]: it has a second 2 and no 3.

Constraints

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

Clarifying Questions Guidance

  • Does the order of the input values matter? No; any arrangement with the required multiplicities qualifies.
  • Can values be inserted or removed? No; the full input must be a permutation of the required array.
  • Which value may occur twice? Only the largest value in the corresponding base[k] ; every smaller positive value must occur exactly once.
  • Is a one-element input good? No; even base[1] has two elements.

C++ Interface

class Solution {
public:
    bool isGood(vector<int>& nums) {
        
    }
};

Reasoning Checkpoints

  • Check the complete multiplicity requirement rather than just the largest value.
  • Account for input values outside the candidate pattern.
  • Preserve the input's elements when checking whether a rearrangement exists.