FastPrepContinuous Subarray Sum Multiple

Continuous Subarray Sum Multiple

Meta logoMeta● MediumINTERNPHONE SCREEN
Learn

Problem statement

Given an integer array nums and a positive integer k, return whether some contiguous subarray has at least two elements and a sum that is a multiple of k.

A multiple is an integer times k. Zero is a multiple of every positive k.

Function

checkSubarraySum(nums: int[], k: int) → boolean

Examples

Example 1

nums = [23,2,4,6,7]k = 6return = true

The adjacent values2 and4 sum to6.

Example 2

nums = [23,2,6,4,7]k = 6return = true

The whole array sums to42, a multiple of6.

Example 3

nums = [23,2,6,4,7]k = 13return = false

No subarray of length at least two has a sum divisible by13.

Constraints

  • 1 <= nums.length <= 100000.
  • 0 <= nums[i] <= 1000000000.
  • 0 <= sum(nums) <= 2147483647.
  • 1 <= k <= 2147483647.

More Meta problems

See Meta hiring insights
public boolean checkSubarraySum(int[] nums, int k) {
    // Write your code here
}
nums[23,2,4,6,7]
k6
expectedtrue
Checking account…