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:
- If current digit >= K then, make that digit = 1.
- 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 <bits/stdc++.h>using namespace std;// Function to count the// digits greater than Kint CountGreater(int n, int k){ int a = 0; while (n) { if ((n % 10) > k) { a++; } n = n / 10; } return a;}// Function to print the listint 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 digitsvoid 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 Codeint 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 digitsimport java.util.*;class GFG{// Function to count the// digits greater than Kstatic 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 liststatic void PrintList(List<Integer> 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 digitsstatic 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<Integer> 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 Codepublic 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 Kdef CountGreater(n, k): a = 0 while (n > 0): if ((n % 10) > k): a += 1 n = n // 10 return a# Function to print the listdef 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 digitsdef 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 CodeN = 51234K = 4getNumber(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 digitsusing System;using System.Collections.Generic;class GFG{// Function to count the// digits greater than Kstatic 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 liststatic 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 digitsstatic 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 Codepublic static void Main(String[] args){ int N = 51234; int K = 4; getNumber(N, K);}}// This code is contributed by Princi Singh |
111111
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


