Find the Shortest Segment Preserving Array Degree

Quick Overview

Find the Shortest Segment Preserving Array Degree. The degree of nums is its highest value frequency.

Find the Shortest Segment Preserving Array Degree

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem The degree of nums is its highest value frequency. Return the minimum length of a contiguous subarray having the same degree as the entire array. ```cpp class Solution { public: int findShortestSubArray(vector<int>& nums); }; ``` ### Examples **Example 1:** ```text nums = [4,1,4,2,1,1] Output: 5 Explanation: One occurs three times; the shortest preserving span runs from index 1 to 5. ``` **Example 2:** ```text nums = [7,8,9] Output: 1 Explanation: The degree is one, so any singleton suffices. ``` ### Constraints - The nonempty array contains at most 50000 integers. Values range from 0 through 49999. ### Clarifying Questions Preserve maximum frequency while comparing the full occurrence spans of all tied values. ```hint Read the boundary condition What interval is forced if a subarray must include every occurrence of a degree-achieving value? ``` ### Contract considerations Preserve maximum frequency while comparing the full occurrence spans of all tied values.

Overview: Find the Shortest Segment Preserving Array Degree. The degree of nums is its highest value frequency.

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

Problem

The degree of nums is its highest value frequency. Return the minimum length of a contiguous subarray having the same degree as the entire array.

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

Examples

Example 1:

nums = [4,1,4,2,1,1]
Output: 5
Explanation: One occurs three times; the shortest preserving span runs from index 1 to 5.

Example 2:

nums = [7,8,9]
Output: 1
Explanation: The degree is one, so any singleton suffices.

Constraints

  • The nonempty array contains at most 50000 integers. Values range from 0 through 49999.

Clarifying Questions Guidance

Preserve maximum frequency while comparing the full occurrence spans of all tied values.

Contract considerations

Preserve maximum frequency while comparing the full occurrence spans of all tied values.