All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0901_1000.s0974_subarray_sums_divisible_by_k.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g0901_1000.s0974_subarray_sums_divisible_by_k;

// #Medium #Array #Hash_Table #Prefix_Sum #2022_03_31_Time_3_ms_(99.95%)_Space_46.1_MB_(95.83%)

public class Solution {
    public int subarraysDivByK(int[] nums, int k) {
        int[] map = new int[k];
        int ans = 0;
        int sum = 0;
        map[0] = 1;
        for (int num : nums) {
            sum += num;
            int temp = sum % k;
            if (temp < 0) {
                temp += k;
            }
            ans += map[temp]++;
        }
        return ans;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy