0

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.

  • Please include your code in your post and add a language tag. That being said, I think a better site to ask this question is [CodeReview SE](http://codereview.stackexchange.com/help/on-topic) as your code already works – Spikatrix Mar 09 '23 at 13:19
  • 1
    @Spikatrix Without code this would be closed on Code Review as well. – pacmaninbw Mar 09 '23 at 13:22
  • @pacmaninbw Yeah, the OP should include their working code when posting it there – Spikatrix Mar 09 '23 at 13:27

0 Answers0