 Open in App
Not now

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

• Last Updated : 22 Feb, 2023

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 the first K digits.

Efficient Solution:

• To obtain an efficient solution, we need to understand the fact that a maximum of 9-digit numbers 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 the 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++

 `// C++ Program to find the smallest``// number greater than or equal``// to N which is made up of``// first K digits``#include ``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;``}`

## Java

 `// Java program to find the smallest``// number greater than or equal``// to N which is made up of``// first K digits``import` `java.util.*;` `class` `GFG{` `// Function to count the``// digits greater than K``static` `int` `CountGreater(``int` `n, ``int` `k)``{``    ``int` `a = ``0``;``    ` `    ``while` `(n > ``0``)``    ``{``        ``if` `((n % ``10``) > k)``        ``{``            ``a++;``        ``}``        ``n = n / ``10``;``    ``}``    ``return` `a;``}` `// Function to print the list``static` `void` `PrintList(List ans)``{``    ``for``(``int` `it : ans)``        ``System.out.print(it);``}` `// Function to find the number``// greater than or equal to n,``// which is only made of first``// k digits``static` `void` `getNumber(``int` `n, ``int` `k)``{``    ``int` `count = CountGreater(n, k);` `    ``// If the number itself``    ``// satisfy the conditions``    ``if` `(count == ``0``)``    ``{``        ``System.out.print(n);``        ``return``;``    ``}` `    ``List ans = ``new` `LinkedList<>();``    ``boolean` `changed = ``false``;` `    ``// Check digit from back``    ``while` `(n > ``0``)``    ``{``        ``int` `digit = n % ``10``;``        ` `        ``if` `(changed == ``true``)``        ``{``            ``ans.add(``0``, digit);``        ``}``        ``else``        ``{``            ` `            ``// If digit > K is``            ``// present previously and``            ``// current digit is less``            ``// than K``            ``if` `(count == ``0` `&& digit < k)``            ``{``                ``ans.add(``0``, digit + ``1``);``                ``changed = ``true``;``            ``}``            ``else``            ``{``                ``ans.add(``0``, ``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.add(``0``, ``1``);``    ``}` `    ``// Print the number``    ``PrintList(ans);` `    ``return``;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``51234``;``    ``int` `K = ``4``;``    ` `    ``getNumber(N, K);``}``}` `// This code is contributed by Amit Katiyar`

## Python3

 `# 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`

## C#

 `// C# program to find the smallest``// number greater than or equal``// to N which is made up of``// first K digits``using` `System;``using` `System.Collections.Generic;``class` `GFG{` `// Function to count the``// digits greater than K``static` `int` `CountGreater(``int` `n, ``int` `k)``{``  ``int` `a = 0;` `  ``while` `(n > 0)``  ``{``    ``if` `((n % 10) > k)``    ``{``      ``a++;``    ``}``    ``n = n / 10;``  ``}``  ``return` `a;``}` `// Function to print the list``static` `void` `PrintList(List<``int``> ans)``{``  ``foreach``(``int` `it ``in` `ans)``    ``Console.Write(it);``}` `// Function to find the number``// greater than or equal to n,``// which is only made of first``// k digits``static` `void` `getNumber(``int` `n, ``int` `k)``{``  ``int` `count = CountGreater(n, k);` `  ``// If the number itself``  ``// satisfy the conditions``  ``if` `(count == 0)``  ``{``    ``Console.Write(n);``    ``return``;``  ``}` `  ``List<``int``> ans = ``new` `List<``int``>();``  ``bool` `changed = ``false``;` `  ``// Check digit from back``  ``while` `(n > 0)``  ``{``    ``int` `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 && 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--;``        ``}``      ``}``    ``}``    ``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``public` `static` `void` `Main(String[] args)``{``  ``int` `N = 51234;``  ``int` `K = 4;``  ``getNumber(N, K);``}``}` `// This code is contributed by Princi Singh`

## Javascript

 `// JS 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``function` `CountGreater(n, k)``{``    ``let a = 0;``    ``while` `(n > 0) {``        ``if` `((n % 10) > k) {``            ``a++;``        ``}``        ``n = Math.floor(n / 10);``    ``}``    ``return` `a;``}` `// Function to print the list``function` `PrintList(ans)``{``    ``console.log(ans.join(``""``))``}` `// Function to find the number``// greater than or equal to n,``// which is only made of first``// k digits``function` `getNumber(n, k)``{``    ``let count = CountGreater(n, k);` `    ``// If the number itself``    ``// satisfy the conditions``    ``if` `(count == 0) {``        ``process.stdout.write(``""` `+ n);``        ``return``;``    ``}` `    ``let ans = [];``    ``let changed = ``false``;` `    ``// Check digit from back``    ``while` `(n > 0) {``        ``let digit = n % 10;``        ``if` `(changed == ``true``) {``            ``ans.unshift(digit);``        ``}``        ``else` `{``            ``// If digit > K is``            ``// present previously and``            ``// current digit is less``            ``// than K``            ``if` `(count == 0 && digit < k) {``                ``ans.unshift(digit + 1);``                ``changed = ``true``;``            ``}``            ``else` `{``                ``ans.unshift(1);``                ``// If current digit is``                ``// greater than K``                ``if` `(digit > k) {``                    ``count--;``                ``}``            ``}``        ``}``        ``n = Math.floor(n / 10);``    ``}` `    ``// If an extra digit needs``    ``// to be added``    ``if` `(changed == ``false``) {``        ``ans.unshift(1);``    ``}` `    ``// Print the number``    ``PrintList(ans);` `    ``return``;``}` `// Driver Code``let N = 51234;``let K = 4;``getNumber(N, K);` `// This code is contributed by phasing17.`

Output:

`111111`

Time Complexity: O(log10N)

Space Complexity: O(N) as ans list has been created.

My Personal Notes arrow_drop_up