Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Continuous Subarray Sum

XPChallenge Points: 20
levelLevel: Medium

You are given an integer array nums and an integer k.
Your task is to determine whether there exists a continuous subarray of at least two elements such that the sum of the subarray is a multiple of k.

In other words, find if there exists a subarray [i, j] (where j - i >= 1) such that:

sum(nums[i] + nums[i+1] + ... + nums[j]) % k == 0

Notes:

  • A subarray must consist of contiguous elements in the array.

  • Any number is a multiple of k if it can be expressed as n * k for some integer n.

  • Zero (0) is always considered a multiple of k.

Example 1:

Input: nums = [23, 2, 4, 6, 7], k = 6

Output: true

Explanation:

Subarray [2, 4] has a sum of 6, which is a multiple of 6.

Example 2:

Input: nums = [23, 2, 6, 4, 7], k = 6

Output: true

Explanation:

The entire array sums to 42, and 42 % 6 == 0.

Example 3:

Input: nums = [23, 2, 6, 4, 7], k = 13

Output: false

Explanation:

No subarray has a sum that is a multiple of 13.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result