BackhardSortingRazorpayNetflix

Maximized Frequency Balance Solution

Problem Statement

You are given an array nums of integers. For any contiguous subarray, define the Frequency Balance as the absolute difference between the maximum frequency of any single element within that subarray and the minimum frequency of any single element within that subarray. Note that the minimum frequency is calculated over the set of distinct elements present in the subarray. Your task is to find the maximum possible Frequency Balance across all possible contiguous subarrays of nums.

Return the maximum Frequency Balance value. If the array contains only one distinct element, the balance is 0 for all subarrays.

Example 1
Input
nums = [1, 2, 1, 3, 1]
Output
2

Explanation: Consider the subarray [1, 2, 1, 3, 1]. The frequencies are: 1 appears 3 times, 2 appears 1 time, 3 appears 1 time. Max frequency = 3, Min frequency = 1. Balance = |3 - 1| = 2. No other subarray yields a higher balance.

Example 2
Input
nums = [5, 5, 5, 5]
Output
0

Explanation: All elements are identical. For any subarray, the only distinct element is 5. Max frequency = length of subarray, Min frequency = length of subarray. Balance = 0.

Example 3
Input
nums = [1, 2, 3, 4, 5]
Output
0

Explanation: All elements are distinct. For any subarray, every element appears exactly once. Max frequency = 1, Min frequency = 1. Balance = 0.

Example 4
Input
nums = [7, 7, 8, 8, 8, 9]
Output
2

Explanation: Consider the full array [7, 7, 8, 8, 8, 9]. Frequencies: 7->2, 8->3, 9->1. Max=3, Min=1. Balance=2. Subarray [7,7,8,8,8] gives Max=3, Min=2, Balance=1. Subarray [8,8,8,9] gives Max=3, Min=1, Balance=2. Maximum is 2.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The answer is guaranteed to fit in a 32-bit integer.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximized Frequency Balance — Problem Statement & Solution Guide

SortingHardCustom Comparator
TimeO(n log n) (or O(n) with bucket array)
|
SpaceO(n)

Problem Description

You are given an array nums of integers. For any contiguous subarray, define the Frequency Balance as the absolute difference between the maximum frequency of any single element within that subarray and the minimum frequency of any single element within that subarray. Note that the minimum frequency is calculated over the set of distinct elements present in the subarray. Your task is to find the maximum possible Frequency Balance across all possible contiguous subarrays of nums.

Return the maximum Frequency Balance value. If the array contains only one distinct element, the balance is 0 for all subarrays.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Maximized Frequency Balance"

hard

WHY DOES IT MATTER?

The pattern combines sliding‑window with dynamic frequency tracking, a staple for problems that ask for the longest/shortest subarray satisfying a statistical constraint (e.g., at most K distinct, equal number of 0s and 1s). Mastery of this pattern lets you turn exponential subarray enumeration into linear time solutions.

OPTIMIZATION CHALLENGE

The breakthrough is realizing that we do not need the full distribution of frequencies – only the highest and lowest non‑zero frequencies. By storing how many values have each frequency (a frequency‑bucket array or ordered map), we can adjust the current max/min in O(1) amortized when a count changes, eliminating the need to recompute from scratch.

REAL-WORLD CONNECTION

Think of a live traffic monitor that continuously aggregates vehicle types passing a checkpoint. The system must instantly report the imbalance between the most common vehicle (e.g., trucks) and the least common (e.g., bicycles) within the last minute. Sliding the time window forward while updating counters mirrors the algorithmic technique.

During an interview, start by describing the naive O(n^3) idea, then immediately pivot to “what if we could keep the frequency histogram alive as the window slides?” Sketch the two‑pointer skeleton first; the rest (bucket updates) follows naturally and shows you can think in terms of incremental state.

COMPLEXITY AT A GLANCE

⏱ Time:O(n log n) (or O(n) with bucket array)
💾 Space:O(n)

Core Theory — Why This Approach?

