↓Skip to main content
  1. LeetCode/

LeetCode 3960: Frequency Balance Subarray

·2 mins· ·
LeetCode Medium Hash-Table Counting Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-06-18

  • Submissions: Wrong Answer → Accepted

Approach
#

n <= 1000, so O(n²) is fine. Fix the left end and extend right; each new number changes O(1) counts (Hash Table Template 4):

  • cnt: value -> count.
  • freq: count -> how many values have it. When a value goes from c to c + 1, decrement freq[c] (deleting it at 0) and increment freq[c + 1].

Balanced means a single value, or freq holding exactly two counts with the larger twice the smaller.

My Attempts
#

Attempt 1 had an extra condition, min(freq_map) in apperance_map, looking up a count in the value -> count table; they mean different things. For [2, 3, 2], 2 appears twice and 3 once, so the whole array works and the answer is 3; the smallest count, 1, is not a value in the array, so it returned 1. Removing the condition passed.

Solution
#

class Solution:
    def getLength(self, nums: List[int]) -> int:
        n, best = len(nums), 0
        for i in range(n):
            cnt, freq = {}, Counter()
            for j in range(i, n):
                c = cnt.get(nums[j], 0)
                if c:
                    freq[c] -= 1
                    if freq[c] == 0:
                        del freq[c]
                cnt[nums[j]] = c + 1
                freq[c + 1] += 1
                if len(cnt) == 1 or (len(freq) == 2 and max(freq) == 2 * min(freq)):
                    best = max(best, j - i + 1)
        return best

Complexity
#

Time O(n²), space O(n). max(freq) and min(freq) only run when len(freq) == 2, so they are O(1).

Mistakes to Avoid
#

  • Delete a count that reaches 0, or len(freq) is wrong.
  • “Count” and “value” are different keys; do not mix them.

Interview Explanation
#

With n only 1000, I fix the left end and extend right, tracking each value’s count and how many values have each count; every new number updates just two of those. When there is a single value, or exactly two counts with one twice the other, I update the answer. Time O(n²).

Related

LeetCode 923: 3Sum With Multiplicity
·4 mins
LeetCode Medium Two-Pointers Sorting Hash-Table Counting Array
LeetCode 923 note: sort, fix the first number, and find the other two with converging pointers; equal end values are counted with a combination, different ones by multiplying each end’s duplicate count
LeetCode 3679: Minimum Discards to Balance Inventory
·3 mins
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 note: the rule already decides what must be discarded, so simulate it; the length-w window counts only kept arrivals, and discarded ones must not be subtracted when they leave
LeetCode 1814: Count Nice Pairs in an Array
·1 min
LeetCode Medium Array Hash-Table Math Counting
LeetCode 1814 note: rearrange the condition into nums[i] - rev(nums[i]) == nums[j] - rev(nums[j]), count pairs per value, and remember the modulus
LeetCode 3071: Minimum Operations to Write the Letter Y on a Grid
·2 mins
LeetCode Medium Array Hash-Table Matrix Counting
LeetCode 3071 note: count each value on and off the Y, enumerate the Y value a and background value b (a != b), and take the fewest cells to change
LeetCode 229: Majority Element II
·2 mins
LeetCode Medium Array Hash-Table Sorting Counting
LeetCode 229 note: at most two values can exceed n / 3; Boyer-Moore keeps two candidates and a second pass verifies their counts
LeetCode 3843: First Element with Unique Frequency
·1 min
LeetCode Medium Hash-Table Counting Array
LeetCode 3843 note: count each value, then count how many values share each count; the first element from the left whose count is its own is the answer