3804. Number of Centered Subarrays
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { public: int centeredSubarrays(vector<int>& nums) { int res = 0, n = nums.size(); for(int i = 0; i < n; i++) { unordered_set<int> us; for(int j = i, sum = 0; j < n; j++) { us.insert(nums[j]); sum += nums[j]; if(us.count(sum)) res++; } } return res; } };
|