Divisible and Non-divisible Sums Difference

You are given positive integers `n` and `m`. Let `num1` be the sum of all integers in `[1, n]` that are **not** divisible by `m`, and `num2` the sum of all integers in `[1, n]` that **are** divisible by `m`. Return `num1 - num2`.

30:00

Divisible and Non-divisible Sums Difference
easy
Free written solution
Topics
Companies

You are given positive integers n and m.

Let num1 be the sum of all integers in [1, n] that are not divisible by m, and num2 the sum of all integers in [1, n] that are divisible by m.

Return num1 - num2.

Example 1:
Input: {"n":10,"m":3}
Output: 19
Constraints:
  • 1≤n,m≤10001 \leq n, m \leq 1000

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":10,"m":3}

Lay 1..10 out in rows of m = 3. Every multiple of 3 lands in the last column, so num2 is that column and num1 is everything else.

1..10 in rows of m = 3

not divisible (num1)

divisible (num2)

1
2
3
4
5
6
7
8
9
10

total = 1 + ... + n

?

k = n // m

?

num2 = m(1 + ... + k)

?

num1 = total - num2

?

num1 - num2

?

Variables
VariableValue
n10
m3
DepthFunction Call
Stack empty
0/8