algorithm - Find number of continuous subarray having sum zero -


you have given array , have give number of continuous subarray sum zero.

example: 1)  0 ,1,-1,0 => 6 {{0},{1,-1},{0,1,-1},{1,-1,0},{0}}; 2)  5, 2, -2, 5 ,-5, 9 => 3. 

with o(n^2) can done.i trying find solution below complexity.

i don't know complexity of suggestion have idea :)
can try reduce element main array not able contribute solution suppose elements -10, 5, 2, -2, 5,7 ,-5, 9,11,19
can see -10,9,11 , 19 element
never gone useful make sum 0 in case
try remove -10,9,11, , 19 main array can

1) create 2 sub array main array   `positive {5,7,2,9,11,19}` , `negative {-10,-2,-5}`    2) remove element positive array not satisfy condition    condition -> value should construct negative arrays element      or sum of  elements      ie.           5 = -5 //so keep //don't consider sign          7 = (-5 + -2 ) // keep          2 = -2 // keep        9 // cannot construct using -10,-2,-5          same 11 , 19 3) remove element form negative array not satisfy condition       condition -> value should construct positive arrays element          or sum of  elements      i.e. -10 // cannot construct discard          -2 = 2 // keep          -5 = 5 // keep  

so got array contains -2,-5,5,7,2 create possible sub array form , check sum = 0
(note if input array contains 0 add 0's in final array)


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -