Minimize increments to make digit sum of N at most S

• Last Updated : 17 Nov, 2021

Given two positive integers N and S. The task is to minimize the number of increments on N (N = N + 1) required to make the sum of digits of N less than or equal to S
NOTE: N can range up to an18 digit number and 1 <= S <= 162.

Examples:

Input: N = 600, S = 5
Output: 400
Explanation: Minimum 400 increments are required as the sum of digits of 1000(600 + 400) is less than 5.

Input: N = 345899211156769, S = 20
Output: 100788843231
Explanation: Minimum required increments are 100788843231.

Approach: The key observation here is that in order to minimize the sum of digits of any number in minimum increments, the digits starting from the unit’s place need to be minimized. Follow the steps below to solve the given problem.

• For the base case, If the sum of digits of N is already less than or equal to S, then output is 0.
• Initialize variables, say ans =0 and p = 1, where ans will store the minimum required increments and p will keep track of the digits place, starting from the unit’s place.
• Run a loop through the maximum number of digits N can have (i.e., 18) and find the last digit of N. Let it be denoted by digit. Then find the minimum increments required to convert this digit to 0. Denoted by say, add.
• digit = (N / p) % 10
• add = p * (10 – digit)
• Increment N and ans by add.
• Now check whether the sum of digits of N is less than or equal to S.
• If the condition is true, break from the loop and output the answer.
• Otherwise, multiply p by 10 to access the second last digit from the unit’s place of N.
• The loop runs again and the same steps are executed till the required answer is found.
• Return ans as the final answer.

Below is the implementation of the above approach:

C++

 // C++ program for the above approach#include using namespace std; // Function to find the sum// of digits of Nint findSum(long long int N){    // Stores the sum of digits of N    int res = 0;     // Loop to extract the digits of N    // and find their sum    while (N) {         // Extracting the last digit of N        // and adding it to res        res += (N % 10);         // Update N        N /= 10;    }     return res;} // Function to find the minimum increments// required to make the sum of digits of N// less than or equal to S.long long int minIncrements(long long int N, int S){    // If the sum of digits of N is less than    // or equal to S    if (findSum(N) <= S) {         // Output 0        return 0;    }     // variable to access the digits of N    long long int p = 1;     // Stores the required answer    long long int ans = 0;     // Loop to access the digits of N    for (int i = 0; i <= 18; ++i) {         // Stores the digit of N starting        // from unit's place        int digit = (N / p) % 10;         // Stores the increment required        // to make the digit 0        long long int add = p * (10 - digit);         // Update N        N += add;         // Update ans        ans += add;         // If the sum of digits of N is less than        // or equal to S        if (findSum(N) <= S) {             // Break from the loop            break;        }         // Update p to access the next digit        p = p * 10;    }     return ans;} // Driver Codeint main(){     // Given N and S    long long int N = 345899211156769;    int S = 20;     // Function call    cout << minIncrements(N, S);     return 0;}

Java

 // Java program for the above approachimport java.util.*;public class GFG{   // Function to find the sum// of digits of Nstatic int findSum(long N){       // Stores the sum of digits of N    int res = 0;     // Loop to extract the digits of N    // and find their sum    while (N != 0) {         // Extracting the last digit of N        // and adding it to res        res += (N % 10);         // Update N        N /= 10;    }     return res;} // Function to find the minimum increments// required to make the sum of digits of N// less than or equal to S.static long minIncrements(long N, int S){       // If the sum of digits of N is less than    // or equal to S    if (findSum(N) <= S) {         // Output 0        return 0;    }     // variable to access the digits of N    long p = 1;     // Stores the required answer    long ans = 0;     // Loop to access the digits of N    for (int i = 0; i <= 18; ++i) {         // Stores the digit of N starting        // from unit's place        long digit = (N / p) % 10;         // Stores the increment required        // to make the digit 0        long add = p * (10 - digit);         // Update N        N += add;         // Update ans        ans += add;         // If the sum of digits of N is less than        // or equal to S        if (findSum(N) <= S) {             // Break from the loop            break;        }         // Update p to access the next digit        p = p * 10;    }     return ans;} // Driver Codepublic static void main(String args[]){    // Given N and S    long N = 345899211156769L;    int S = 20;     // Function call    System.out.println(minIncrements(N, S));}} // This code is contributed by Samim Hossain Mondal

Python3

 # Python program for the above approach # Function to find the sum# of digits of Ndef findSum(N):     # Stores the sum of digits of N  res = 0;   # Loop to extract the digits of N  # and find their sum  while (N):     # Extracting the last digit of N    # and adding it to res    res += (N % 10);     # Update N    N = N // 10;     return res; # Function to find the minimum increments# required to make the sum of digits of N# less than or equal to S.def minIncrements(N, S):     # If the sum of digits of N is less than  # or equal to S  if (findSum(N) <= S):     # Output 0    return 0;     # variable to access the digits of N  p = 1;   # Stores the required answer  ans = 0;   # Loop to access the digits of N  for i in range(0, 18):     # Stores the digit of N starting    # from unit's place    digit = (N // p) % 10;     # Stores the increment required    # to make the digit 0    add = p * (10 - digit);     # Update N    N += add;     # Update ans    ans += add;     # If the sum of digits of N is less than    # or equal to S    if (findSum(N) <= S):       # Break from the loop      break;         # Update p to access the next digit    p = p * 10;     return ans; # Driver Code # Given N and SN = 345899211156769;S = 20; # Function callprint(minIncrements(N, S)) # This code is contributed by saurabh_jaiswal.

C#

 // C# program for the above approachusing System;using System.Collections; class GFG{   // Function to find the sum// of digits of Nstatic long findSum(long N){       // Stores the sum of digits of N    long res = 0;     // Loop to extract the digits of N    // and find their sum    while (N != 0) {         // Extracting the last digit of N        // and adding it to res        res += (N % 10);         // Update N        N /= 10;    }     return res;} // Function to find the minimum increments// required to make the sum of digits of N// less than or equal to S.static long minIncrements(long N, long S){       // If the sum of digits of N is less than    // or equal to S    if (findSum(N) <= S) {         // Output 0        return 0;    }     // variable to access the digits of N    long p = 1;     // Stores the required answer    long ans = 0;     // Loop to access the digits of N    for (int i = 0; i <= 18; ++i) {         // Stores the digit of N starting        // from unit's place        long digit = (N / p) % 10;         // Stores the increment required        // to make the digit 0        long add = p * (10 - digit);         // Update N        N += add;         // Update ans        ans += add;         // If the sum of digits of N is less than        // or equal to S        if (findSum(N) <= S) {             // Break from the loop            break;        }         // Update p to access the next digit        p = p * 10;    }         return ans;} // Driver Codepublic static void Main(){    // Given N and S    long N = 345899211156769;    long S = 20;     // Function call    Console.Write(minIncrements(N, S));}} // This code is contributed by Samim Hossain Mondal

Javascript


Output
100788843231

Time Complexity: O(18*logN).

Auxiliary Space: O(1).

My Personal Notes arrow_drop_up