Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[-1,1,0]
[2,-3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3804: Number of Centered Subarrays
class Solution {
public int centeredSubarrays(int[] nums) {
int n = nums.length;
int ans = 0;
for (int i = 0; i < n; i++) {
Set<Integer> st = new HashSet<>();
int s = 0;
for (int j = i; j < n; j++) {
s += nums[j];
st.add(nums[j]);
if (st.contains(s)) {
ans++;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3804: Number of Centered Subarrays
func centeredSubarrays(nums []int) int {
n := len(nums)
ans := 0
for i := 0; i < n; i++ {
st := make(map[int]struct{})
s := 0
for j := i; j < n; j++ {
s += nums[j]
st[nums[j]] = struct{}{}
if _, ok := st[s]; ok {
ans++
}
}
}
return ans
}
# Accepted solution for LeetCode #3804: Number of Centered Subarrays
class Solution:
def centeredSubarrays(self, nums: List[int]) -> int:
n = len(nums)
ans = 0
for i in range(n):
st = set()
s = 0
for j in range(i, n):
s += nums[j]
st.add(nums[j])
if s in st:
ans += 1
return ans
// Accepted solution for LeetCode #3804: Number of Centered Subarrays
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3804: Number of Centered Subarrays
// class Solution {
// public int centeredSubarrays(int[] nums) {
// int n = nums.length;
// int ans = 0;
// for (int i = 0; i < n; i++) {
// Set<Integer> st = new HashSet<>();
// int s = 0;
// for (int j = i; j < n; j++) {
// s += nums[j];
// st.add(nums[j]);
// if (st.contains(s)) {
// ans++;
// }
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3804: Number of Centered Subarrays
function centeredSubarrays(nums: number[]): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n; i++) {
const st = new Set<number>();
let s = 0;
for (let j = i; j < n; j++) {
s += nums[j];
st.add(nums[j]);
if (st.has(s)) {
ans++;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.