Smallest number greater than or equal to N using only digits 1 to K

Given a number N and an integer K, the task is to find the smallest number greater than or equal to N, formed using only first K non-zero digits( 1, 2, …, K-1, K).

Examples:

Input: N = 124, K = 3
Output: 131
Explanation:
The smallest number greater than or equal to 124, which is only made of digits 1, 2, 3 is 131.

Input: N = 325242, K = 4
Output: 331111

Naive Approach:
The simplest solution is to start a for loop from N + 1 and find the first number made up of first K digits.



Efficient Solution:

  • To obtain an efficient solution, we need to understand the fact that a maximum of 9 digit number can be formed up-to 1010. So, we will iterate over digits of the number in reverse and check:

    1. If current digit >= K then, make that digit = 1.
    2. If current digit < K and there is no digit greater than K after this, then increment the digit by 1 and copy all the remaining digits as it is.
  • Once we have iterated over all the digits and still haven’t found any digit which is less than K then we need to add a digit (1) to the answer.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find the smallest
// number greater than or equal
// to N which is made up of
// first K digits
#include <bits/stdc++.h>
using namespace std;
  
// Function to count the
// digits greater than K
int CountGreater(int n, int k)
{
    int a = 0;
    while (n) {
        if ((n % 10) > k) {
            a++;
        }
        n = n / 10;
    }
    return a;
}
  
// Function to print the list
int PrintList(list<int> ans)
{
    for (auto it = ans.begin();
         it != ans.end(); it++)
        cout << *it;
}
  
// Function to find the number
// greater than or equal to n,
// which is only made of first
// k digits
void getNumber(int n, int k)
{
    int count = CountGreater(n, k);
  
    // If the number itself
    // satisfy the conditions
    if (count == 0) {
        cout << n;
        return;
    }
  
    list<int> ans;
    bool changed = false;
  
    // Check digit from back
    while (n > 0) {
        int digit = n % 10;
        if (changed == true) {
            ans.push_front(digit);
        }
        else {
            // If digit > K is
            // present previously and
            // current digit is less
            // than K
            if (count == 0 && digit < k) {
                ans.push_front(digit + 1);
                changed = true;
            }
            else {
                ans.push_front(1);
                // If current digit is
                // greater than K
                if (digit > k) {
                    count--;
                }
            }
        }
        n = n / 10;
    }
  
    // If an extra digit needs
    // to be added
    if (changed == false) {
        ans.push_front(1);
    }
  
    // Print the number
    PrintList(ans);
  
    return;
}
  
// Driver Code
int main()
{
    int N = 51234;
    int K = 4;
    getNumber(N, K);
    return 0;
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the smallest 
# number greater than or equal 
# to N which is made up of 
# first K digits 
  
# Function to count the 
# digits greater than K 
def CountGreater(n, k):
  
    a = 0
    while (n > 0):
        if ((n % 10) > k):
            a += 1
              
        n = n // 10
  
    return a
  
# Function to print the list
def PrintList (ans):
  
    for i in ans:
        print(i, end = '')
  
# Function to find the number 
# greater than or equal to n, 
# which is only made of first 
# k digits 
def getNumber(n, k):
  
    count = CountGreater(n, k)
      
    # If the number itself 
    # satisfy the conditions 
    if (count == 0):
        print(n)
        return
  
    ans = []
    changed = False
  
    # Check digit from back
    while (n > 0):
        digit = n % 10
        if (changed == True):
            ans.insert(0, digit)
  
        else:
              
            # If digit > K is 
            # present previously and 
            # current digit is less 
            # than K
            if (count == 0 and digit < k):
                ans.insert(0, digit + 1)
                changed = True
  
            else:
                ans.insert(0, 1)
                  
                # If current digit is 
                # greater than K
                if (digit > k):
                    count -= 1
  
        n = n // 10
  
    # If an extra digit needs 
    # to be added 
    if (changed == False):
        ans.insert(0, 1)
  
    # Print the number
    PrintList(ans)
    return
  
# Driver Code
N = 51234
K = 4
  
getNumber(N, K)
  
# This code is contributed by himanshu77

chevron_right


Output:

111111

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

Check out this Author's contributed articles.

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.



Improved By : himanshu77