Watch 10 video solutions for Number of Centered Subarrays, a medium level problem involving Array, Hash Table, Enumeration. This walkthrough by yoBro has 582 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
| 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 |