Example – A = [1, 4 , -1, 3, -7,-3, -1,10]

Output – Yes

Subarrays – [1,-1] , [4,3,-7], [3,-3] , [-3,-7,10]

Approach

O(n) – Easily solvable in linear time with hashing. We traverse the given array, and maintain sum of elements seen so far in a hash map or dictionary . If sum is seen before (i.e. sum exists in dictionary/hash), we return true as there exists at-least one sub-array with zero sum which ends at current index else we insert the sum into the set.

LEAVE A REPLY