Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Subarray Sums Divisible by K (medium)
On this page

Problem Statement

Given an array of integers nums and an integer k, return the count of non-empty subarrays that have a sum that is divisible by k.

A subarray is a continuous part of an array.

Examples

Example 1

  • Input: nums = [3, 1, 2, -2, 5, -1], k = 3
  • Expected Output: 7
  • Justification: The subarrays that sum to a multiple of 3 are [3], [1, 2], [3, 1, 2], [3, 1, 2, -2, 5], [1, 2, -2, 5], [-2, 5], and [2, -2].

Example 2

  • Input: nums = [4, 5, 0, -2, -3, 1], k = 5
  • Expected Output: 7

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page