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++
#include <bits/stdc++.h>
using namespace std;
int findSum( long long int N)
{
int res = 0;
while (N) {
res += (N % 10);
N /= 10;
}
return res;
}
long long int minIncrements( long long int N, int S)
{
if (findSum(N) <= S) {
return 0;
}
long long int p = 1;
long long int ans = 0;
for ( int i = 0; i <= 18; ++i) {
int digit = (N / p) % 10;
long long int add = p * (10 - digit);
N += add;
ans += add;
if (findSum(N) <= S) {
break ;
}
p = p * 10;
}
return ans;
}
int main()
{
long long int N = 345899211156769;
int S = 20;
cout << minIncrements(N, S);
return 0;
}
|
Java
import java.util.*;
public class GFG
{
static int findSum( long N)
{
int res = 0 ;
while (N != 0 ) {
res += (N % 10 );
N /= 10 ;
}
return res;
}
static long minIncrements( long N, int S)
{
if (findSum(N) <= S) {
return 0 ;
}
long p = 1 ;
long ans = 0 ;
for ( int i = 0 ; i <= 18 ; ++i) {
long digit = (N / p) % 10 ;
long add = p * ( 10 - digit);
N += add;
ans += add;
if (findSum(N) <= S) {
break ;
}
p = p * 10 ;
}
return ans;
}
public static void main(String args[])
{
long N = 345899211156769L;
int S = 20 ;
System.out.println(minIncrements(N, S));
}
}
|
Python3
def findSum(N):
res = 0 ;
while (N):
res + = (N % 10 );
N = N / / 10 ;
return res;
def minIncrements(N, S):
if (findSum(N) < = S):
return 0 ;
p = 1 ;
ans = 0 ;
for i in range ( 0 , 18 ):
digit = (N / / p) % 10 ;
add = p * ( 10 - digit);
N + = add;
ans + = add;
if (findSum(N) < = S):
break ;
p = p * 10 ;
return ans;
N = 345899211156769 ;
S = 20 ;
print (minIncrements(N, S))
|
C#
using System;
using System.Collections;
class GFG
{
static long findSum( long N)
{
long res = 0;
while (N != 0) {
res += (N % 10);
N /= 10;
}
return res;
}
static long minIncrements( long N, long S)
{
if (findSum(N) <= S) {
return 0;
}
long p = 1;
long ans = 0;
for ( int i = 0; i <= 18; ++i) {
long digit = (N / p) % 10;
long add = p * (10 - digit);
N += add;
ans += add;
if (findSum(N) <= S) {
break ;
}
p = p * 10;
}
return ans;
}
public static void Main()
{
long N = 345899211156769;
long S = 20;
Console.Write(minIncrements(N, S));
}
}
|
Javascript
<script>
function findSum(N)
{
let res = 0;
while (N) {
res += (N % 10);
N /= 10;
}
return res;
}
function minIncrements(N, S) {
if (findSum(N) <= S) {
return 0;
}
let p = 1;
let ans = 0;
for (let i = 0; i <= 18; ++i) {
let digit = (N / p) % 10;
let add = p * (10 - digit);
N += add;
ans += add;
if (findSum(N) <= S) {
break ;
}
p = p * 10;
}
return ans;
}
let N = 345899211156769;
let S = 20;
document.write(minIncrements(N, S))
</script>
|
Time Complexity: O(18*logN).
Auxiliary Space: O(1).