You are given an integer array nums.
A subarray of nums is called centered if the sum of its elements is equal to at least one element within that same subarray.
Return the number of centered subarrays of nums.
Example 1:
Input: nums = [-1,1,0]
Output: 5
Explanation:
[-1], [1], [0]) are centered.[1, 0] has a sum of 1, which is present in the subarray.[-1, 1, 0] has a sum of 0, which is present in the subarray.Example 2:
Input: nums = [2,-3]
Output: 2
Explanation:
Only single-element subarrays ([2], [-3]) are centered.
Constraints:
1 <= nums.length <= 500-105 <= nums[i] <= 105Problem Overview: You are given an integer array and need to count how many subarrays are centered. A subarray is centered if there exists an index inside it that acts as the center and the elements around it balance in a way that keeps the center as the median of the subarray.
Approach 1: Brute Force Enumeration (O(n^3) time, O(1) space)
Enumerate every possible subarray using two nested loops. For each subarray, test every index as a potential center and verify whether it remains the median of the elements inside that subarray. This requires scanning the subarray and counting elements smaller and larger than the candidate center. The approach is straightforward but expensive: generating all subarrays costs O(n^2) and validating the center takes O(n). It works only for very small inputs and mainly helps you reason about the centered property before optimizing.
Approach 2: Hash Table + Enumeration (O(n^2) time, O(n) space)
Fix each index i as the center and expand outward. Track the balance between numbers greater than nums[i] and numbers smaller than it. Define a running balance such as +1 for greater elements and -1 for smaller ones. When the balance of the left and right sides cancel out, the center behaves like the median of that subarray.
A hash table stores frequencies of balance values observed on one side while expanding. As you enumerate elements on the other side, look up complementary balances that form a valid centered configuration. This converts repeated scans into constant‑time lookups. The outer loop enumerates every possible center (O(n)), while expansions and hash lookups take another O(n), giving O(n^2) time overall and O(n) extra space.
This strategy combines array traversal with hash table frequency counting to avoid recomputing comparisons for every candidate subarray. Instead of validating each subarray independently, you reuse balance information around the center.
Recommended for interviews: The hash table + enumeration method is the expected solution. Interviewers want to see that you first reason about the brute force definition of a centered subarray, then convert repeated checks into prefix balance counts with constant‑time hash lookups. That shift from repeated scanning to balance tracking demonstrates strong problem‑solving and familiarity with enumeration patterns.
We enumerate all starting indices i of subarrays, then starting from index i, we enumerate the ending index j of the subarray, calculate the sum s of elements in the subarray nums[i ldots j], and add all elements in the subarray to the hash table st. After each enumeration, we check if s appears in the hash table st. If it does, it means the subarray nums[i ldots j] is a centered subarray, and we increment the answer by 1.
The time complexity is O(n^2) and the space complexity is O(n), where n is the length of the array nums.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(n^3) | O(1) | Understanding the centered condition or validating small inputs |
| Hash Table + Enumeration | O(n^2) | O(n) | General case. Uses balance counting and hash lookups for efficient centered subarray counting |
LeetCode 3804. Number of Centered Subarrays | Weekly Contest 484 #leetcode #dailycoding #dsa • yoBro • 582 views views
Watch 9 more video solutions →Practice Number of Centered Subarrays with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor