Skip to content

Commit dd7a2d5

Browse files
committed
Add untracked files
1 parent beedbbf commit dd7a2d5

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
# Problem Title
2+
3+
**Difficulty:** Easy
4+
**Category:** Math
5+
**Leetcode Link:** [Problem Link](https://leetcode.com/problems/divisible-and-non-divisible-sums-difference/)
6+
7+
---
8+
9+
## 📝 Introduction
10+
11+
You're given two integers, n and m. Your task is to calculate the difference between:
12+
13+
num1: the sum of all numbers from 1 to n not divisible by m.
14+
15+
num2: the sum of all numbers from 1 to n divisible by m.
16+
17+
Return the value of num1 - num2.
18+
19+
---
20+
21+
## 💡 Approach & Key Insights
22+
23+
The total sum of numbers from 1 to n is known via the formula: n(n + 1) / 2.
24+
25+
The sum of numbers divisible by m is also a simple arithmetic series.
26+
27+
Subtracting 2 * (sum of numbers divisible by m) from the total sum gives us num1 - num2.
28+
29+
30+
31+
---
32+
33+
## 🛠️ Breakdown of Approaches
34+
35+
### 1️⃣ Brute Force / Naive Approach
36+
37+
-**Explanation:** Loop through all numbers from 1 to n. For each number:
38+
39+
If it is divisible by m, add to num2.
40+
41+
Else, add to num1.
42+
Finally return num1 - num2.
43+
- **Time Complexity:** *O(?) - because we loop from 1 to n.*
44+
- **Space Complexity:** *O(?) - constant space for sums.*
45+
- **Example/Dry Run:**
46+
47+
Example input: n = 10, m = 3
48+
49+
Loop from 1 to 10:
50+
51+
Not divisible by 3: 1 + 2 + 4 + 5 + 7 + 8 + 10 = 37
52+
53+
Divisible by 3: 3 + 6 + 9 = 18
54+
55+
Output: 37 - 18 = 19
56+
57+
58+
### 2️⃣ Optimized Approach
59+
60+
- **Explanation:** Use math formulas to compute the total sum of numbers from 1 to n, and separately the sum of numbers divisible by m:
61+
62+
Total sum: n(n + 1) / 2
63+
64+
Sum divisible by m: m * k(k + 1) / 2 where k = n // m
65+
66+
Return: total_sum - 2 * divisible_sum
67+
- **Time Complexity:** *O(1) - no loops, only arithmetic.*
68+
- **Space Complexity:** *O(1)
69+
- **Example/Dry Run:**
70+
71+
Input: n = 10, m = 3
72+
73+
total_sum = 10 × 11 / 2 = 55
74+
75+
k = 10 // 3 = 3
76+
77+
divisible_sum = 3 × (3 × 4 / 2) = 3 × 6 = 18
78+
79+
Output = 55 - 2 × 18 = 19
80+
81+
82+
### 3️⃣ Best / Final Optimized Approach (if applicable)
83+
84+
- **Explanation:** *Discuss the best possible solution.*
85+
- **Time Complexity:** *O(?) - Explanation*
86+
- **Space Complexity:** *O(?) - Explanation*
87+
- **Example/Dry Run:**
88+
89+
Example input: [Insert example] Step 1 → Step 2 → Step 3 → Output
90+
91+
---
92+
93+
## 📊 Complexity Analysis
94+
95+
| Approach | Time Complexity | Space Complexity |
96+
| ------------- | --------------- | ---------------- |
97+
| Brute Force | O(n) | O(1) |
98+
| Optimized | O(1) | O(1) |
99+
| Best Approach | O(?) | O(?) |
100+
101+
---
102+
103+
## 📉 Optimization Ideas
104+
105+
The optimized solution already achieves constant time and space using mathematical formulas. No further optimization is necessary unless extending to very large ranges, in which case care for integer overflow may be required.
106+
107+
---
108+
109+
## 📌 Example Walkthroughs & Dry Runs
110+
111+
Example:
112+
Input: n = 5, m = 2
113+
Total sum = 5 * 6 / 2 = 15
114+
Divisible by 2: 2 + 4 = 6
115+
Not divisible: 1 + 3 + 5 = 9
116+
Answer: 9 - 6 = 3
117+
118+
Or:
119+
Answer = 15 - 2 * 6 = 3
120+
```
121+
122+
---
123+
124+
## 🔗 Additional Resources
125+
126+
- Arithmetic Series - Wikipedia
127+
- Python Integer Arithmetic
128+
129+
130+
---
131+
132+
Author: Andrew
133+
Date: 07/06/2025
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution(object):
2+
def differenceOfSums(self, n, m):
3+
4+
totalsum = n * (n+1) // 2
5+
6+
7+
k = n // m
8+
9+
10+
sum_divisible_by_m = m * k * (k + 1) // 2
11+
12+
# num1 - num2 = (sum of numbers not divisible by m) - (sum of numbers divisible by m)
13+
result = totalsum - 2 * sum_divisible_by_m
14+
return result

0 commit comments

Comments
 (0)