3804. Number of Centered Subarrays Medium

@problem@discussion
#Array#Hash Table#Enumeration



1/**
2 * [3804] Number of Centered Subarrays
3 *
4 * You are given an integer array nums.
5 * A <span data-keyword="subarray-nonempty">subarray</span> of nums is called centered if the sum of its elements is equal to at least one element within that same subarray.
6 * Return the number of centered subarrays of nums.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">nums = [-1,1,0]</span>
11 * Output: <span class="example-io">5</span>
12 * Explanation:
13 * 
14 * 	All single-element subarrays ([-1], [1], [0]) are centered.
15 * 	The subarray [1, 0] has a sum of 1, which is present in the subarray.
16 * 	The subarray [-1, 1, 0] has a sum of 0, which is present in the subarray.
17 * 	Thus, the answer is 5.
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">nums = [2,-3]</span>
22 * Output: <span class="example-io">2</span>
23 * Explanation:
24 * Only single-element subarrays ([2], [-3]) are centered.
25 * </div>
26 *  
27 * Constraints:
28 * 
29 * 	1 <= nums.length <= 500
30 * 	-10^5 <= nums[i] <= 10^5
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/number-of-centered-subarrays/
36// discuss: https://leetcode.com/problems/number-of-centered-subarrays/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41    pub fn centered_subarrays(nums: Vec<i32>) -> i32 {
42        0
43    }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_3804() {
54    }
55}
56

Back
© 2026 bowen.ge All Rights Reserved.