Python3 Program to Find if there is a subarray with 0 sum
Last Updated :
06 Sep, 2024
Given an array of positive and negative numbers, find if there is a subarray (of size at-least one) with 0 sum.
Examples :
Input: {4, 2, -3, 1, 6}
Output: true
Explanation:
There is a subarray with zero sum from index 1 to 3.
Input: {4, 2, 0, 1, 6}
Output: true
Explanation:
There is a subarray with zero sum from index 2 to 2.
Input: {-3, 2, 3, 1, 6}
Output: false
A simple solution is to consider all subarrays one by one and check the sum of every subarray. We can run two loops: the outer loop picks a starting point i and the inner loop tries all subarrays starting from i (See this for implementation). The time complexity of this method is O(n2).
We can also use hashing. The idea is to iterate through the array and for every element arr[i], calculate the sum of elements from 0 to i (this can simply be done as sum += arr[i]). If the current sum has been seen before, then there is a zero-sum array. Hashing is used to store the sum values so that we can quickly store sum and find out whether the current sum is seen before or not.
Example :
arr[] = {1, 4, -2, -2, 5, -4, 3}
If we consider all prefix sums, we can
notice that there is a subarray with 0
sum when :
1) Either a prefix sum repeats or
2) Or prefix sum becomes 0.
Prefix sums for above array are:
1, 5, 3, 1, 6, 2, 5
Since prefix sum 1 repeats, we have a subarray
with 0 sum.
Following is implementation of the above approach.
Python3
# A python program to find if
# there is a zero sum subarray
def subArrayExists(arr, n):
# traverse through array
# and store prefix sums
n_sum = 0
s = set()
for i in range(n):
n_sum += arr[i]
# If prefix sum is 0 or
# it is already present
if n_sum == 0 or n_sum in s:
return True
s.add(n_sum)
return False
# Driver code
arr = [-3, 2, 3, 1, 6]
n = len(arr)
if subArrayExists(arr, n) == True:
print("Found a sunbarray with 0 sum")
else:
print("No Such sub array exits!")
# This code is contributed by Shrikant13
OutputNo Such Sub Array Exists!
Time Complexity of this solution can be considered as O(n) under the assumption that we have good hashing function that allows insertion and retrieval operations in O(1) time.
Space Complexity: O(n) .Here we required extra space for unordered_set to insert array elements.
Please refer complete article on
Find if there is a subarray with 0 sum for more details!
Similar Reads
Find all subarrays with sum in the given range Given an unsorted array of size, N. Find subarrays that add to a sum in the given range L-R. Examples: Input: arr[] = {2, 3, 5, 8}, L = 4, R = 13Output: The indexes of subarrays are {0, 1}, {0, 2}, {1, 2}, {2, 2}, {2, 3}, {3, 3} Input: arr[] = {1, 4, 6}, L = 3, R = 8Output: The indexes of subarrays
4 min read
Python Program to Find Sum of Array Given an array of integers, find the sum of its elements. Examples: Input : arr[] = {1, 2, 3}Output : 6Explanation: 1 + 2 + 3 = 6This Python program calculates the sum of an array by iterating through each element and adding it to a running total. The sum is then returned. An example usage is provid
4 min read
Python | Find the number of unique subsets with given sum in array Given an array and a sum, find the count of unique subsets with each subset's sum equal to the given sum value. Examples: Input : 4 12 5 9 12 9 Output : 2 (subsets will be [4, 5] and [9]) Input : 1 2 3 4 5 10 Output : 3 We will use dynamic programming to solve this problem and this solution has time
2 min read
Count of subarray that does not contain any subarray with sum 0 Given an array arr, the task is to find the total number of subarrays of the given array which do not contain any subarray whose sum of elements is equal to zero. All the array elements may not be distinct.Examples: Input: arr = {2, 4, -6} Output: 5 Explanation: There are 5 subarrays which do not co
11 min read
First subarray with negative sum from the given Array Given an array arr[] consisting of N integers, the task is to find the start and end indices of the first subarray with a Negative Sum. Print "-1" if no such subarray exists. Note: In the case of multiple negative-sum subarrays in the given array, the first subarray refers to the subarray with the l
15+ min read
Print all subarrays with sum in a given range Given an array arr[] of positive integers and two integers L and R defining the range [L, R]. The task is to print the subarrays having sum in the range L to R. Examples: Input: arr[] = {1, 4, 6}, L = 3, R = 8Output: {1, 4}, {4}, {6}.Explanation: All the possible subarrays are the following{1] with
5 min read