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 Overview: Given two integers n and m, compute the difference between the sum of numbers from 1..n that are not divisible by m and the sum of numbers that are divisible by m. The result is sumNonDivisible - sumDivisible.
Approach 1: Iterative Calculation (O(n) time, O(1) space)
Iterate through every number from 1 to n. For each value i, check the divisibility condition using i % m. If the remainder is zero, add i to the divisible sum; otherwise add it to the non‑divisible sum. After the loop finishes, subtract the two totals. This approach mirrors the problem statement directly and is easy to implement in any language. It works well when n is small or when clarity matters more than micro‑optimizations. The algorithm uses constant memory and a single pass through the range.
Approach 2: Formula-based Optimization (O(1) time, O(1) space)
The optimized approach relies on arithmetic series formulas from math. First compute the sum of all numbers from 1..n using total = n * (n + 1) / 2. Next count how many numbers are divisible by m: k = n / m. Those numbers are m, 2m, 3m ... km. Their sum forms another arithmetic series: sumDivisible = m * (k * (k + 1) / 2). The non‑divisible sum is simply total - sumDivisible. The required difference becomes (total - sumDivisible) - sumDivisible, which simplifies to total - 2 * sumDivisible. This eliminates iteration entirely and runs in constant time using simple arithmetic operations, a common trick in number theory style problems.
Recommended for interviews: Start with the iterative approach to show you understand the requirement and the divisibility check. Then optimize using the arithmetic formula. Interviewers usually expect the constant‑time mathematical solution because it demonstrates pattern recognition and familiarity with sum formulas.
This approach involves iterating through every number from 1 to n and checking whether each number is divisible by m. Based on this check, we add the number to either num1 or num2. Finally, calculate the result as num1 - num2.
Iterate through numbers 1 to n. Use modulo to check divisibility by m. Sum numbers accordingly in num1 and num2, then return the difference.
Time Complexity: O(n), as we iterate from 1 to n. Space Complexity: O(1), since we use constant extra space.
This approach uses arithmetic series formulas to calculate the sums directly rather than iterating through each element. The sum of numbers divisible by m can be expressed in terms of another arithmetic series with step m.
Calculates the total sum from 1 to n using n(n+1)/2. Divisible sums are determined using the formula m * (p * (p + 1))/2, where p = n / m. Updates non-divisible sum by subtracting divisible sums from total sums.
Time Complexity: O(1) because calculations are done through formulas. Space Complexity: O(1).
We traverse every number in the range [1, n]. If it is divisible by m, we subtract it from the answer. Otherwise, we add it to the answer.
After the traversal, we return the answer.
The time complexity is O(n), where n is the given integer. The space complexity is O(1).
| Approach | Complexity |
|---|---|
| Iterative Approach | Time Complexity: O(n), as we iterate from 1 to n. Space Complexity: O(1), since we use constant extra space. |
| Formula-based Optimization | Time Complexity: O(1) because calculations are done through formulas. Space Complexity: O(1). |
| Simulation | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Calculation | O(n) | O(1) | Simple implementation or when directly simulating the condition |
| Formula-based Optimization | O(1) | O(1) | Best for large n and interviews requiring mathematical optimization |
LeetCode 2894: Divisible and Non-divisible Sums Difference • Engineering Digest • 5,140 views views
Watch 9 more video solutions →Practice Divisible and Non-divisible Sums Difference with our built-in code editor and test cases.
Practice on FleetCode