The first line contains a single integer T, denoting the number of test cases.
The first line of each test case will contain the integer N, denoting the number of elements in the given array.
The second and last line contains N space-separated integers that denote the value of the elements of the array.
The first and only line of each test case in the output contains an integer denoting the length of the longest subarray whose sum is zero.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^4
-10^5 <= arr[i] <= 10^5
Time Limit: 1 sec
We will create the various sub-arrays possible from the given array. We will then eliminate those sub-arrays whose sum is not zero. Out of the remaining sub-arrays, we check the length of each sub-array and see which has the largest length. We will return the largest length as our final answer.
The steps are as follows:
We can maintain a prefix sum for each index. We can check if standing at a particular index sum is zero. If it is zero, then we store the index value as the length of the sub-array. Then we hold the sum values in a hash map. If the present sum is previously present in the hash map, then it means starting from the previous index till the current index, we get another sub-array whose sum is zero. If the length of this sub-array is greater than the previously stored length, then we modify our answer with this value.
The steps are as follows:
Longest Subarray With Zero Sum
Merge Two Sorted Arrays Without Extra Space
Ninja And The Strictly Increasing Array
Maximum GCD
Negative To The End