Divisible and Non-divisible Sums Difference (LeetCode 2894): Solution and Explanation
Given positive integers n and m, let num1 be the sum of the numbers in [1, n] not divisible by m and num2 the sum of those that are. Return num1 - num2.
Intuition
Write the numbers 1 to n in rows of m. Every row ends on a multiple of m, so all the multiples line up in the last column and everything else sits to their left. num2 is that one column; num1 is the rest of the grid.
Summing the whole grid is the classic triangle number: 1 + 2 + ... + n = n(n + 1) / 2.
The last column reads m, 2m, 3m, ..., km where k = n // m is the number of complete rows. Factor out m and you get m * (1 + 2 + ... + k) = m * k(k + 1) / 2. That is num2.
Whatever is not in the column is num1, so num1 = total - num2, and the answer num1 - num2 equals total - 2 * num2. No loop is needed.
Approach: Two arithmetic series
- Compute
total = n * (n + 1) / 2. - Count the multiples:
k = n // m. - Sum them:
num2 = m * k * (k + 1) / 2. - The rest:
num1 = total - num2. - Return
num1 - num2.
Step-by-step example
n = 10, m = 3
- Rows of three: [1 2 3], [4 5 6], [7 8 9], [10]. The last column holds 3, 6, 9.
- total = 10 * 11 / 2 = 55.
- k = 10 // 3 = 3 multiples.
- num2 = 3 * 3 * 4 / 2 = 18, which is 3 + 6 + 9.
- num1 = 55 - 18 = 37.
- Return 37 - 18 = 19.
Code: Python, Java, C++
1class Solution:
2 def differenceOfSums(self, n: int, m: int) -> int:
3 total = n * (n + 1) // 2
4 k = n // m
5 num2 = m * k * (k + 1) // 2
6 num1 = total - num2
7 return num1 - num21class Solution {
2 public int differenceOfSums(int n, int m) {
3 int total = n * (n + 1) / 2;
4 int k = n / m;
5 int num2 = m * k * (k + 1) / 2;
6 int num1 = total - num2;
7 return num1 - num2;
8 }
9}1class Solution {
2public:
3 int differenceOfSums(int n, int m) {
4 int total = n * (n + 1) / 2;
5 int k = n / m;
6 int num2 = m * k * (k + 1) / 2;
7 int num1 = total - num2;
8 return num1 - num2;
9 }
10};Time and space complexity
O(1)
A fixed handful of multiplications and divisions, independent of n and m.
O(1)
Only a few integer variables.
Other approaches
Simple loop (time O(n), space O(1))
Walk i from 1 to n, adding i when i % m != 0 and subtracting it otherwise. With n at most 1000 this is perfectly fast and is the most direct reading of the statement.
Step through multiples only (time O(n / m), space O(1))
Compute the total with the formula, then loop j = m, 2m, ... up to n to build num2. It does n / m iterations instead of n.
Common mistakes
- Forgetting that m can be larger than n. Then k = 0, num2 = 0 and the answer is just the total.
- Missing that m = 1 makes every number divisible, so the answer is the negative total.
- Using floating point division in JavaScript for
n / mwithoutMath.floor, which gives a fractional k.
Frequently asked questions
Why is the answer total - 2 * num2?
num1 is everything except the multiples, so num1 = total - num2. Subtracting num2 once more gives num1 - num2 = total - 2 * num2.
Can the products overflow a 32-bit integer?
No. With n at most 1000 the total is at most 500500, and m * k * (k + 1) is at most about one million, both far below the 32-bit limit.
Is the loop solution acceptable in an interview?
Yes for these limits, but mentioning the closed form shows you recognise the arithmetic series, which is usually the point of the question.