Skip to content

Commit 1323ee1

Browse files
committed
Sync LeetCode submission Runtime - 5 ms (17.31%), Memory - 17.7 MB (90.64%)
1 parent db70a37 commit 1323ee1

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>You are given positive integers <code>n</code> and <code>m</code>.</p>
2+
3+
<p>Define two integers as follows:</p>
4+
5+
<ul>
6+
<li><code>num1</code>: The sum of all integers in the range <code>[1, n]</code> (both <strong>inclusive</strong>) that are <strong>not divisible</strong> by <code>m</code>.</li>
7+
<li><code>num2</code>: The sum of all integers in the range <code>[1, n]</code> (both <strong>inclusive</strong>) that are <strong>divisible</strong> by <code>m</code>.</li>
8+
</ul>
9+
10+
<p>Return <em>the integer</em> <code>num1 - num2</code>.</p>
11+
12+
<p>&nbsp;</p>
13+
<p><strong class="example">Example 1:</strong></p>
14+
15+
<pre>
16+
<strong>Input:</strong> n = 10, m = 3
17+
<strong>Output:</strong> 19
18+
<strong>Explanation:</strong> In the given example:
19+
- 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.
20+
- Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18.
21+
We return 37 - 18 = 19 as the answer.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> n = 5, m = 6
28+
<strong>Output:</strong> 15
29+
<strong>Explanation:</strong> In the given example:
30+
- 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.
31+
- Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0.
32+
We return 15 - 0 = 15 as the answer.
33+
</pre>
34+
35+
<p><strong class="example">Example 3:</strong></p>
36+
37+
<pre>
38+
<strong>Input:</strong> n = 5, m = 1
39+
<strong>Output:</strong> -15
40+
<strong>Explanation:</strong> In the given example:
41+
- Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0.
42+
- 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.
43+
We return 0 - 15 = -15 as the answer.
44+
</pre>
45+
46+
<p>&nbsp;</p>
47+
<p><strong>Constraints:</strong></p>
48+
49+
<ul>
50+
<li><code>1 &lt;= n, m &lt;= 1000</code></li>
51+
</ul>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Approach 1: Traversal
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def differenceOfSums(self, n: int, m: int) -> int:
8+
return sum(x if x % m != 0 else -x for x in range(1, n + 1))
9+

0 commit comments

Comments
 (0)