Leetcode - subarray-sums-divisible-by-k
Given an array A
of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K
.
Example 1:
Input: A = [4,5,0,-2,-3,1], K = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by K = 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]
Note:
-
1 <= A.length <= 30000
-
-10000 <= A[i] <= 10000
-
2 <= K <= 10000
The solution is similar to this one, using preSum map Leetcode - subarray-sum-equals-k
func subarraysDivByK(A []int, K int) int { preSum := map[int]int{ 0: 1, } count := 0 sum := 0 for _, a := range A { sum = (sum + a)%K if sum < 0 { sum += K } count += preSum[sum] preSum[sum]++ } return count }
# @param {Integer[]} a # @param {Integer} k # @return {Integer} def subarrays_div_by_k(a, k) pre_sum = {} pre_sum[0] = 1 count = 0 sum = 0 a.each do |val| sum = (sum + val)%k if sum < 0 sum += k end pre_sum[sum] = 0 if pre_sum[sum].nil? count += pre_sum[sum] pre_sum[sum] += 1 end return count end
Dec 06, 2021 01:35:53 PM
You have created a shocking and important article for us. Mumbai Escorts Service Escorts in Mumbai Mumbai Escorts Hot Mumbai Escorts Escortsin Mumbai Hot Mumbai Escorts Mumbai Escort Mumbai Escorts Escorts Service in Mumbai Escorts Service in Mumbai Mumbai Escorts Service Hot Mumbai Escorts Mumbai Escort Mumbai Escorts Mumbai Escorts Service Escorts in Mumbai Mumbai Escort Escorts Service in Mumbai Mumbai Escorts Mumbai Escorts Mumbai Escorts Service Escorts in Mumbai Escorts in Mumbai Hot Mumbai Escorts Chennai Escorts Mumbai Escor Russion Escorts Mumbai Independent Mumbai Escorts Escorts in Mumbai Mumbai Escorts Escorts in Mumbai Mumbai Escort Escort in Mumbai Mumbai Escort Service Mumbai Escorts Escorts in Mumbai Mumbai Escort I want to say you that I am taught you in regards to your article and Sharing it with my associates.