trauma_vai

 avatar
bruteCoder
java
a year ago
623 B
3
Indexable
class Solution{
    int longSubarrWthSumDivByK(int a[], int n, int k)
    {
        // Complete the function
       HashMap<Integer,Integer> map = new HashMap<>();
       int preFix = 0;
       int out = 0;
       for(int i = 0 ;i<n;i++){
           preFix += a[i];
           int rem = preFix%k;
           if(rem < 0) rem += k;
           if(rem == 0){
               out = i+1;
           }else {
               if(map.containsKey(rem)) {
                   out = Math.max(out,i-map.get(rem));
               }else {
                   map.put(rem,i);
               }
           }
       }
       return out;
    }
 
}
Editor is loading...
Leave a Comment