Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Math
Google
Continuous Subarray Sum

Determine whether an array contains a subarray of length at least 2 whose sum is divisible by a given integer.

Acceptance 31%
Problem Statement

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 array
  • k: an integer divisor

Output Format

Return a boolean indicating whether a qualifying contiguous subarray exists.

Constraints

  • The subarray must contain at least 2 elements.
  • nums may contain positive, negative, or zero values.
  • k is non-zero.
Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.