Skip to main content

Contiguous Subarray Sum

Hard

Determine if a given array contains a subarray of at least two elements whose sum is a multiple of a specified number k.

An array is considered to have a "good subarray" if there exists at least one subarray (consisting of two or more elements) such that the sum of the elements in this subarray is a multiple of k.

For example, the array [23, 2, 4, 7] with k = 6 has a "good subarray" ([2, 4]), as the sum 6 is a multiple of k = 6. The array [5, 0, 0, 0] with k = 3 also has a "good subarray" ([0, 0]), since the sum is 0, and 0 is a multiple of 3.

Examples

nums = [23, 2, 4, 7], k = 6 output: true nums = [5, 0, 0, 0], k = 3 output: true nums = null, k = 1 output: false

In these examples, the output is true if there exists a "good subarray" in the given array nums for the specified k, and false otherwise.

Can you come up with a solution with a time complexity of O(n)?

Can you come up with a solution with a space complexity of O(n)?