Open In App

Smallest number greater than or equal to N having sum of digits not exceeding S

Improve
Improve
Like Article
Like
Save
Share
Report

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.

  1. Check if the sum of digits of N does not exceed S, return N.
  2. Initialize a variable, say ans equal to the given integer N and k with 1 to store the powers of 10.
  3. There can be at most 10 digits in the integer range.
  4. Iterate from i = 0 to 8. At each iteration, calculate the last digit as (ans / k)%10.
  5. The sum to make the last digit 0 is k*((10-last_digit)%10). Add it to ans.
  6. 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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate sum
// digits of n
int sum(int n)
{
    int res = 0;
    while (n > 0) {
        res += n % 10;
        n /= 10;
    }
    return res;
}
 
// Function to find the smallest
// possible integer satisfying the
// given condition
int smallestNumber(int n, int s)
{
    // If the sum of digits
    // is already smaller than S
    if (sum(n) <= s) {
        return n;
    }
 
    // Initialize variables
    int ans = n, k = 1;
 
    for (int i = 0; i < 9; ++i) {
 
        // Finding last kth digit
        int digit = (ans / k) % 10;
 
        // Add remaining to make digit 0
        int add = k * ((10 - digit) % 10);
 
        ans += add;
 
        // If sum of digits
        // does not exceed S
        if (sum(ans) <= s) {
            break;
        }
 
        // Update k
        k *= 10;
    }
    return ans;
}
 
// Driver Code
int main()
{
 
    // Given N and S
    int N = 3, S = 2;
 
    // Function call
    cout << smallestNumber(N, S) << endl;
 
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to calculate sum
// digits of n
static int sum(int n)
{
    int res = 0;
    while (n > 0)
    {
        res += n % 10;
        n /= 10;
    }
    return res;
}
 
// Function to find the smallest
// possible integer satisfying the
// given condition
static int smallestNumber(int n, int s)
{
     
    // If the sum of digits
    // is already smaller than S
    if (sum(n) <= s)
    {
        return n;
    }
 
    // Initialize variables
    int ans = n, k = 1;
 
    for(int i = 0; i < 9; ++i)
    {
         
        // Finding last kth digit
        int digit = (ans / k) % 10;
 
        // Add remaining to make digit 0
        int add = k * ((10 - digit) % 10);
 
        ans += add;
 
        // If sum of digits
        // does not exceed S
        if (sum(ans) <= s)
        {
            break;
        }
 
        // Update k
        k *= 10;
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N and S
    int N = 3, S = 2;
 
    // Function call
    System.out.println(smallestNumber(N, S));
}
}
 
// This code is contributed by akhilsaini


Python3




# Python program for the above approach
 
# Function to calculate
# sum of digits of n
def sum(n):
    sm = 0
    while(n > 0):
        sm += n % 10
        n //= 10
    return sm
 
# Function to find the smallest
# possible integer satisfying the
# given condition
def smallestNumber(n, s):
 
# If sum of digits is
# already smaller than s
    if(sum(n) <= s):
        return n
 
# Initialize variables
    ans, k = n, 1
 
    for i in range(9):
 
# Find the k-th digit
        digit = (ans // k) % 10
 
# Add remaining
        add = k * ((10 - digit) % 10)
 
        ans += add
 
# If sum of digits
# does not exceed s
        if(sum(ans) <= s):
            break
 
# Update K
        k *= 10
 
# Return answer
    return ans
 
# Driver Code
 
# Given N and S
n, s = 3, 2
 
# Function call
print(smallestNumber(n, s))


C#




// C# program for the above approach
using System;
 
class GFG{
 
// Function to calculate sum
// digits of n
static int sum(int n)
{
    int res = 0;
    while (n > 0)
    {
        res += n % 10;
        n /= 10;
    }
    return res;
}
 
// Function to find the smallest
// possible integer satisfying the
// given condition
static int smallestNumber(int n, int s)
{
     
    // If the sum of digits
    // is already smaller than S
    if (sum(n) <= s)
    {
        return n;
    }
 
    // Initialize variables
    int ans = n, k = 1;
 
    for(int i = 0; i < 9; ++i)
    {
         
        // Finding last kth digit
        int digit = (ans / k) % 10;
 
        // Add remaining to make digit 0
        int add = k * ((10 - digit) % 10);
 
        ans += add;
 
        // If sum of digits
        // does not exceed S
        if (sum(ans) <= s)
        {
            break;
        }
         
        // Update k
        k *= 10;
    }
    return ans;
}
 
// Driver Code
public static void Main()
{
     
    // Given N and S
    int N = 3, S = 2;
 
    // Function call
    Console.WriteLine(smallestNumber(N, S));
}
}
 
// This code is contributed by akhilsaini


Javascript




<script>
// javascript program for the above approach
 
// Function to calculate sum
// digits of n
function sum(n)
{
    var res = 0;
    while (n > 0)
    {
        res += n % 10;
        n /= 10;
    }
    return res;
}
 
// Function to find the smallest
// possible integer satisfying the
// given condition
function smallestNumber(n , s)
{
     
    // If the sum of digits
    // is already smaller than S
    if (sum(n) <= s)
    {
        return n;
    }
 
    // Initialize variables
    var ans = n, k = 1;
 
    for(i = 0; i < 9; ++i)
    {
         
        // Finding last kth digit
        var digit = (ans / k) % 10;
 
        // Add remaining to make digit 0
        var add = k * ((10 - digit) % 10);
 
        ans += add;
 
        // If sum of digits
        // does not exceed S
        if (sum(ans) <= s)
        {
            break;
        }
 
        // Update k
        k *= 10;
    }
    return ans;
}
 
// Driver Code
 
// Given N and S
var N = 3, S = 2;
 
// Function call
document.write(smallestNumber(N, S));
 
// This code is contributed by shikhasingrajput.
</script>


Output: 

10

 

Time Complexity: O(log210(N)) where N is the given integer.
Space Complexity: O(1)



Last Updated : 13 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads