|
| 1 | +--- |
| 2 | +id: subarray-sums-divisible-by-k |
| 3 | +title: Subarray Sums Divisible by K |
| 4 | +sidebar_label: 974. Subarray Sums Divisible by K |
| 5 | + |
| 6 | +tags: |
| 7 | +- Array |
| 8 | +- Sliding Window |
| 9 | +- Hashmap |
| 10 | + |
| 11 | +description: "This is a solution to theSubarray Sums Divisible by K problem on LeetCode." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | +Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k. |
| 16 | + |
| 17 | +A subarray is a contiguous part of an array. |
| 18 | +### Examples |
| 19 | + |
| 20 | +**Example 1:** |
| 21 | +``` |
| 22 | +Input: nums = [4,5,0,-2,-3,1], k = 5 |
| 23 | +Output: 7 |
| 24 | +Explanation: There are 7 subarrays with a sum divisible by k = 5: |
| 25 | +[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3] |
| 26 | +``` |
| 27 | + |
| 28 | +**Example 2:** |
| 29 | +``` |
| 30 | +Input: nums = [5], k = 9 |
| 31 | +Output: 0 |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +### Constraints |
| 36 | +- `1 <= nums.length <= 3 * 10^4` |
| 37 | +- `-10^4 <= nums[i] <= 10^4` |
| 38 | +- `2 <= k <= 10^4` |
| 39 | + |
| 40 | +## Solution for Subarray Sums Divisible by K Problem |
| 41 | +### Approach |
| 42 | +#### Brute Force |
| 43 | +- Generate All the Subarray and Check whether the which subarray sum is divisible by k. |
| 44 | + |
| 45 | +#### Optimized Approach |
| 46 | +- The problem is to find the number of subarrays whose sum is divisible by a given integer k. The key insight is to use the concept of prefix sums and remainders: |
| 47 | + |
| 48 | +##### Prefix Sum and Remainders: |
| 49 | +- Compute the prefix sum as you iterate through the array.Compute the remainder of the prefix sum divided by k. This remainder helps in identifying subarrays that are divisible by |
| 50 | +k. |
| 51 | + |
| 52 | +##### Handling Negative Remainders: |
| 53 | +- If the remainder is negative, adjust it by adding k to make it positive. This ensures consistent handling of remainders. |
| 54 | +##### Using a HashMap: |
| 55 | + |
| 56 | +- Use a hash map (or dictionary) to count the frequency of each remainder. |
| 57 | +- If the same remainder has been seen before, it means there are subarrays whose sum is divisible by k. |
| 58 | +##### Counting Valid Subarrays: |
| 59 | + |
| 60 | +- For each prefix sum remainder, the count of valid subarrays is incremented by the frequency of the same remainder seen so far. |
| 61 | + |
| 62 | +<Tabs> |
| 63 | + <TabItem value="Solution" label="Solution"> |
| 64 | + |
| 65 | + #### Implementation |
| 66 | + ```jsx live |
| 67 | + function Solution(arr) { |
| 68 | + var subarraysDivByK = function(nums, k) { |
| 69 | + let sum = 0; |
| 70 | + const mp = new Map(); |
| 71 | + mp.set(0, 1); |
| 72 | + let cnt = 0; |
| 73 | + |
| 74 | + for (let i = 0; i < nums.length; i++) { |
| 75 | + sum += nums[i]; |
| 76 | + sum = sum % k; |
| 77 | + if (sum < 0) { |
| 78 | + sum += k; |
| 79 | + } |
| 80 | + if (mp.has(sum)) { |
| 81 | + cnt += mp.get(sum); |
| 82 | + } |
| 83 | + mp.set(sum, (mp.get(sum) || 0) + 1); |
| 84 | + } |
| 85 | + |
| 86 | + return cnt; |
| 87 | + }; |
| 88 | + const input =[4,5,0,-2,-3,1] |
| 89 | + const k = 5 |
| 90 | + const output = subarraysDivByK(input , k) |
| 91 | + return ( |
| 92 | + <div> |
| 93 | + <p> |
| 94 | + <b>Input: </b> |
| 95 | + {JSON.stringify(input)} |
| 96 | + </p> |
| 97 | + <p> |
| 98 | + <b>Output:</b> {output.toString()} |
| 99 | + </p> |
| 100 | + </div> |
| 101 | + ); |
| 102 | + } |
| 103 | + ``` |
| 104 | + |
| 105 | + #### Complexity Analysis |
| 106 | + |
| 107 | + - Time Complexity: $ O(n) $ |
| 108 | + - Space Complexity: $ O(k)$ |
| 109 | + |
| 110 | + ## Code in Different Languages |
| 111 | + <Tabs> |
| 112 | + <TabItem value="JavaScript" label="JavaScript"> |
| 113 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 114 | + ```javascript |
| 115 | + var subarraysDivByK = function(nums, k) { |
| 116 | + let sum = 0; |
| 117 | + const mp = new Map(); |
| 118 | + mp.set(0, 1); |
| 119 | + let cnt = 0; |
| 120 | + |
| 121 | + for (let i = 0; i < nums.length; i++) { |
| 122 | + sum += nums[i]; |
| 123 | + sum = sum % k; |
| 124 | + if (sum < 0) { |
| 125 | + sum += k; |
| 126 | + } |
| 127 | + if (mp.has(sum)) { |
| 128 | + cnt += mp.get(sum); |
| 129 | + } |
| 130 | + mp.set(sum, (mp.get(sum) || 0) + 1); |
| 131 | + } |
| 132 | + |
| 133 | + return cnt; |
| 134 | +}; |
| 135 | + ``` |
| 136 | + |
| 137 | + </TabItem> |
| 138 | + <TabItem value="TypeScript" label="TypeScript"> |
| 139 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 140 | + ```typescript |
| 141 | + function subarraysDivByK(nums: number[], k: number): number { |
| 142 | + let sum = 0; |
| 143 | + const mp: Map<number, number> = new Map(); |
| 144 | + mp.set(0, 1); |
| 145 | + let cnt = 0; |
| 146 | + |
| 147 | + for (let i = 0; i < nums.length; i++) { |
| 148 | + sum += nums[i]; |
| 149 | + sum = sum % k; |
| 150 | + if (sum < 0) { |
| 151 | + sum += k; |
| 152 | + } |
| 153 | + if (mp.has(sum)) { |
| 154 | + cnt += mp.get(sum)!; |
| 155 | + } |
| 156 | + mp.set(sum, (mp.get(sum) || 0) + 1); |
| 157 | + } |
| 158 | + |
| 159 | + return cnt; |
| 160 | +} |
| 161 | +
|
| 162 | + ``` |
| 163 | + </TabItem> |
| 164 | + <TabItem value="Python" label="Python"> |
| 165 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 166 | + ```python |
| 167 | + from collections import defaultdict |
| 168 | +
|
| 169 | +class Solution: |
| 170 | + def subarraysDivByK(self, nums, k): |
| 171 | + sum = 0 |
| 172 | + mp = defaultdict(int) |
| 173 | + mp[0] = 1 |
| 174 | + cnt = 0 |
| 175 | + |
| 176 | + for num in nums: |
| 177 | + sum += num |
| 178 | + sum = sum % k |
| 179 | + if sum < 0: |
| 180 | + sum += k |
| 181 | + cnt += mp[sum] |
| 182 | + mp[sum] += 1 |
| 183 | + |
| 184 | + return cnt |
| 185 | +
|
| 186 | + ``` |
| 187 | + |
| 188 | + </TabItem> |
| 189 | + <TabItem value="Java" label="Java"> |
| 190 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 191 | + ```java |
| 192 | + import java.util.HashMap; |
| 193 | +import java.util.Map; |
| 194 | +
|
| 195 | +class Solution { |
| 196 | + public int subarraysDivByK(int[] nums, int k) { |
| 197 | + int sum = 0; |
| 198 | + Map<Integer, Integer> mp = new HashMap<>(); |
| 199 | + mp.put(0, 1); |
| 200 | + int cnt = 0; |
| 201 | + |
| 202 | + for (int num : nums) { |
| 203 | + sum += num; |
| 204 | + sum = sum % k; |
| 205 | + if (sum < 0) { |
| 206 | + sum += k; |
| 207 | + } |
| 208 | + cnt += mp.getOrDefault(sum, 0); |
| 209 | + mp.put(sum, mp.getOrDefault(sum, 0) + 1); |
| 210 | + } |
| 211 | + |
| 212 | + return cnt; |
| 213 | + } |
| 214 | +} |
| 215 | +
|
| 216 | + ``` |
| 217 | + |
| 218 | + </TabItem> |
| 219 | + <TabItem value="C++" label="C++"> |
| 220 | + <SolutionAuthor name="@hiteshgahanolia"/> |
| 221 | + ```cpp |
| 222 | + class Solution { |
| 223 | +public: |
| 224 | + int subarraysDivByK(vector<int>& nums, int k) { |
| 225 | + int sum=0; |
| 226 | + map<int,int>mp; |
| 227 | + mp[0]=1; |
| 228 | + int cnt=0; |
| 229 | + for(int i=0;i<nums.size();i++) |
| 230 | + { |
| 231 | + sum+=nums[i]; |
| 232 | + sum=sum%k; |
| 233 | + if(sum<0) |
| 234 | + { |
| 235 | + sum+=k; |
| 236 | + } |
| 237 | + cnt+=mp[sum]; |
| 238 | + mp[sum]++; |
| 239 | + } |
| 240 | + return cnt; |
| 241 | + } |
| 242 | +}; |
| 243 | +
|
| 244 | +``` |
| 245 | +</TabItem> |
| 246 | +</Tabs> |
| 247 | + |
| 248 | + </TabItem> |
| 249 | +</Tabs> |
| 250 | + |
| 251 | +## References |
| 252 | + |
| 253 | +- **LeetCode Problem**: [Subarray Sums Divisible by K](https://leetcode.com/problems/subarray-product-less-than-k/description/) |
| 254 | + |
| 255 | +- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/subarray-sums-divisible-by-k/solutions) |
| 256 | + |
0 commit comments