Longest Subarray with Zero Sum

Ninja enjoys working with numbers, and as a birthday challenge, his friend provides him with an array consisting of both positive and negative integers. Ninja is curious to identify the length of the longest subarray within this array that sums to zero.

Input:

The first line contains a single integer T, representing the number of test cases.
The first line of each test case consists of the integer N, which represents the number of elements in the array.
The second line of each test case contains N space-separated integers representing the array's elements.

Output:

A single line for each test case, indicating the integer value of the length of the longest subarray with a sum of zero.

Example:

If T = 2, and the arrays are arr1 = [1, -1, 3, 2, -2, -3, 3] and arr2 = [1, 2, -3, 3, -2, -1, 2], the outputs should be 4 and 5 respectively.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^4
  • -10^5 <= arr[i] <= 10^5
  • Time Limit: 1 sec
Durga Prakash
1y

I solved this problem using hashmaps because of their O(1) search operation. I applied 2 nested loops and then used the hashmap to search for the third element to make the sum zero. Time Complexity of...read more

Help your peers!
Add answer anonymously...
Bharti Airtel Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter