Find if there is a subarray with 0 sum | GeeksforGeeks

preview_player
Показать описание


This video is contributed by Shubham Kumar

Please Like, Comment and Share the Video among your friends.

Follow us on Facebook:

And Twitter:

Also, Subscribe if you haven't already! :)
Рекомендации по теме
Комментарии
Автор

The reason why this works is that if there is a repeating prefix sum, that means adding all the elements before that has resulted in zero. Here, prefix sums are 1, 5, 3, 1 meaning that whatever was elements were added to form 1 add up to 0 themselves. Meaning, 4, -2, -2 become 0!

StingSting
Автор

Explanation:
Suppose we have the prefix sums as : 1, 5, 3, 1, 6, 2, 5
Now see as we move from prefix sum 1->5 we have an increment of 4. This means our sub-array has [4] or [1, 2, 1] or [2, 2], basically elements that add upto 4. Now as we go from 5->1, we again have a drop of 4, so our sub-array can have elements as [-4], [-5, 1] or [10, -16, 2], basically which makes our sum as -4. Now as we are considering a contiguous sub-array, the elements causing increment by 4 and decrement by 4 will come in the same sub-array making the sum as 0

hardikraj
Автор

weak explanation you just read whatever was written on the back slide or whatever document. this is just like text to speech program. you should explain it well

anujsaini
Автор

Explanation: If at any point in the iteration, the prefix sum repeats, that means the subarray starting from previous prefix sum and current prefix sum, has a sum of zero. That is why there is no increase in the sum.

MrAbhay
Автор

Hi, if for eg array A=[4, -2, 1, -2, 1, 3] in this case the prefix sums would be=4, 2, 3, 1, 2, 5 . So here prefix sum= 2 repeats whereas there is no contiguous sub-array with sum =0. Here the OR condition might fail. Kindly correct me if I am wrong.

srs.shashank
Автор

How can we calculate the first index of subaaray when sumSet.find() statement is trus? Please help

thestone
Автор

This code(Hashing based one) didnt work for int arr[] = {1, 4, 2, 2, -5}; here {1, 4, -5} is the subarray for which sum=0, is this solution only applicable for continuous subarray finding only

gauravkvtamboli
Автор

Can it be done by O(N) which will reduce the time of the program?

martialcoder
Автор

I came to know from this problem that hashset is faster than arraylist

shriyanshagarwal
Автор

its great the code is short. but you forgot to explain why this works.

spicytuna
Автор

Can anyone provide an implementation of this solution in python language?

jiba_jyoti_panda_
Автор

Shoudn't the title be renamed to "Find if there's a contiguous subarray with 0 sum" ?

sagarsakre
Автор

these videos are crap. He is just going through the description given in the geeksforgeeks website. He is ranting about how this solution derived

atefnazi
Автор

hey in second eg -3 and 3 form up a zero

ankitapawaskar
welcome to shbcf.ru