AfterAcademy Tech
•
14 Nov 2019

Difficulty: Hard
Asked in: Microsoft
Problem Description: Given an array A[] of n integer elements, find the length of the longest subarray with sum equals to 0.
For Example:
Input: A[] = {15, -2, 0, -8, 3, 7, 10, 23}
Output: 5
Explanation : The largest subarray with 0 sum is -2, 0, -8, 3, 7
Possible follow-up questions to ask the interviewer:-
We are discussing two ways to solve this problem :
A simple solution is to check for all subarrays one by one and see if its sum is equal to zero or not. For this, we generate all (i, j): i <= j pairs and calculate the sum between.
Pseudo-Code
int maxLengthSubarraySumToZero(int A[], int n)
{
int maxLen = 0
for(i = 0 to n-1)
{
for(j = i to n-1)
{
int sum = 0
for(k = i to j)
sum = sum + A[k]
if(sum == 0)
{
if(maxLen < j-i+1)
maxLen = j-i+1
}
}
}
return maxLen
}
Complexity Analysis
Time Complexity: O(n³) (Why?)
Space Complexity: O(1)
Critical ideas to think!
This is the optimized version of the above approach. The idea is to start at all positions in the array and calculate running sums. The outer loop picks the beginning element, the inner loop finds the possible sum from the first element picked by the outer loop and if sum comes out to be zero, it checks the length and updates it with max length.
Pseudo-Code
int maxLengthSubarraySumToZero(int A[], int n)
{
int maxLen = 0
for (i = 0 to n-1)
{
int sum = 0
for (j = i to n-1)
{
sum = sum + A[i]
if(sum == 0)
{
maxLen = max(maxLen, j-i+1)
}
}
}
return maxLen
}
Complexity Analysis
Time Complexity: O(n²) (Why?)
Space Complexity: O(1)
Critical ideas to think!
The idea is to store the sum of all the subarrays starting from the beginning. From that, if any of the values repeat itself, let’s say sum till i index and sum till j index is same, it means that the sum of elements in the subarray A[i+1…..j] is 0. Moreover, if the sum any index i is 0, then the subarray A[0…i] has sum of its elements equal to 0.
Solution Steps
5. Return maxLen.
Pseudo-Code
int maxLengthSubarraySumToZero(int A[], int n)
{
Create a HashTable H
int sum = 0
int maxLen = 0
for(i = 0 to n-1)
{
sum = sum + A[i]
if (sum == 0)
{
if (maxLen < i)
{
maxLen = i+1
}
}
else if(sum in H)
{
if(maxLen < i-H[sum])
{
maxLen = i-H[sum]
}
}
else
H[sum] = i
}
return maxLen
}
Complexity Analysis
Time Complexity: O(n) (Why?)
Space Complexity: O(n), for storing the Hash Table
Critical ideas to think!

Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
You are given an array A[] with n elements. You need to find the maximum sum of a subarray among all subarrays of that array. A subarray of array A[] of length n is a contiguous segment from A[i] through A[j] where 0<= i <= j <= n.

AfterAcademy Tech
Given an array arr[] of size n , write a program to find the largest element in it. To find the largest element we can just traverse the array in one pass and find the largest element by maintaining a max variable.

AfterAcademy Tech
You are given an array arr[] with n elements. Write a program to find the contiguous subarray which has the largest product. It's a typical optimization problem.

AfterAcademy Tech
Given an array arr[ ] of n integers, are there elements x, y, z in arr such that x + y + z = 0? Find all unique triplets in the array which gives the sum of zero. The problem is asked in many interviews and required do be solved in O(n^2) with constant space.
