Programming is a complex concept, and even the professional level programmers will agree that not everyone can compile codes that a programmer can easily understand!

Which is why we have the concept of arrays and Subarrays. These data structures are used to effectively store elements of data so that we would not have to manage every complicated task on our own.

When we encounter a zero sum subarrays problem, this is where the main issue with arrays arises! 

These subarrays are stored in contiguous locations within the array and they take up extra space which is why they are required to be eliminated.

This blog mentions approaches that can effectively solve the problem of finding and eliminating the subarrays with zero sum!

 

What is a Zero Sum Subarray?

Similar to an array, a Subarray can be defined as structures consisting of elements for the input of an array. 

In the context of a zero sum Subarray problem, we take the largest Subarray containing the 0 sum problem and we are given the array and an input.

The main task of a zero sum Subarray problem is to figure out the size of the largest Subarray that contains a sum 0.

In the following section, we will consider a Zero Sum Subarray Problem, and discuss the different approaches that can be applied for solving this programming problem.

 

Problem Discussion on Zero Sum Subarrays

Check out the following problem statement and solve the Zero Sum Subarray question.

Problem Statement:

You have been provided an array consisting of integers. Within the array we have a number of a

Subarrays whose sum equals to 0. 

The objective of the problem is to identify the subarrays consisting of sum 0 within the given array. 

Following is the array that we are referring to in the problem:

0 1 2 3 4 5 6 7

←——————>

             x

 

Answer Key:

We will now consider the basic premise of the problem statement and discuss various approaches that can be applied for solving the zero sum Subarray based problem.

 

Approach 1: Brute Force Algorithm

In this approach, we will consider using three nested loops. 

  • Starting off, we will take every Subarray into consideration and figure out the summation of all the subarrays and also check the summation of all the subarrays that we have considered so far.
  • Next up, we will also start storing the considerably smaller Subarrays into the longest Subarray that we can find. The smaller Subarrays must have a summation of 0.
  • Finally, the value of the longest Subarray will be returned as a result!

 

Here is the algorithm for performing all the functions mentioned above:

  • Start by declaring and initiating variables of the length max_length = 0. This variable will be used for storing the largest Subarray with value 0.
  • You can now start traversing the array from the beginning till the end.
  • For each of the indexes that are present (consider the index as i), you can initiate a nested loop containing the variable j. The variable must range from index i to the entirety of the array.
  • Next we will initiate another variable known as cursum=0 to store the value of the Subarray that begins from the ith index and ends at the jth index.
  • Also, we will further run a loop based on the sum of the Subarray that starts from the ith index and ends on the jth index in the cursum variable.
  • We are almost there! Now, we have to check whether the value of the cursum represents 0. If found that the value of the Subarray is greater than the value of the cursum then we have to update the max_length.
  • Finally, we shall return the max_length in the program.

Here are the time and space complexities that we will observe when completing this program using the Brute Force Algorithm.

 

Time Complexity:

Since we have used the three nested loops, each for the starting index within the Subarray, the second index within the Subarray and for finding the sum of the current Subarray, the worst time complexity for this program can be represented as follows:

O(n3)

 

Space Complexity: 

For this particular program, we will not be using extra space meaning that the space complexity will only be represented by: O(1).

 

Approach 2: Effective Brute Force Algorithm

This is a similar version to the Brute Force Algorithm with the only difference that instead of using three nested loops, we shall now be using two nested loops. 

  • This approach is more of an optimised version of the previous approach that we used to solve the problem. In this approach we will be employing two nested loops for determining the running sum of the subarrays.
  • Here, the first loop will be used for determining the beginning index of the Subarray, while the inner loop will be determining the last index of the Subarrays while it iterates on the second loop.
  • We will also find the length of the largest Subarray by finding out the sum and store the value of the largest Subarray that has a 0 sum.

 

Here’s the algorithm for performing the above mentioned functions:

  • Firstly, start off with initiating the maximum length of the variable i.e max_length=0. This variable will be used for storing the entire length of the largest Subarray consisting of a sum 0.
  • Also initialise cur_sum=0 variable and start traversing the given array from its first index. The initiated variable will be used for storing the sum of the given Subarrays within the ith index.
  • Continue this process and make sure to keep storing the iterated value of the current element to the variable cur_sum. You can traverse the array from the next element to the first index.
  • As the second last step, you will have to check whether the sum of the initiated variable i.e cur_sum is equal to 0 or not. If the value is found to be greater than the max_length, you need to update the value.
  • After you have completed traversing the entire values, you can finally return the max_length for the results.

 

The time and space complexities for this approach will be as follows:

Time Complexity:

O(n2)

Space Complexity:

O(n2)

 

Wrapping Up

The brute force algorithm is not only effective for solving the zero sum subarray problems but, they can also be effectively used for figuring out the minimum cost to make left and right subtree values equal.

This is because the algorithm uses an in-order traversal method that initiates the process of traversing each and every element till the data structure is thoroughly checked.