523. Continuous Subarray Sum
Difficulty: Medium
Topics: Array, Hash Table, Math, Prefix Sum
Given an integer array nums and an integer k, return true if nums has a good subarray or false otherwise.
A good subarray is a subarray where:
- its length is at least two, and
- the sum of the elements of the subarray is a multiple of
k.
Note that:
- A subarray is a contiguous part of the array.
- An integer
xis a multiple ofkif there exists an integernsuch thatx = n * k.0is always a multiple ofk.
Example 1:
- Input: nums = [23,2,4,6,7], k = 6
- Output: true
- Explanation: [2, 4] is a continuous subarray of size 2 whose elements sum up to 6.
Example 2:
- Input: nums = [23,2,6,4,7], k = 6
- Output: true
-
Explanation: [23, 2, 6, 4, 7] is a continuous subarray of size 5 whose elements sum up to 42.
- 42 is a multiple of 6 because 42 = 7 * 6 and 7 is an integer.
Example 3:
- Input: nums = [23,2,6,4,7], k = 13
- Output: false
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 1090 <= sum(nums[i]) <= 231 - 11 <= k <= 231 - 1
Solution:
We need to check whether there is a subarray of at least two elements whose sum is a multiple of k.
Key Observations:
Modulo Property:
The sum of a subarray can be reduced to the remainder (modulok). Specifically, for any two indicesiandj(wherei < j), if the prefix sumsprefix_sum[j] % k == prefix_sum[i] % k, then the sum of the subarray betweeni+1andjis a multiple ofk. This is because the difference between these prefix sums is divisible byk.HashMap for Prefix Modulo:
We'll store the modulo values of prefix sums in a hash table (or associative array). If the same modulo value repeats at a later index, it means the subarray sum between these indices is divisible byk.-
Handling Edge Cases:
- If
k == 0, we simply need to check if any subarray has a sum of0. - If the subarray length is less than 2, we ignore it.
- If
Solution Strategy:
- Initialize a hashmap (associative array) to store the modulo of the prefix sum.
- Traverse the array and calculate the cumulative sum. For each element, compute the modulo with
k. - If the same modulo value has already been seen and the subarray length is at least 2, return
true. - Store the current modulo and its index in the hashmap if not already present.
Let's implement this solution in PHP: 523. Continuous Subarray Sum
<?php
/**
* @param Integer[] $nums
* @param Integer $k
* @return Boolean
*/
function checkSubarraySum($nums, $k) {
...
...
...
/**
* go to ./solution.php
*/
}
// Example 1
$nums = [23, 2, 4, 6, 7];
$k = 6;
echo checkSubarraySum($nums, $k) ? 'true' : 'false'; // Output: true
// Example 2
$nums = [23, 2, 6, 4, 7];
$k = 6;
echo checkSubarraySum($nums, $k) ? 'true' : 'false'; // Output: true
// Example 3
$nums = [23, 2, 6, 4, 7];
$k = 13;
echo checkSubarraySum($nums, $k) ? 'true' : 'false'; // Output: false
?>
Explanation:
mod_mapInitialization:
We initialize themod_mapwith a key0and value-1. This is used to handle cases where a subarray from the start of the array is divisible byk.Iterating through
nums:
We calculate the cumulative sum as we iterate through the array. For each element, we compute the sum modulok.Modulo Check:
If the current modulo value has already been seen in themod_map, it means there is a subarray whose sum is divisible byk. We also ensure the subarray length is greater than or equal to 2 by checking if the difference in indices is more than 1.-
Return Result:
- If a valid subarray is found, we return
true. - If we finish iterating through the array without finding such a subarray, we return
false.
- If a valid subarray is found, we return
Time Complexity:
-
Time Complexity: O(n), where
nis the length of the array. We traverse the array once. -
Space Complexity: O(min(n, k)), since we store at most
kunique remainders in the hashmap.
This solution is efficient and works within the problem's constraints.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!
If you want more helpful content like this, feel free to follow me:
Top comments (0)