DEV Community

Somuya Khandelwal
Somuya Khandelwal

Posted on

DAY 17 Solving Math-Based Problems with Precision

Hello Everyone!

I’m Somuya Khandelwal, back to share my updates from Day 2 of Week 4 in my competitive programming journey. Today’s focus was on Math-based problems, a category that often seems straightforward but demands a strong grasp of logical reasoning and precise implementation. The problems I solved today pushed me to think deeply about patterns and efficient methods, and I gained a lot of valuable insights along the way.


What I Worked On Today

  1. Palindrome Number (Easy Difficulty)

    • Task: Check if a given integer reads the same forward and backward.
    • Approach:
      • Used mathematical operations to reverse half of the number and compared it with the remaining half.
      • Avoided converting the number to a string to make the solution more efficient in terms of space.
    • What I Learned:
      • Mathematical operations can often replace string manipulation for better efficiency.
      • Handling special cases, like negative numbers or numbers ending in zero, is critical for ensuring a robust solution.
  2. Plus One (Easy Difficulty)

    • Task: Increment a non-negative integer represented as an array of digits.
    • Approach:
      • Started from the least significant digit and propagated the carry backward.
      • For cases where all digits resulted in a carry (e.g., [9,9,9]), added a new digit at the start.
    • What I Learned:
      • Carry propagation requires careful backward traversal and attention to how digits are updated.
      • Edge cases, such as inputs with only 9s, require adding additional logic to handle new digits.
  3. Factorial Trailing Zeros (Medium Difficulty)

    • Task: Count the trailing zeros in the factorial of a given number n.
    • Approach:
      • Counted the number of factors of 5 in the range 1 to n, as each factor of 5 pairs with a factor of 2 to create a trailing zero.
    • What I Learned:
      • Problems like this emphasize the importance of understanding properties like prime factorization instead of brute-forcing a solution.
      • Avoiding direct factorial computation is essential, as factorial values grow too large to handle directly.

What I Learned Today

  1. Efficiency Through Mathematical Patterns:

    • The Factorial Trailing Zeros problem showed me how leveraging mathematical properties can simplify solutions and avoid unnecessary calculations.
  2. Optimized Approaches Save Time and Space:

    • In Palindrome Number, reversing only half the digits instead of converting to a string reduced space complexity while maintaining accuracy.
  3. Handling Edge Cases:

    • I paid special attention to edge cases, such as 0, negative numbers in Palindrome Number, or arrays with all 9s in Plus One, which helped me ensure robust solutions.
  4. Breaking Problems into Steps:

    • Dividing problems into smaller, manageable steps—like separating the digit manipulation in Plus One or calculating factors in Factorial Trailing Zeros—made implementation much simpler.

Reflections and Challenges

The Factorial Trailing Zeros problem was the most thought-provoking today. It required me to shift my approach from computing the factorial itself to analyzing its factors, which was both challenging and rewarding. Debugging the carry propagation logic in Plus One also took time, as it involved handling multiple edge cases, but it helped me understand the importance of meticulous planning in array-based problems.


Looking Ahead

Tomorrow, I’ll move on to Two-Pointer Problems, tackling challenges like Text Justification, Container With Most Water, and 3 Sum. These problems will test my ability to efficiently traverse and manipulate arrays while solving optimization tasks.

Thank you for following along with my journey! I’m excited to continue learning and sharing my progress in the world of competitive programming. Stay tuned for more updates!

Best regards,

Somuya

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

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay