Longest Subarray with Balanced Frequencies

Quick Overview

Find the longest contiguous subarray with one distinct value or exactly two positive frequency levels in a two-to-one ratio, with both levels represented.

Longest Subarray with Balanced Frequencies

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Longest Subarray with Balanced Frequencies You are given an integer array `nums`. A nonempty contiguous subarray is frequency balanced when either: - It contains just one distinct value, with any positive number of occurrences; or - There is a positive integer `f` such that each distinct value occurs either `f` or `2 * f` times, and at least one value has each frequency. Return the length of the longest frequency-balanced subarray. ### Function `int getLength(vector<int>& nums)` ### Example 1 **Input:** `nums = [8, 8, 3, 8, 3, 8]` **Output:** `6` **Explanation:** In the whole array, `8` occurs four times and `3` occurs twice. These are the required two frequencies for `f = 2`. ### Example 2 **Input:** `nums = [4, 4, 9, 9]` **Output:** `3` **Explanation:** The whole array has two distinct values that each occur twice, so it is not balanced: both required frequencies must occur. The subarray `[4, 4, 9]` has frequencies `2` and `1` and is balanced, giving length `3`. ### Constraints - `1 <= nums.length <= 1000` - `1 <= nums[i] <= 1000000000` ### Clarifications - A subarray consists of consecutive elements; it is not an arbitrary subsequence. - A subarray with multiple distinct values all occurring equally often does not qualify. - Only values present in the chosen subarray participate in the frequency condition. - A singleton always qualifies, so the answer is at least `1`. ### What a Strong Answer Covers - Applying the one-distinct-value exception separately from the two-frequency rule. - Requiring exactly the allowed frequency levels, with both present. - Finding the longest qualifying contiguous range while supporting large element values.

Overview: Find the longest contiguous subarray with one distinct value or exactly two positive frequency levels in a two-to-one ratio, with both levels represented.

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

Longest Subarray with Balanced Frequencies

You are given an integer array nums. A nonempty contiguous subarray is frequency balanced when either:

  • It contains just one distinct value, with any positive number of occurrences; or
  • There is a positive integer f such that each distinct value occurs either f or 2 * f times, and at least one value has each frequency.

Return the length of the longest frequency-balanced subarray.

Function

int getLength(vector<int>& nums)

Example 1

Input: nums = [8, 8, 3, 8, 3, 8]

Output: 6

Explanation: In the whole array, 8 occurs four times and 3 occurs twice. These are the required two frequencies for f = 2.

Example 2

Input: nums = [4, 4, 9, 9]

Output: 3

Explanation: The whole array has two distinct values that each occur twice, so it is not balanced: both required frequencies must occur. The subarray [4, 4, 9] has frequencies 2 and 1 and is balanced, giving length 3.

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000000000

Clarifications

  • A subarray consists of consecutive elements; it is not an arbitrary subsequence.
  • A subarray with multiple distinct values all occurring equally often does not qualify.
  • Only values present in the chosen subarray participate in the frequency condition.
  • A singleton always qualifies, so the answer is at least 1 .

What a Strong Answer Covers Guidance

  • Applying the one-distinct-value exception separately from the two-frequency rule.
  • Requiring exactly the allowed frequency levels, with both present.
  • Finding the longest qualifying contiguous range while supporting large element values.