Given integer N and integer S, the task is to find the smallest number greater than or equal to N such that the sum of its digits does not exceed S.
Examples:
Input: N = 3, S = 2
Output: 10
Explanation: Sum of digits of 10 is 1, which is less than 2.
Input: N = 19, S = 3
Output: 20
Explanation: Sum of digits of 20 is 2, which is less than 3.
Approach: The problem can be solved using a greedy approach. Follow the below steps to solve the problem.
- Check if the sum of digits of N does not exceed S, return N.
- Initialize a variable, say ans equal to the given integer N and k with 1 to store the powers of 10.
- There can be at most 10 digits in the integer range.
- Iterate from i = 0 to 8. At each iteration, calculate the last digit as (ans / k)%10.
- The sum to make the last digit 0 is k*((10-last_digit)%10). Add it to ans.
- Check the sum of digits of ans. If it does not exceed S, print ans and break. Otherwise, update k as k = k*10 and repeat the above steps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int sum( int n)
{
int res = 0;
while (n > 0) {
res += n % 10;
n /= 10;
}
return res;
}
int smallestNumber( int n, int s)
{
if (sum(n) <= s) {
return n;
}
int ans = n, k = 1;
for ( int i = 0; i < 9; ++i) {
int digit = (ans / k) % 10;
int add = k * ((10 - digit) % 10);
ans += add;
if (sum(ans) <= s) {
break ;
}
k *= 10;
}
return ans;
}
int main()
{
int N = 3, S = 2;
cout << smallestNumber(N, S) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG{
static int sum( int n)
{
int res = 0 ;
while (n > 0 )
{
res += n % 10 ;
n /= 10 ;
}
return res;
}
static int smallestNumber( int n, int s)
{
if (sum(n) <= s)
{
return n;
}
int ans = n, k = 1 ;
for ( int i = 0 ; i < 9 ; ++i)
{
int digit = (ans / k) % 10 ;
int add = k * (( 10 - digit) % 10 );
ans += add;
if (sum(ans) <= s)
{
break ;
}
k *= 10 ;
}
return ans;
}
public static void main(String[] args)
{
int N = 3 , S = 2 ;
System.out.println(smallestNumber(N, S));
}
}
|
Python3
def sum (n):
sm = 0
while (n > 0 ):
sm + = n % 10
n / / = 10
return sm
def smallestNumber(n, s):
if ( sum (n) < = s):
return n
ans, k = n, 1
for i in range ( 9 ):
digit = (ans / / k) % 10
add = k * (( 10 - digit) % 10 )
ans + = add
if ( sum (ans) < = s):
break
k * = 10
return ans
n, s = 3 , 2
print (smallestNumber(n, s))
|
C#
using System;
class GFG{
static int sum( int n)
{
int res = 0;
while (n > 0)
{
res += n % 10;
n /= 10;
}
return res;
}
static int smallestNumber( int n, int s)
{
if (sum(n) <= s)
{
return n;
}
int ans = n, k = 1;
for ( int i = 0; i < 9; ++i)
{
int digit = (ans / k) % 10;
int add = k * ((10 - digit) % 10);
ans += add;
if (sum(ans) <= s)
{
break ;
}
k *= 10;
}
return ans;
}
public static void Main()
{
int N = 3, S = 2;
Console.WriteLine(smallestNumber(N, S));
}
}
|
Javascript
<script>
function sum(n)
{
var res = 0;
while (n > 0)
{
res += n % 10;
n /= 10;
}
return res;
}
function smallestNumber(n , s)
{
if (sum(n) <= s)
{
return n;
}
var ans = n, k = 1;
for (i = 0; i < 9; ++i)
{
var digit = (ans / k) % 10;
var add = k * ((10 - digit) % 10);
ans += add;
if (sum(ans) <= s)
{
break ;
}
k *= 10;
}
return ans;
}
var N = 3, S = 2;
document.write(smallestNumber(N, S));
</script>
|
Time Complexity: O(log210(N)) where N is the given integer.
Space Complexity: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
13 Apr, 2021
Like Article
Save Article