fork download
  1. class Solution {
  2. public int cntSubarrays(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. HashMap<Integer,Integer>f = new HashMap<>();
  13. f.put(0,1); //ye to hoga hi
  14.  
  15. int count=0;
  16. for(int i=0;i<n;i++){
  17. if(f.containsKey(prefixsum[i]-k)){
  18. count+=f.getOrDefault(prefixsum[i]-k,0);
  19. }
  20.  
  21. f.put(prefixsum[i],f.getOrDefault(prefixsum[i],0)+1);
  22. }
  23.  
  24. return count;
  25.  
  26. }
  27. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:12: error: cannot find symbol
        HashMap<Integer,Integer>f = new HashMap<>();
        ^
  symbol:   class HashMap
  location: class Solution
Main.java:12: error: cannot find symbol
        HashMap<Integer,Integer>f = new HashMap<>();
                                        ^
  symbol:   class HashMap
  location: class Solution
2 errors
stdout
Standard output is empty