Hello Everyone!
I am Somuya Khandelwal, here to share the updates from Day 3 of Week 3 in my competitive programming journey. Today, I tackled Math-based problems, including geometry and power computations, which required a strong grasp of mathematical logic and efficient implementation techniques.
What I Worked On Today
-
Palindrome Number (Easy Difficulty)
- Revisited to optimize my initial solution by reversing only half of the digits using mathematical operations.
-
What I Learned:
- Optimized solutions often reduce memory usage and run faster, especially when handling large numbers.
-
Sqrt(x) (Easy Difficulty)
- Compute the square root of a number
x
and return its integer part. -
Approach:
- Used binary search to find the integer square root efficiently.
- Checked midpoints to avoid overshooting and ensured accuracy.
-
What I Learned:
- Binary search is a powerful tool for narrowing down solutions in logarithmic time.
- Handling edge cases, such as
x = 0
orx = 1
, ensures robustness.
- Compute the square root of a number
-
Pow(x, n) (Medium Difficulty)
- Implement a function to calculate
x
raised to the powern
. -
Approach:
- Used a divide-and-conquer strategy to compute powers recursively, halving
n
at each step. - Optimized for negative exponents by returning
1 / pow(x, -n)
.
- Used a divide-and-conquer strategy to compute powers recursively, halving
-
What I Learned:
- Divide-and-conquer reduces time complexity to O(log n), making the solution efficient for large
n
. - Negative exponents and edge cases (e.g.,
x = 0
,n = 0
) require careful handling.
- Divide-and-conquer reduces time complexity to O(log n), making the solution efficient for large
- Implement a function to calculate
-
Max Points on a Line (Hard Difficulty)
- Find the maximum number of points that lie on the same straight line in a 2D plane.
-
Approach:
- Calculated slopes between points using the formula
(y2 - y1) / (x2 - x1)
and stored them in a hashmap. - Used a greatest common divisor (GCD) approach to represent slopes as fractions and avoid precision issues.
- Calculated slopes between points using the formula
-
What I Learned:
- Geometry problems often involve efficient representations of ratios to avoid floating-point errors.
- Hashmaps are invaluable for grouping and counting occurrences of slopes.
What I Learned Today
-
Binary Search in Mathematical Problems:
- Problems like Sqrt(x) highlight the utility of binary search for finding roots or solving numerical constraints efficiently.
-
Divide-and-Conquer in Exponentiation:
- Recursive solutions like in Pow(x, n) demonstrate how divide-and-conquer strategies drastically reduce computational time.
-
Handling Precision in Geometry:
- Representing slopes using fractions instead of floating-point numbers ensures accuracy in problems like Max Points on a Line.
-
Hashmaps for Grouping:
- Using hashmaps to group and count slopes simplified the solution for Max Points on a Line, making it manageable despite its complexity.
Reflections and Challenges
The Max Points on a Line problem was the most challenging today. Debugging precision issues and efficiently grouping points by slope required multiple iterations and careful attention to detail. However, solving it provided a deeper understanding of how geometry problems can be approached programmatically.
Overall, today’s challenges tested my ability to combine mathematical logic with efficient programming techniques, and I feel more confident handling complex math-based problems.
Looking Ahead
Tomorrow, I’ll shift focus to Array-based problems, including Rotate Array and Best Time to Buy and Sell Stock II. These problems will test my understanding of array manipulation and optimization techniques.
Thank you for following along on my journey! Stay tuned for more updates and insights as I continue exploring competitive programming.
Top comments (0)