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:
- 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++
#include <bits/stdc++.h>
using namespace std;
int CountGreater( int n, int k)
{
int a = 0;
while (n) {
if ((n % 10) > k) {
a++;
}
n = n / 10;
}
return a;
}
int PrintList(list< int > ans)
{
for ( auto it = ans.begin();
it != ans.end(); it++)
cout << *it;
}
void getNumber( int n, int k)
{
int count = CountGreater(n, k);
if (count == 0) {
cout << n;
return ;
}
list< int > ans;
bool changed = false ;
while (n > 0) {
int digit = n % 10;
if (changed == true ) {
ans.push_front(digit);
}
else {
if (count == 0 && digit < k) {
ans.push_front(digit + 1);
changed = true ;
}
else {
ans.push_front(1);
if (digit > k) {
count--;
}
}
}
n = n / 10;
}
if (changed == false ) {
ans.push_front(1);
}
PrintList(ans);
return ;
}
int main()
{
int N = 51234;
int K = 4;
getNumber(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int CountGreater( int n, int k)
{
int a = 0 ;
while (n > 0 )
{
if ((n % 10 ) > k)
{
a++;
}
n = n / 10 ;
}
return a;
}
static void PrintList(List<Integer> ans)
{
for ( int it : ans)
System.out.print(it);
}
static void getNumber( int n, int k)
{
int count = CountGreater(n, k);
if (count == 0 )
{
System.out.print(n);
return ;
}
List<Integer> ans = new LinkedList<>();
boolean changed = false ;
while (n > 0 )
{
int digit = n % 10 ;
if (changed == true )
{
ans.add( 0 , digit);
}
else
{
if (count == 0 && digit < k)
{
ans.add( 0 , digit + 1 );
changed = true ;
}
else
{
ans.add( 0 , 1 );
if (digit > k)
{
count--;
}
}
}
n = n / 10 ;
}
if (changed == false )
{
ans.add( 0 , 1 );
}
PrintList(ans);
return ;
}
public static void main(String[] args)
{
int N = 51234 ;
int K = 4 ;
getNumber(N, K);
}
}
|
Python3
def CountGreater(n, k):
a = 0
while (n > 0 ):
if ((n % 10 ) > k):
a + = 1
n = n / / 10
return a
def PrintList (ans):
for i in ans:
print (i, end = '')
def getNumber(n, k):
count = CountGreater(n, k)
if (count = = 0 ):
print (n)
return
ans = []
changed = False
while (n > 0 ):
digit = n % 10
if (changed = = True ):
ans.insert( 0 , digit)
else :
if (count = = 0 and digit < k):
ans.insert( 0 , digit + 1 )
changed = True
else :
ans.insert( 0 , 1 )
if (digit > k):
count - = 1
n = n / / 10
if (changed = = False ):
ans.insert( 0 , 1 )
PrintList(ans)
return
N = 51234
K = 4
getNumber(N, K)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int CountGreater( int n, int k)
{
int a = 0;
while (n > 0)
{
if ((n % 10) > k)
{
a++;
}
n = n / 10;
}
return a;
}
static void PrintList(List< int > ans)
{
foreach ( int it in ans)
Console.Write(it);
}
static void getNumber( int n, int k)
{
int count = CountGreater(n, k);
if (count == 0)
{
Console.Write(n);
return ;
}
List< int > ans = new List< int >();
bool changed = false ;
while (n > 0)
{
int digit = n % 10;
if (changed == true )
{
ans.Insert(0, digit);
}
else
{
if (count == 0 && digit < k)
{
ans.Insert(0, digit + 1);
changed = true ;
}
else
{
ans.Insert(0, 1);
if (digit > k)
{
count--;
}
}
}
n = n / 10;
}
if (changed == false )
{
ans.Insert(0, 1);
}
PrintList(ans);
return ;
}
public static void Main(String[] args)
{
int N = 51234;
int K = 4;
getNumber(N, K);
}
}
|
Javascript
function CountGreater(n, k)
{
let a = 0;
while (n > 0) {
if ((n % 10) > k) {
a++;
}
n = Math.floor(n / 10);
}
return a;
}
function PrintList(ans)
{
console.log(ans.join( "" ))
}
function getNumber(n, k)
{
let count = CountGreater(n, k);
if (count == 0) {
process.stdout.write( "" + n);
return ;
}
let ans = [];
let changed = false ;
while (n > 0) {
let digit = n % 10;
if (changed == true ) {
ans.unshift(digit);
}
else {
if (count == 0 && digit < k) {
ans.unshift(digit + 1);
changed = true ;
}
else {
ans.unshift(1);
if (digit > k) {
count--;
}
}
}
n = Math.floor(n / 10);
}
if (changed == false ) {
ans.unshift(1);
}
PrintList(ans);
return ;
}
let N = 51234;
let K = 4;
getNumber(N, K);
|
Time Complexity: O(log10N)
Space Complexity: O(N) as ans list has been created.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...