Minimum value of X that can be added to N to minimize sum of the digits to ≤ K

Given two integers N and K, the task is to find the minimum integer X that can be added to N so that the sum of the digits of the newly formed number does not exceed K.

Examples:

Input: N = 1, K = 1 
Output:
Explanation: 
The sum of the digits of the given number is 1, which is already equal to K(=1).

Input: N = 11, K = 1 
Output: 89 
Explanation: 
Adding the number 89 to the given number 11 results to 100. 
The sum of digits of the new number formed is 1 which does not exceed K(=1). 
Therefore, the minimum number that can be added is 89. 

Approach: Follow the steps below to solve the problem: 



  1. Check if the sum of the digits of the given number N does not exceed K or not. If found to be true, then the least number added is 0.
  2. Now, start calculating the sum of digits from the unit’s place and continue until the sum of digits exceeds K.
  3. Now, the part of N having a sum of the digits greater than or equal to K is found. So, eliminate the last digit of that part so that the sum of the digits becomes less than K.
  4. Now, add 1 to the newly obtained number as it will keep the sum of the digits less than or equal to K.
  5. Now, to obtain the new number which exceeds N and has the number of digits less than or equal to K, multiply the number with 10P + 1, where P is the count of digits up to which sum did not exceed K.
  6. Now subtract N from the new number to get the result X.
  7. Print the value of X after completing the above steps.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum number
// needed to be added so that the sum
// of the digits does not exceed K
int minDigits(int N, int K)
{
    // Find the number of digits
    int digits_num
        = floor(log10(N) + 1);
 
    int temp_sum = 0;
    int temp = digits_num;
    int result;
 
    int X, var;
 
    int sum = 0;
    int num2 = N;
 
    // Calculate sum of the digits
    while (num2 != 0) {
 
        // Add the digits of num2
        sum += num2 % 10;
        num2 /= 10;
    }
 
    // If the sum of the digits of N
    // is less than or equal to K
    if (sum <= K) {
 
        // No number needs to
        // be added
        X = 0;
    }
 
    // Otherwise
    else {
 
        while (temp > 0) {
 
            // Calculate the sum of digits
            // from least significant digit
            var = (N / (pow(10, temp - 1)));
            temp_sum += var % 10;
 
            // If sum exceeds K
            if (temp_sum >= K) {
 
                // Increase previous
                // digit by 1
                var /= 10;
                var++;
 
                // Add zeros to the end
                result
                    = var * pow(10, temp);
 
                break;
            }
 
            temp--;
        }
 
        // Calculate difference
        // between the result and N
        X = result - N;
 
        // Return the result
        return X;
    }
}
 
// Driver Code
int main()
{
    int N = 11, K = 1;
    cout << minDigits(N, K);
 
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
import java.util.*;
import java.io.*;
 
class GFG{
     
// Function to find the minimum number
// needed to be added so that the sum
// of the digits does not exceed K
static int minDigits(int N, int K)
{
     
    // Find the number of digits
    int digits_num = (int)Math.floor(
                          Math.log(N) + 1);
   
    int temp_sum = 0;
    int temp = digits_num;
    int result = 0;
    int X, var;
    int sum = 0;
    int num2 = N;
   
    // Calculate sum of the digits
    while (num2 != 0)
    {
         
        // Add the digits of num2
        sum += num2 % 10;
        num2 /= 10;
    }
   
    // If the sum of the digits of N
    // is less than or equal to K
    if (sum <= K)
    {
         
        // No number needs to
        // be added
        X = 0;
    }
   
    // Otherwise
    else
    {
        while (temp > 0)
        {
             
            // Calculate the sum of digits
            // from least significant digit
            var = (N / ((int)Math.pow(
                   10, temp - 1)));
            temp_sum += var % 10;
   
            // If sum exceeds K
            if (temp_sum >= K)
            {
                 
                // Increase previous
                // digit by 1
                var /= 10;
                var++;
   
                // Add zeros to the end
                result = var * (int)Math.pow(
                         10, temp);
                break;
            }
            temp--;
        }
         
        // Calculate difference
        // between the result and N
        X = result - N;
   
        // Return the result
        return X;
    }
    return -1;
}
 
// Driver Code
public static void main(String args[])
{
    int N = 11;
    int K = 1;
        
    System.out.println(minDigits(N, K));
}
}
 
// This code is contributed by bikram2001jha

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program for
# the above approach
import math;
 
# Function to find the minimum number
# needed to be added so that the sum
# of the digits does not exceed K
def minDigits(N, K):
 
    # Find the number of digits
    digits_num = int(math.floor(math.log(N) + 1));
 
    temp_sum = 0;
    temp = digits_num;
    result = 0;
    X = 0; var = 0;
    sum1 = 0;
    num2 = N;
 
    # Calculate sum of the digits
    while (num2 != 0):
 
        # Add the digits of num2
        sum1 += num2 % 10;
        num2 /= 10;   
 
    # If the sum of the digits of N
    # is less than or equal to K
    if (sum1 <= K):
 
        # No number needs to
        # be added
        X = 0;   
 
    # Otherwise
    else:
        while (temp > 0):
 
            # Calculate the sum of digits
            # from least significant digit
            var = int(N // (pow(10, temp - 1)));
            temp_sum += var % 10;
 
            # If sum exceeds K
            if (temp_sum >= K):
 
                # Increase previous
                # digit by 1
                var = var // 10;
                var += 1;
 
                # Add zeros to the end
                result = var * int(pow(10, temp));
                break;
             
            temp -= 1;       
 
        # Calculate difference
        # between the result and N
        X = result - N;
 
        # Return the result
        return X;
     
    return -1;
 
# Driver Code
if __name__ == '__main__':
    N = 11;
    K = 1;
    print(minDigits(N, K));
 
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the minimum number
// needed to be added so that the sum
// of the digits does not exceed K
static int minDigits(int N, int K)
{
     
    // Find the number of digits
    int digits_num = (int)Math.Floor(
                          Math.Log(N) + 1);
   
    int temp_sum = 0;
    int temp = digits_num;
    int result = 0;
    int X, var;
    int sum = 0;
    int num2 = N;
   
    // Calculate sum of the digits
    while (num2 != 0)
    {
         
        // Add the digits of num2
        sum += num2 % 10;
        num2 /= 10;
    }
   
    // If the sum of the digits of N
    // is less than or equal to K
    if (sum <= K)
    {
         
        // No number needs to
        // be added
        X = 0;
    }
   
    // Otherwise
    else
    {
        while (temp > 0)
        {
             
            // Calculate the sum of digits
            // from least significant digit
            var = (N / ((int)Math.Pow(
                   10, temp - 1)));
                    
            temp_sum += var % 10;
   
            // If sum exceeds K
            if (temp_sum >= K)
            {
                 
                // Increase previous
                // digit by 1
                var /= 10;
                var++;
   
                // Add zeros to the end
                result = var * (int)Math.Pow(
                         10, temp);
                break;
            }
            temp--;
        }
         
        // Calculate difference
        // between the result and N
        X = result - N;
   
        // Return the result
        return X;
    }
    return -1;
}
 
// Driver Code
public static void Main(String []args)
{
    int N = 11;
    int K = 1;
        
    Console.WriteLine(minDigits(N, K));
}
}
 
// This code is contributed by Amit Katiyar

chevron_right


Output: 

89






 

Time Complexity: O(log10N) 
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:



If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.