Advertisement

Minimum Addition to Make Integer Beautiful - LeetCode 2457 Solution

Minimum Addition to Make Integer Beautiful - Complete Solution Guide

Minimum Addition to Make Integer Beautiful is LeetCode problem 2457, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given two positive integers n and target . An integer is considered beautiful if the sum of its digits is less than or equal to target . Return the minimum non-negative integer x such that n + x is beautiful . The input will be generated such that it is always possible to make n beautiful. Example 1: Input: n = 16, target = 6 Output: 4 Explanation: Initially n is 16 and its digit sum is 1 + 6 = 7. After adding 4, n becomes 20 and digit sum becomes 2 + 0 = 2. It can be shown that we can n

Detailed Explanation

The problem requires finding the smallest non-negative integer 'x' that, when added to a given integer 'n', results in a 'beautiful' integer. An integer is considered 'beautiful' if the sum of its digits is less than or equal to a given 'target' value. The input 'n' is a positive integer, and 'target' is a positive integer. The constraints guarantee that a solution always exists.

Solution Approach

The solution uses a greedy approach. It starts by checking if the initial number 'n' is already beautiful. If not, it iteratively adds a value 'x' to 'n' to make the last digits zero, effectively rounding 'n' up to the nearest power of 10. This process continues until the resulting number's digit sum is less than or equal to 'target'. The algorithm finds the smallest 'x' by incrementing the power of 10 being considered. The loop stops when `new_n` becomes 'beautiful'.

Step-by-Step Algorithm

  1. Step 1: Define a helper function `digit_sum(num)` that calculates the sum of the digits of a given number 'num'.
  2. Step 2: Check if the initial number 'n' is already beautiful (i.e., `digit_sum(n) <= target`). If it is, return 0.
  3. Step 3: Initialize a variable 'p' to 10. This represents the current power of 10 we are considering.
  4. Step 4: Enter a `while` loop that continues until a beautiful number is found.
  5. Step 5: Calculate the remainder of 'n' when divided by 'p'. This represents the part of 'n' that needs to be rounded up.
  6. Step 6: Calculate 'x' as `(p - remainder) % p`. This ensures 'x' is the smallest non-negative integer that, when added to 'n', makes the last digits (up to the current power of 10) zero.
  7. Step 7: Calculate the new number `new_n = n + x`.
  8. Step 8: Check if `new_n` is beautiful (i.e., `digit_sum(new_n) <= target`). If it is, return 'x'.
  9. Step 9: If `new_n` is not beautiful, multiply 'p' by 10 to consider the next power of 10 and continue the loop.

Key Insights

  • Insight 1: The core idea is to progressively round 'n' up to the nearest power of 10, starting from the least significant digit. We repeatedly add to 'n' until its digit sum is less than or equal to the target.
  • Insight 2: The `digit_sum` function efficiently calculates the sum of the digits of an integer.
  • Insight 3: The modulo operator (%) is used effectively to isolate the rightmost digits of 'n' and calculate the value 'x' needed to round it up.

Complexity Analysis

Time Complexity: O(log(n))

Space Complexity: O(1)

Topics

This problem involves: Math, Greedy.

Companies

Asked at: Infosys.