Approach

problem 2 can also be done using hash and prefixsum, timing complexity is O(N^2)

Code

class Solution {
public:
    int centeredSubarrays(vector<int>& nums) {
        int n = nums.size();
        int totalCnt = 0;
        for(int i = 0 ; i < n ; ++i){
            unordered_set<int> st;
            int sum = 0;
            for(int j = i ; j < n ; ++j) {
                sum += nums[j];
                st.insert(nums[j]);
                if(st.count(sum)) ++totalCnt;
            }
        }
        return totalCnt;
    }
};