Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Build confidence with an intuition-first walkthrough focused on math fundamentals.
You are given positive integers n and m.
Define two integers as follows:
num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m.num2: The sum of all integers in the range [1, n] (both inclusive) that are divisible by m.Return the integer num1 - num2.
Example 1:
Input: n = 10, m = 3 Output: 19 Explanation: In the given example: - Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37. - Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18. We return 37 - 18 = 19 as the answer.
Example 2:
Input: n = 5, m = 6 Output: 15 Explanation: In the given example: - Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15. - Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0. We return 15 - 0 = 15 as the answer.
Example 3:
Input: n = 5, m = 1 Output: -15 Explanation: In the given example: - Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0. - Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15. We return 0 - 15 = -15 as the answer.
Constraints:
1 <= n, m <= 1000Problem summary: You are given positive integers n and m. Define two integers as follows: num1: The sum of all integers in the range [1, n] (both inclusive) that are not divisible by m. num2: The sum of all integers in the range [1, n] (both inclusive) that are divisible by m. Return the integer num1 - num2.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
10 3
5 6
5 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2894: Divisible and Non-divisible Sums Difference
class Solution {
public int differenceOfSums(int n, int m) {
int ans = 0;
for (int i = 1; i <= n; ++i) {
ans += i % m == 0 ? -i : i;
}
return ans;
}
}
// Accepted solution for LeetCode #2894: Divisible and Non-divisible Sums Difference
func differenceOfSums(n int, m int) (ans int) {
for i := 1; i <= n; i++ {
if i%m == 0 {
ans -= i
} else {
ans += i
}
}
return
}
# Accepted solution for LeetCode #2894: Divisible and Non-divisible Sums Difference
class Solution:
def differenceOfSums(self, n: int, m: int) -> int:
return sum(i if i % m else -i for i in range(1, n + 1))
// Accepted solution for LeetCode #2894: Divisible and Non-divisible Sums Difference
impl Solution {
pub fn difference_of_sums(n: i32, m: i32) -> i32 {
let mut ans = 0;
for i in 1..=n {
if i % m != 0 {
ans += i;
} else {
ans -= i;
}
}
ans
}
}
// Accepted solution for LeetCode #2894: Divisible and Non-divisible Sums Difference
function differenceOfSums(n: number, m: number): number {
let ans = 0;
for (let i = 1; i <= n; ++i) {
ans += i % m ? i : -i;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.