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:
Example 2:
Input: n = 5, m = 6 Output: 15 Explanation: In the given example:
Write a function to solve this problem efficiently.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach for this problem involves checking every single number within the given range and then calculating a sum based on whether it's divisible by a certain number. We then calculate another sum for the numbers that are not divisible and find the difference.
Here's how the algorithm would work step-by-step:
def divisible_and_non_divisible_sums_difference(limit, divisor):
divisible_sum = 0
non_divisible_sum = 0
for number in range(1, limit + 1):
# Check if the number is divisible by the divisor.
if number % divisor == 0:
divisible_sum += number
# If it's not divisible, accumulate to non_divisible_sum.
else:
non_divisible_sum += number
# The problem asks us to subtract non_divisible_sum from divisible_sum
return divisible_sum - non_divisible_sum
The optimal approach involves calculating two sums and finding their difference directly. We calculate the sum of numbers divisible by a given number and the sum of numbers that are not, within a specified range. Then, we subtract the second sum from the first to find the final difference.
Here's how the algorithm would work step-by-step:
def divisible_and_non_divisible_sums_difference(upper_limit, divisor):
divisible_sum = 0
non_divisible_sum = 0
# Sum all numbers divisible by divisor.
for number in range(1, upper_limit + 1):
if number % divisor == 0:
divisible_sum += number
# Sum all numbers NOT divisible by divisor.
for number in range(1, upper_limit + 1):
if number % divisor != 0:
non_divisible_sum += number
# Calculate the difference.
sums_difference = divisible_sum - non_divisible_sum
return sums_difference
Case | How to Handle |
---|---|
n is zero or negative | Return 0 immediately, as the problem specifies positive n. |
n is very large, leading to potential integer overflow in sums | Use 64-bit integers (long) to store sums to avoid overflow. |
num1 or num2 is zero | Handle division by zero by adding conditional checks. |
num1 and num2 are both 1 | The condition i % num1 == 0 and i % num2 == 0 reduces to i % 1 == 0, which is always true, potentially affecting performance. |
num1 and num2 are equal | The calculations will still be correct but should be tested. |
All numbers from 1 to n are divisible by num1 or num2 | This results in nonDivisibleSum being 0 and checks for potential unexpected behaviors. |
n is close to the maximum integer value | Check for potential integer overflow when calculating sums and use long if needed. |
num1 and num2 are both larger than n | divisibleSum will be 0 as there are no numbers from 1 to n divisible by num1 or num2. |