Skip to content

Commit d551ff3

Browse files
committed
updated the explaination for the solution
1 parent ec24618 commit d551ff3

File tree

1 file changed

+65
-27
lines changed
  • Math/#2894 - DIvisible and non-divisible sums difference - Easy

1 file changed

+65
-27
lines changed

Math/#2894 - DIvisible and non-divisible sums difference - Easy/Explaination.md

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,75 @@
88

99
## 📝 Introduction
1010

11-
*A brief introduction to the problem. Mention the key aspects, constraints, and expected output.*
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.
1218

1319
---
1420

1521
## 💡 Approach & Key Insights
1622

17-
*Describe the main idea behind solving the problem. What observations lead to a solution? Mention brute force or naive approaches first, then discuss optimal solutions.*
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+
1830

1931
---
2032

2133
## 🛠️ Breakdown of Approaches
2234

2335
### 1️⃣ Brute Force / Naive Approach
2436

25-
- **Explanation:** *Describe the brute force solution and why it works.*
26-
- **Time Complexity:** *O(?) - Explanation*
27-
- **Space Complexity:** *O(?) - Explanation*
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.*
2845
- **Example/Dry Run:**
2946

30-
Example input: [Insert example] Step 1 → Step 2 → Step 3 → Output
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
3156

3257

3358
### 2️⃣ Optimized Approach
3459

35-
- **Explanation:** *Describe an optimized approach with clear reasoning.*
36-
- **Time Complexity:** *O(?) - Explanation*
37-
- **Space Complexity:** *O(?) - Explanation*
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)
3869
- **Example/Dry Run:**
3970

40-
Example input: [Insert example] Step 1 → Step 2 → Step 3 → Output
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
4180

4281

4382
### 3️⃣ Best / Final Optimized Approach (if applicable)
@@ -55,41 +94,40 @@ Example input: [Insert example] Step 1 → Step 2 → Step 3 → Output
5594

5695
| Approach | Time Complexity | Space Complexity |
5796
| ------------- | --------------- | ---------------- |
58-
| Brute Force | O(?) | O(?) |
59-
| Optimized | O(?) | O(?) |
97+
| Brute Force | O(n) | O(1) |
98+
| Optimized | O(1) | O(1) |
6099
| Best Approach | O(?) | O(?) |
61100

62101
---
63102

64103
## 📉 Optimization Ideas
65104

66-
*Are there any ways to further improve the solution? Can we reduce memory usage or optimize certain operations?*
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.
67106

68107
---
69108

70109
## 📌 Example Walkthroughs & Dry Runs
71110

72-
*Use visuals, ASCII diagrams, or step-by-step breakdowns for clarity.*
73-
74-
```plaintext
75111
Example:
76-
Input: 1 -> 2 -> 3 -> 4
77-
Process:
78-
1 -> Swap 2 & 3
79-
2 -> Reverse half of list
80-
3 -> Merge both halves
81-
Output: 1 -> 3 -> 2 -> 4
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
82120
```
83121
84122
---
85123
86124
## 🔗 Additional Resources
87125
88-
- [Resource 1]()
89-
- [Resource 2]()
90-
- [Resource 3]()
126+
- Arithmetic Series - Wikipedia
127+
- Python Integer Arithmetic
128+
91129
92130
---
93131
94-
Author: Your Name
95-
Date: DD/MM/YYYY
132+
Author: Andrew
133+
Date: 07/06/2025

0 commit comments

Comments
 (0)