Continuous Subarray Sum - Problem
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
- 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.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [23,2,4,6,7], k = 6
›
Output:
true
💡 Note:
Subarray [2,4] has sum 6, which is divisible by 6. Length is 2 ≥ 2, so it's a good subarray.
Example 2 — Multiple Elements
$
Input:
nums = [23,2,4,6,6], k = 7
›
Output:
true
💡 Note:
Subarray [23,2,4,6] has sum 35, which is divisible by 7 (35 = 7 × 5). Length is 4 ≥ 2.
Example 3 — No Good Subarray
$
Input:
nums = [23,2,4,6,6], k = 13
›
Output:
false
💡 Note:
No contiguous subarray of length ≥ 2 has a sum divisible by 13.
Constraints
- 1 ≤ nums.length ≤ 105
- 0 ≤ nums[i] ≤ 109
- 1 ≤ k ≤ 231 - 1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code