DEV Community

Somuya Khandelwal
Somuya Khandelwal

Posted on

DAY 13 Advanced Math Challenges and Problem Solving

Hello Everyone!

I’m Somuya Khandelwal, here to share the updates from Day 3 of Week 3 in my competitive programming journey. Today, I worked on Math-based problems, diving into geometry and power computations. These challenges pushed me to think logically and implement solutions efficiently while tackling precision issues and optimizing for performance.


What I Worked On Today

  1. Palindrome Number (Easy Difficulty)

    • I revisited this problem to refine my earlier solution by reversing only half of the number using mathematical operations instead of strings.
    • What I Learned:
      • Optimizing solutions reduces memory usage and improves performance, especially when dealing with larger inputs.
      • Revisiting problems to enhance initial solutions is a great way to deepen understanding.
  2. Sqrt(x) (Easy Difficulty)

    • Task: Calculate the integer square root of a number x.
    • Approach:
      • Used binary search to find the integer square root, narrowing down the range of possible values.
      • Carefully handled edge cases, such as x = 0 or x = 1, for accurate results.
    • What I Learned:
      • Binary search is an incredibly efficient tool for numerical problems, offering logarithmic time complexity.
      • Testing edge cases ensures that the solution is robust and works across all inputs.
  3. Pow(x, n) (Medium Difficulty)

    • Task: Implement a function to compute x raised to the power n.
    • Approach:
      • Used a divide-and-conquer strategy, recursively breaking down the problem by halving n.
      • Handled negative exponents by returning 1 / pow(x, -n) and considered edge cases like x = 0 and n = 0.
    • What I Learned:
      • Divide-and-conquer techniques drastically reduce time complexity, making the solution efficient for large powers.
      • Handling special cases (e.g., negative exponents) is crucial for accuracy.
  4. Max Points on a Line (Hard Difficulty)

    • Task: Determine the maximum number of points that lie on the same straight line in a 2D plane.
    • Approach:
      • Calculated slopes between every pair of points using the formula (y2 - y1) / (x2 - x1) and grouped slopes using a hashmap.
      • Used the greatest common divisor (GCD) to represent slopes as fractions to avoid floating-point precision errors.
    • What I Learned:
      • For geometry problems, representing ratios as fractions (instead of floats) ensures precision.
      • Hashmaps are incredibly helpful for grouping and counting occurrences, especially for problems involving relationships like slopes.

What I Learned Today

  1. Binary Search for Mathematical Problems:

    • Problems like Sqrt(x) reinforced how binary search simplifies finding numerical solutions efficiently.
  2. Divide-and-Conquer Efficiency:

    • Recursive techniques like in Pow(x, n) showcased how breaking problems into smaller chunks can save computational effort.
  3. Precision in Geometry:

    • Problems like Max Points on a Line emphasized the importance of avoiding floating-point calculations and using exact representations (e.g., fractions) for accuracy.
  4. Grouping Data with Hashmaps:

    • Hashmaps streamlined the grouping and counting of slopes in Max Points on a Line, making an otherwise complex problem manageable.

Reflections and Challenges

The Max Points on a Line problem was the toughest challenge today. Debugging slope calculations and ensuring precision using fractions instead of floating-point numbers required patience and careful implementation. However, solving it was deeply satisfying and improved my confidence in tackling geometry-based challenges.

Overall, these problems helped me better understand the intersection of math and programming, strengthening both my logical reasoning and coding efficiency.


Looking Ahead

Tomorrow, I’ll focus on Array-based problems, including Rotate Array and Best Time to Buy and Sell Stock II. These will test my ability to manipulate arrays effectively and optimize solutions for time and space.

Thank you for following my journey! Stay tuned for more updates as I continue to learn, solve, and grow in competitive programming.

Best regards,

Somuya

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more