Longest Subarray Zero Sum

Given an array arr of length N consisting of positive and negative integers, return the length of the longest subarray whose sum is zero.

Input Format:
The first line of input contains an integer N, the length of the array.

The second line of input contains N integers, the elements of the array.
Output Format:
The single line contains an integer, the length of the longest subarray whose sum is zero.
Note:
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
0 <= N <= 10^6    
-10^9 <= arr[i] <= 10^9

Time Limit: 1sec
CodingNinjas
author
2y
Brute Force

The brute force approach will be to calculate the sum for all possible subarrays. To calculate this, for each element(ith, where 0 <= i < N) in the array fix it as the first end of the suba...read more

CodingNinjas
author
2y
Using HashMap

In this approach, we will use a hashmap to store the prefix sums as keys and the value as the lowest index till which the prefix sum occurred. If any prefix sum occurs again in the array(...read more

Help your peers!
Add answer anonymously...
NatWest Group Software Developer Intern 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
Get AmbitionBox app

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