K-Subarrays

  • + 0 comments
    class Result {
    
    /*
     * Complete the 'kSub' function below.
     *
     * The function is expected to return a LONG_INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER k
     *  2. INTEGER_ARRAY nums
     */
    
    public static long kSub(int k, List<Integer> nums) {
        long count =0;
        long[] mod = new long[k];
        mod[0]=1;
        long sum = 0;
    
        for(int i = 0;i<nums.size();i++){
            sum+=nums.get(i);
            int rem =(int) sum%k;
            count+=mod[rem];
            mod[rem]++;
    
        }
        return count;
    
        }
                }
    
    }
    

    }