Largest Subarray with 0 Sum

Problem Statement
Given the array of size, has positive and negative integers. From the array , find the length of the largest subarray having a 0 sum.
As we have to find the length of the largest subarray. So, first of all, it is required to understand what is a subarray
Subarray :
A subarray of an array is defined as any contiguous sequence of elements of an array.
Example :
Subarrays of the above example :
Example
Let us understand the largest subarray with the 0 sum problem with the examples.
Example : 1
Output :
Example : 2
Output :
Example Explanation
We are required to find all the subarrays starting from the ith index and ending with the jth index where and where represents the length of the array.
Example : 1
First of all, we will find the sum of every subarray of the input array .
Here the size of the array is 7 so we are required to find the sum of all the subarrays starting from the ith index and ending with the jth index where and .
Refer to the below image for the explanation of Example1 :
Subarrays with 0 sum are : with length 3 and with length 4.
So the maximum length of subarray with 0 sum is 4.
Example : 2
First, we will find the sum of every subarray of the input array ar[]. Here in the example, the size of the array is 8 so we are required to find the sum of all the subarrays starting from the ith index and ending with the jth index where and .
Refer to the below image for the explanation of Example2 :
Subarrays with 0 sum are : with length 3 , with length 5, with length 2.
So the maximum length of subarray with a 0 sum is 5.
Input :
Given a positive integer n which represents the size of an array . And the n positive/negative integers represent the elements of .
Output :
You have to return the length of the largest subarray that has a sum of 0.
Constraints
The constraints for the largest subarray with a 0 sum problem are given below :
,
, for i in range 0 to .
Approach 1 : Brute Force Approach - Using Three Nested Loops
Approach
We will consider all the subarrays of the input array and find the sum of every subarray and check the sum of every subarray. And then, store the length of the longest subarray among all the subarrays that have a sum of 0 and return the longest length as a result.
Algorithm
- Declare and initialize a variable max_length , which stores the length of the largest subarray with 0 sum.
- Start traversing an array from the start.
- For every index i of the array, start a nested loop with variable j that ranges from i to the size of the array.
- Initialize the variable cursum for storing the sum of subarray starting from the ith index and ending with the jth index.
- And then run a loop to find the sum of subarray starting with the ith index and ending with the jth index and store the sum in cursum.
- Check if the value of cursum is 0, then check if the length of the current subarray is greater than the length stored in max_length and update the max_length with the current subarray length .
- Return the max_length.
Implementation
C++ Implementation :
Output :
Java Implementation :
Output :
Python Implementation :
Output :
Time Complexity
As we run three nested loops one loop for starting index of the subarray, the second loop for the ending index of the subarray, and the third loop for finding the sum of the current subarray. So worst case time complexity of running three nested loops is .
So worst case time complexity of this approach is .
Space Complexity
We are not using the extra space. So worst case space complexity of this approach is .
Approach 2 : Efficient Brute Force - Using Two Nested Loops
Approach
This approach is an optimized version of the previous approach. In this approach, we use the brute force approach, where two nested loops are used to calculate the subarrays running sum. In two loops, the outer loop is to fix the starting index of a subarray, and the inner loop is for the ending index of the subarray while iterating the second loop, we will find the sum and store the length of the largest subarray with 0 sum.
Algorithm
- Declare and Initialize a variable max_length , which will store the length of the largest subarray with the sum 0.
- Start traversing the array from starting index and initialize the variable cur_sum which will store the sum of the subarray starting from the ith index.
- Traverse array from next element of current index till the end of th array and at every iteration add the current element to cur_sum.
- Now check if cur_sum , and check if max_length current subarray length, then update the value of max_length with current subarray length.
- After traversing the whole array return max_length as the result.
Implementation
C++ Implementation :
Output :
Java Implementation :
Output :
Python Implementation :
Output :
Time Complexity
As we are running two nested loops, one loop for starting index of the subarray, the second loop for the ending index of the subarray, and while running the second loop, we are also calculating the running sum of the subarray.
We know that worst case time complexity of running two nested loops is .
So worst case time complexity of this approach is .
Space Complexity
We are not using the extra space. So worst case space complexity of this approach is .
Approach 3 : Alternative and Efficient Approach - Using the Hash Table
Approach
In this approach to the largest subarray with the 0 sum problem, we are using hashing for finding the length of the largest subarray. The idea of this approach is to iterate over the input array and calculate the sum of elements from starting element to the current element. And check if the current sum is present in the map, then there is a subarray with a 0 sum.
Algorithm
- Initialize max_length , cur_sum and create an empty map for storing the previous sum-index as a key-value pair.
- Iterate over the input array.
- At every index update sum by adding current element cur_sum cur_sum .
- For every index, check if the cur_sum is on the map or not.
- If the cur_sum is present in the map, then update the max_length to a maximum max_length and the difference between two indices (current index and index in the hash-map).
- Otherwise, put the cur_sum as a key in the map with the index as a value.
- Return the maximum length (max_length).
Implementation
C++ Implementation :
Ouput :
Java Implementation :
Ouput :
Python Implementation :
Ouput :
Time Complexity
As we are iterating over an input array of size n once whose time complexity and while iterating, we are searching/inserting an element in the map whose time complexity is . So total time complexity of this approach is .
So worst case time complexity of this approach is .
Space Complexity
As we are using a hashmap for storing the previous sums. So worst case space complexity of this approach is .
Comparison of Different Solutions
Comparison of time and space complexity of all approaches of solution of largest subarray with 0 sum problem is given below :
Approach | Time Complexity | Space Complexity |
---|---|---|
Brute Force Approach - using three nested loops | ||
Efficient Brute Force - using two nested loops | ||
Alternative and Efficient Approach - using hash table |
Conclusion
- In the largest subarray with the 0 sum problem, we need to find the size of the longest subarray among all the subarrays, which has a sum of 0.
- Subarray is the contiguous sequence of the elements of the input array.
- In the largest subarray with the 0 sum problem, we are given the size of the array and the elements of an array as an input and we are required to return the length of the largest subarray with sum 0.
- Brute Force Approach to this problem finds the length of the largest subarray using three loops and time its time complexity is .
- Efficient Brute Force Approach to this problem finds the length of the largest subarray using two loops and time its time complexity is .
- With the help of hashing, the length of the largest subarray with a 0 sum problem's solution is possible in time and space.