Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Minimum Addition to Make Integer Beautiful

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2457" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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 not make n beautiful with adding non-negative integer less than 4. Example 2: Input: n = 467, target = 6 Output: 33 Explanation: Initially n is 467 and its digit sum is 4 + 6 + 7 = 17. After adding 33, n becomes 500 and digit sum becomes 5 + 0 + 0 = 5. It can be shown that we can not make n beautiful with adding non-negative integer less than 33. Example 3: Input: n = 1, target = 1 Output: 0 Explanation: Initially n is 1 and its digit sum is 1, which is already smaller than or equal to target. Constraints: 1 <= n <= 1012 1 <= target <= 150 The input will be generated such that it is always possible to make n beautiful.

Explanation

Here's a breakdown of the approach, followed by the Python code:

  • Iterate from right to left: Process the digits of 'n' from the least significant to the most significant.
  • Handle digits exceeding the target: If the current digit causes the sum of digits to exceed the target, we need to increment the next higher digit (carry-over). Calculate the value to add to 'n' to achieve this.
  • Optimization: Keep track of sum of digits so far, so that we don't recalculate every time.

  • Runtime Complexity: O(log n), where n is the input number. The number of iterations is proportional to the number of digits in n. Storage Complexity: O(1).

Code

    def make_beautiful(n: int, target: int) -> int:
    """
    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.
    """
    s = str(n)
    sum_digits = 0
    ans = 0
    for i in range(len(s) - 1, -1, -1):
        digit = int(s[i])
        sum_digits += digit
        if sum_digits > target:
            add = 10**(len(s) - 1 - i) - (n % 10**(len(s) - 1 - i))
            n += add
            ans += add
            s = str(n)
            sum_digits = 0
            for j in range(len(s)):
                sum_digits += int(s[j])

    return ans

More from this blog

C

Chatmagic blog

2894 posts