↓Skip to main content
  1. LeetCode/

LeetCode 3804: Number of Centered Subarrays

·1 min· ·
LeetCode Medium Hash-Table Enumeration 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-01-11

  • Submissions: Accepted (first try, 2.8 s)

Approach
#

With n <= 500 there are O(n²) subarrays. Fix the left end i and extend the right end j:

  • Add to the sum as it extends.
  • Keep the subarray’s values in a set, also extended incrementally, for O(1) lookups.

My Solution
#

I originally checked curr_sum in nums[i: j + 1], slicing and scanning every time, O(n³) overall, 2.8 s. With an incremental set:

class Solution:
    def centeredSubarrays(self, nums: List[int]) -> int:
        n, res = len(nums), 0
        for i in range(n):
            seen, total = set(), 0
            for j in range(i, n):
                total += nums[j]
                seen.add(nums[j])
                res += total in seen
        return res

Complexity
#

Time O(n²), space O(n).

Mistakes to Avoid
#

  • Do not slice and search each time; that adds an O(n) factor.

Interview Explanation
#

I fix the left end and extend right, keeping the running sum and a set of values seen; whenever the sum is in the set, the subarray counts. Each extension is O(1), so O(n²) total.

Related

LeetCode 2958: Length of Longest Subarray With at Most K Frequency
·1 min
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2958 note: a longest-window slide that shrinks the left whenever the newly added number appears more than k times
LeetCode 2653: Sliding Subarray Beauty
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2653 note: values are only -50 to 50, so the fixed window keeps a count array, and each window accumulates upward from -50 to find the x-th smallest negative
LeetCode 2831: Find the Longest Equal Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Binary-Search Array
LeetCode 2831 note: list the positions of each value and slide a window over that list; the deletions needed are the span of positions minus the number of copies
LeetCode 3795: Minimum Subarray Length With Distinct Sum At Least K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 3795 note: a shortest-window slide where each value counts once; track each value’s last position, and subtract the left value only if it was the last occurrence
LeetCode 2461: Maximum Sum of Distinct Subarrays With Length K
·2 mins
LeetCode Medium Sliding-Window Hash-Table Array
LeetCode 2461 note: in a fixed window of length k, all values are distinct exactly when the number of distinct values equals k
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