fork download
  1. class Solution {
  2. public int longestSubarray(int[] arr, int k) {
  3. int n=arr.length;
  4.  
  5. int[] prefixsum = new int[n];
  6. prefixsum[0]=arr[0];
  7.  
  8. for(int i=1;i<n;i++){
  9. prefixsum[i]=prefixsum[i-1]+arr[i];
  10. }
  11.  
  12. int len = Integer.MIN_VALUE;
  13.  
  14. HashMap<Integer,Integer>f = new HashMap<>();
  15. f.put(0,-1); //ye to hoga hi
  16.  
  17. for(int i=0;i<n;i++){
  18. if(f.containsKey(prefixsum[i]-k)){
  19. int ll=f.get(prefixsum[i]-k);
  20. int r=i;
  21.  
  22. int l=ll+1;
  23. int lenn =r-l+1;
  24. len=Math.max(len,lenn);
  25.  
  26. }
  27.  
  28. if(!f.containsKey(prefixsum[i])){
  29. f.put(prefixsum[i],i);
  30. }
  31.  
  32.  
  33.  
  34. }
  35.  
  36. if(len!= Integer.MIN_VALUE){
  37. return len;
  38. }
  39. else{
  40. return 0;
  41. }
  42.  
  43. }
  44. }
  45.  
  46.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:14: error: cannot find symbol
        HashMap<Integer,Integer>f = new HashMap<>();
        ^
  symbol:   class HashMap
  location: class Solution
Main.java:14: error: cannot find symbol
        HashMap<Integer,Integer>f = new HashMap<>();
                                        ^
  symbol:   class HashMap
  location: class Solution
2 errors
stdout
Standard output is empty