The Frequency Balance of a subarray is defined as maxFreq – minFreq, where maxFreq is the highest occurrence count of any value inside the subarray and minFreq is the lowest occurrence count among the distinct values that actually appear. A naïve solution enumerates every possible subarray (O(n^2) choices) and recomputes the frequency map from scratch (another O(n) per subarray), leading to O(n^3) time – impossible for n up to 10^5. The optimal paradigm treats the problem as a sliding‑window / two‑pointer search while maintaining a dynamic frequency histogram. By keeping two auxiliary structures – a hash map value→count and a balanced multiset (or two arrays of buckets) that stores how many values have a given count – we can query the current maximum and minimum non‑zero frequencies in O(1) or O(log n). As the right pointer expands, we increment the count of nums[right] and update the buckets; as the left pointer contracts, we decrement the count and adjust the buckets. The window is always valid because the balance only grows when the gap between the highest and lowest bucket indices widens. This yields an overall O(n log n) (or O(n) with bucket arrays) solution, dramatically faster than the cubic brute force.

Interview Questions on This Problem

Q1How would you modify the algorithm if the definition of Frequency Balance used the *second* smallest frequency instead of the minimum?

Maintain a second‑order statistic in the frequency multiset – for example, keep a min‑heap of frequencies and a secondary min‑heap that skips the smallest element, or store counts in a Fenwick tree indexed by frequency. When the smallest frequency changes, update the second smallest accordingly, keeping all operations O(log n).

Q2Can the same sliding‑window technique be applied when the array contains up to 10^9 distinct values?

Yes, because the algorithm only stores frequencies for values that are currently inside the window. The hash map grows at most to the window size, not to the total distinct universe, so memory stays O(windowSize) ≤ O(n). The bucket structure can be implemented with a map from frequency to count of values, also bounded by O(windowSize).

Q3Explain why a monotonic queue cannot be used directly to maintain minFreq and maxFreq in this problem.

A monotonic queue works when the metric is a function of the order of elements (e.g., max/min of values) and each element enters and leaves exactly once. Here the metric depends on *counts* of values, which change arbitrarily as the window slides; a single element’s removal can affect the frequency of many distinct values, breaking the monotonic property. Hence we need a structure that can update frequencies incrementally, such as a hash map plus a frequency‑to‑count multiset.

Examples

Example 1

Input

nums = [1, 2, 1, 3, 1]

Output

2

Explanation: Consider the subarray [1, 2, 1, 3, 1]. The frequencies are: 1 appears 3 times, 2 appears 1 time, 3 appears 1 time. Max frequency = 3, Min frequency = 1. Balance = |3 - 1| = 2. No other subarray yields a higher balance.

Example 2

Input

nums = [5, 5, 5, 5]

Output

0

Explanation: All elements are identical. For any subarray, the only distinct element is 5. Max frequency = length of subarray, Min frequency = length of subarray. Balance = 0.

Example 3

Input

nums = [1, 2, 3, 4, 5]

Output

0

Explanation: All elements are distinct. For any subarray, every element appears exactly once. Max frequency = 1, Min frequency = 1. Balance = 0.

Example 4

Input

nums = [7, 7, 8, 8, 8, 9]

Output

2

Explanation: Consider the full array [7, 7, 8, 8, 8, 9]. Frequencies: 7->2, 8->3, 9->1. Max=3, Min=1. Balance=2. Subarray [7,7,8,8,8] gives Max=3, Min=2, Balance=1. Subarray [8,8,8,9] gives Max=3, Min=1, Balance=2. Maximum is 2.

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • The answer is guaranteed to fit in a 32-bit integer.

Optimal Approach & Strategy

Use two pointers to maintain a sliding window, update a value→count map and a count→how‑many‑values map, and read max and min non‑zero frequencies in O(1) per step.

Brute Force Approach

Enumerate every possible subarray, build a frequency map for each, compute max and min frequencies, and keep the largest difference.

Verified Code Solutions

JavaScript Solution
Time: O(n log n) (or O(n) with bucket array)
function solution(nums) {
      nums.sort((a, b) => a - b);
      let median;
      if (nums.length % 2 === 0) {
         median = (nums[nums.length / 2 - 1] + nums[nums.length / 2]) / 2;
      } else {
         median = nums[Math.floor(nums.length / 2)];
      }
      let max = Math.max(...nums);
      let min = Math.min(...nums);
      let avg = (max + min) / 2;
      return Math.abs(median - avg);
   }

Asked in Top Tech Interviews

RazorpayNetflix

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.