10. Subarray with 0 sum
Last updated
Last updated
The problem can be found at the following link: Question Link
To check if there is a subarray with a sum of 0, I maintain a running sum while traversing the array. I use an unordered set to store the cumulative sum at each step. If, at any point, I find that the sum already exists in the set, it means there is a subarray with a sum of 0. This is because the difference between the current sum and the sum stored in the set would be 0.
Consider the array [4, 2, -3, 1, 6]. The running sum would be [4, 6, 3, 4, 10]. At the third step, the sum becomes 4, and we check if 4 is already in the set. Since it is, we know there is a subarray with a sum of 0 (from index 1 to 3: 2 - 3 + 1
).
Time Complexity : O(n)
, where n is the size of the array.
Auxiliary Space Complexity : O(n)
, where n is the size of the array.
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.