Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- public class Main
- {
- public static void solve(int a[],int n)
- {
- HashMap<Integer,Integer> map=new HashMap<>();
- int sum=0;
- int ans=0;
- for(int i=0;i<n;i++)
- {
- sum+=a[i];
- if(sum==0)
- ans=i+1;
- if(map.containsKey(sum))
- ans=Math.max(ans,i-map.get(sum));
- else
- map.put(sum,i);
- }
- System.out.println(ans);
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- //your code here
- Scanner sc=new Scanner(System.in);
- int n=sc.nextInt();
- int a[]=new int[n];
- for(int i=0;i<n;i++)
- a[i]=sc.nextInt();
- solve(a,n);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement