I have an array of N integers, and I have to find the number of such segments that they contain within themselves segments whose cumulative sum is equal to zero. For example, if I have an array [5, -5, 5], then [0,1], [1,2], [0,2] (the left number is the left border of the segment, the right number is the right border of the segment, both inclusive) would be suitable segments, because they contain segments [0, 1] and [1, 2] with cumulative sum equal to zero , so the answer will be 3. Another example is [1, 2, 3, -6]. In this case, the answer will be 1, because there is only one segment with cumulative sum equal to 0, and this segment will be contained only in the segment [0,3].
I tried to solve the problem by calculating the cumulative sum for the entire array, after which I found segments with a cumulative sum equal to zero. After that, I go through all possible segments, and count the segments containing segments with a cumulative sum equal to zero. The program outputs the correct answer, but I'm looking for a more efficient solution.