Determine whether an array contains a subarray of length at least 2 whose sum is divisible by a given integer.
Given an integer array nums and an integer k, determine whether there exists a contiguous subarray of length at least 2 whose elements sum to a multiple of k.
A sum s is considered a multiple of k if s % k == 0.
Return true if such a subarray exists, otherwise return false.
Input Format
nums: an integer arrayk: an integer divisor
Output Format
Return a boolean indicating whether a qualifying contiguous subarray exists.
Constraints
- The subarray must contain at least 2 elements.
numsmay contain positive, negative, or zero values.kis non-zero.
Example 1
Input
nums = [23,2,4,6,7], k = 6
Output
true
Explanation
The subarray [2, 4] has sum 6, which is divisible by 6.
Example 2
Input
nums = [23,2,6,4,7], k = 13
Output
false
Explanation
No contiguous subarray of length at least 2 has a sum that is a multiple of 13.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.