DEV Community

Somuya Khandelwal
Somuya Khandelwal

Posted on

DAY 17 Solving Math-Based Problems with Precision

Hello Everyone!

I am Somuya khandelwal, back with updates from Day 2 of Week 4 in my competitive programming journey. Today’s focus was on Math-based problems, which are often deceptively simple but require careful reasoning and precise implementation. The challenges today helped me reinforce my understanding of mathematical patterns and efficient computational techniques.


What I Worked On Today

  1. Palindrome Number (Easy Difficulty)

    • Determine if an integer reads the same forward and backward.
    • Approach:
      • Used mathematical operations to reverse half of the number and compared it with the other half.
      • Avoided converting the number to a string to ensure the solution was space-efficient.
    • What I Learned:
      • Using mathematical operations over string manipulation reduces space usage.
      • Handling edge cases like negative numbers and numbers ending in zero is crucial.
  2. Plus One (Easy Difficulty)

    • Add one to a non-negative integer represented as an array of digits.
    • Approach:
      • Iterated from the least significant digit to the most significant, propagating any carry.
      • Inserted an additional digit if all digits resulted in a carry (e.g., [9,9,9] to [1,0,0,0]).
    • What I Learned:
      • Problems involving carry propagation require careful backward traversal.
      • Handling edge cases like an all-9 input ensures robustness.
  3. Factorial Trailing Zeros (Medium Difficulty)

    • Count the number of trailing zeros in the factorial of a number n.
    • Approach:
      • Calculated the number of factors of 5 in the numbers from 1 to n, as each factor of 5 pairs with a factor of 2 to form a trailing zero.
    • What I Learned:
      • Efficient solutions avoid direct computation of factorials, which grow exponentially.
      • Understanding the properties of prime factorization helps solve such problems elegantly.

What I Learned Today

  1. Mathematical Insights for Efficiency:

    • Problems like Factorial Trailing Zeros highlight the importance of understanding mathematical properties to avoid computational overhead.
  2. Optimized Solutions over Brute Force:

    • Efficiently reversing numbers in Palindrome Number and using a greedy carry propagation in Plus One reinforced the value of optimized approaches.
  3. Edge Case Handling:

    • Ensuring correctness for inputs like 0, negative numbers, or arrays with all 9s (e.g., [9,9,9]) is a critical part of problem-solving.
  4. Breaking Problems into Steps:

    • Dividing problems into smaller logical steps, as seen in all three problems, makes implementation simpler and debugging easier.

Reflections and Challenges

The Factorial Trailing Zeros problem was the most interesting today. It required a shift in perspective from computing the factorial to analyzing its factors. Debugging the carry logic in Plus One was also a valuable exercise, as it required careful attention to index management. Overall, today’s challenges strengthened my problem-solving skills and mathematical intuition.


Looking Ahead

Tomorrow, I’ll work on Two-Pointer Problems, including Text Justification, Container With Most Water, and 3 Sum. These problems will test my ability to efficiently navigate arrays and solve complex optimization challenges.

Thank you for following my journey! Stay tuned for more updates and insights as I continue to tackle exciting challenges in competitive programming.

Top comments (0)