trauma_vai

 avatar
bruteCoder
java
2 months ago
623 B
0
Indexable
Never
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;
    }
 
}
Leave a Comment