Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

974. Subarray Sums Divisible by K

Medium55.5% Acceptance
ArrayHash TablePrefix Sum
Asked by:
T
Twilio
ProblemSolutions (12)VideosCompanies (4)Notes

Problem Statement

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

A subarray is a contiguous part of an array.

Example 1:

Input: nums = [4,5,0,-2,-3,1], k = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by k = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]

Example 2:

Input: nums = [5], k = 9
Output: 0

Constraints:

  • 1 <= nums.length <= 3 * 104
  • -104 <= nums[i] <= 104
  • 2 <= k <= 104
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
▲455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A
Amazon
F
Facebook
S
Snapchat

Approach

To solve #974 Subarray Sums Divisible by K, the key idea is to use prefix sums combined with a hash table. Instead of checking every possible subarray (which would be inefficient), we track the cumulative sum while iterating through the array.

For each index, compute the running sum and store the remainder when dividing by k. If two prefix sums have the same remainder when taken modulo k, the subarray between them has a sum divisible by k. A hash map can store the frequency of each remainder encountered so far, allowing us to quickly count how many valid subarrays end at the current index.

This approach efficiently transforms the problem into counting matching remainders while scanning the array once. Compared to brute-force methods that check every subarray, the prefix sum + hash map technique significantly reduces the time complexity while using additional space to track remainder frequencies.

Complexity

ApproachTime ComplexitySpace Complexity
Prefix Sum with Hash MapO(n)O(k)

Video Solution Available

take U forward

View all video solutions

Solutions (12)

Prefix Sums with HashMap

This approach leverages prefix sums and the properties of modular arithmetic to efficiently count subarrays whose sums are divisible by k. By maintaining a hashmap (or dictionary) that keeps track of the frequency of each remainder observed when computing prefix sums modulo k, we can determine how many valid subarrays end at each position in the array.

For any position in the array, if we have the same remainder as a previously computed prefix sum, it indicates that the subarray between the two indices is divisible by k.

Time Complexity: O(n), where n is the number of elements in the array, as we iterate over the array once.
Space Complexity: O(k), where k is the number of different remainders we track in our modCount array.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4int subarraysDivByK(int* nums, int numsSize, int k) {
5

Explanation

In this C implementation, an array is used to store the frequency of each remainder when the cumulative sum is divided by k. We initialize the first element to 1 as a subarray with sum 0 (no elements) is considered valid when dividing by k. We then iterate over the input array, updating the cumulative sum and calculating the current remainder. Each time we find a previously seen remainder in our modCount array, it indicates subarrays ending at the current position are divisible by k.

Brute Force Subarray Sum

The brute force approach involves checking all possible subarrays to find those whose sum is divisible by k. This method is less efficient and should be used primarily for small inputs due to its higher time complexity. We iterate over all subarray starting points and compute the sum for all possible ending positions, counting subarrays that meet the criteria.

Time Complexity: O(n^2), where n is the number of elements in the array (due to double loop).
Space Complexity: O(1), as only integer counters and accumulators are used.

CC++JavaPythonC#JavaScript
1

Video Solutions

Watch expert explanations and walkthroughs

Count Subarray sum Equals K | Brute - Better -Optimal

take U forward
24:09446,120 views

Asked By Companies

4 companies
T
Twilio
A
Amazon
F
Facebook
S
Snapchat

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Two SumEasy
Valid SudokuMedium
Sudoku SolverHard
First Missing PositiveHard
More similar problems

Related Topics

ArrayHash TablePrefix Sum

Problem Stats

Acceptance Rate55.5%
DifficultyMedium
Companies4

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why do we use modulo with prefix sums in this problem?

Using modulo k helps identify when two prefix sums differ by a multiple of k. If two cumulative sums produce the same remainder, their difference is divisible by k, which means the subarray between them satisfies the condition.

Is Subarray Sums Divisible by K asked in FAANG interviews?

Yes, variations of this problem are commonly asked in technical interviews at companies like Amazon, Google, and Meta. It tests understanding of prefix sums, modular arithmetic, and hash map optimization techniques.

What data structure is best for Subarray Sums Divisible by K?

A hash map (or frequency array) is the most useful data structure for this problem. It stores how many times each remainder of prefix sum modulo k has appeared, enabling quick counting of valid subarrays.

What is the optimal approach for Subarray Sums Divisible by K?

The optimal approach uses prefix sums along with a hash map to track remainders of cumulative sums modulo k. If the same remainder appears multiple times, the subarray between those indices has a sum divisible by k. This allows the problem to be solved in linear time.

int
*
modCount
=
(
int
*
)
calloc
(
k
,
sizeof
(
int
)
)
;
6
modCount
[
0
]
=
1
;
7
int
count
=
0
,
cumSum
=
0
;
8
for
(
int
i
=
0
;
i
<
numsSize
;
i
++
)
{
9
cumSum
+=
nums
[
i
]
;
10
int
mod
=
(
(
cumSum
%
k
)
+
k
)
%
k
;
11
count
+=
modCount
[
mod
]
;
12
modCount
[
mod
]
++
;
13
}
14
free
(
modCount
)
;
15
return
count
;
16
}
17
18
int
main
(
)
{
19
int
nums
[
]
=
{
4
,
5
,
0
,
-
2
,
-
3
,
1
}
;
20
int
k
=
5
;
21
printf
(
"Number of subarrays divisible by %d: %d\n"
,
k
,
subarraysDivByK
(
nums
,
sizeof
(
nums
)
/
sizeof
(
int
)
,
k
)
)
;
22
return
0
;
23
}
#
include
<stdio.h>
2
3
int
subarraysDivByK
(
int
*
nums
,
int
numsSize
,
int
k
)
{
4
int
count
=
0
;
5
for
(
int
start
=
0
;
start
<
numsSize
;
start
++
)
{
6
int
sum
=
0
;
7
for
(
int
end
=
start
;
end
<
numsSize
;
end
++
)
{
8
sum
+=
nums
[
end
]
;
9
if
(
sum
%
k
==
0
)
{
10
count
++
;
11
}
12
}
13
}
14
return
count
;
15
}
16
17
int
main
(
)
{
18
int
nums
[
]
=
{
4
,
5
,
0
,
-
2
,
-
3
,
1
}
;
19
int
k
=
5
;
20
printf
(
"Number of subarrays divisible by %d: %d\n"
,
k
,
subarraysDivByK
(
nums
,
sizeof
(
nums
)
/
sizeof
(
int
)
,
k
)
)
;
21
return
0
;
22
}

Explanation

This C implementation follows a nested loop technique. The first loop sets potential starting points for subarrays, and the inner loop accumulates sums for subarrays ending at each subsequent index. Each valid subarray whose sum is zero modulo k is counted. This approach is simple but becomes inefficient as input sizes grow.