I don't know If this is a good decision or not... As a man who likes Probabilitis, there's one 2 in this case:
- I go CRAZYπ« . or
- I Become
THE BEST PROGRAMMER IN THE WORLDπ΅
SOUNDS GOOD TO ME!!
π§ Solving LeetCode Until I Become Top 1% β Day 1
πΉ Problem: Divisible and Non-Divisible Sums Difference
Difficulty: Easy
Tags: Math
π Problem Summary
We have to find the difference between the sums of two arrays
num1
andnum2
, with elements from 1 ton
.num1
contains numbers not divisible bym
, andnum2
contains numbers divisible bym
. The result should be the difference between the sums ofnum1
andnum2
.
π§ My Thought Process
- Brute Force Idea: Iterate through both arrays, calculate the sums while checking divisibility, and return the difference.
Time Complexity: O(n)
But as this is a math problem, I thought there might be a faster way to solve it.
-
Optimized Strategy:
Since both arrays consist of elements from 1 to
n
, depending on divisibility, there are key properties we can use to calculate the sums directly without iterating through all elements.
Key Observations:
We can find the sum of
num1
by subtracting the sum of numbers divisible bym
from the total sum of numbers from 1 ton
.The count of numbers divisible by
m
in the range 1 ton
isn // m
.-
To find the sum of numbers divisible by
m
, letβs saym = 2
. The numbers divisible bym
are2, 4, 6, ..., n
(ifn
is even) or2, 4, 6, ..., n-1
(ifn
is odd).- This can be rewritten as
2 * (1 + 2 + 3 + ... + k)
, wherek = n // m
. - So the sum of numbers divisible by
m
ism * (k * (k + 1)) // 2
.
- This can be rewritten as
The formula for the sum of the first
n
natural numbers isn * (n + 1) // 2
.Therefore, the sum of numbers not divisible by
m
istotal_sum - sum_of_divisible_by_m
.-
The sum of
num2
is just the sum of numbers divisible bym
, which we've already calculated.- Algorithm Used: Math β Using properties of arithmetic series to calculate sums directly.
βοΈ Code Implementation (Python)
class Solution:
def differenceOfSums(self, n: int, m: int) -> int:
total = n * (n + 1) // 2
k = n // m
divisible_sum = m * (k * (k + 1)) // 2
non_divisible_sum = total - divisible_sum
return non_divisible_sum - divisible_sum
β±οΈ Time & Space Complexity
- Time: O(1)
- Space: O(1)
π§© Key Takeaways
-
β What concept or trick did I learn?
- Remembered the formula for the sum of the first
n
natural numbers and how to apply it to find sums based on divisibility.
- Remembered the formula for the sum of the first
-
π‘ What made this problem tricky?
- It wasnβt immediately obvious that a mathematical approach would be more efficient than brute force.
-
π How will I recognize a similar problem in the future?
- Look for problems involving sums or sequences where properties of numbers can simplify calculations.
π Reflection (Self-Check)
- [β ] Could I solve this without help?
- [β ] Did I write code from scratch?
- [β ] Did I understand why it works?
- [β ] Will I be able to recall this in a week?
π Progress Tracker
Metric | Value |
---|---|
Day | 1 |
Total Problems Solved | 334 |
Confidence Today | π |
Current Rank | 40.25% |
Top comments (0)