Given an integer N, the task is to find the closest number to N which is greater than N and contains at most one non-zero digit.
Examples:
Input: N = 540
Output: 600
Explanation: Since the number 600 contains only one non-zero digit, it is the required output is 600.
Input: N = 1000
Output: 2000
Approach: The problem can be solved based on the following observations.

Follow the steps below to solve the problem:
- Initialize a variable, say, ctr to store the count of digits in N.
- Compute the value of power(10, ctr – 1)
- Print the value of the above-mentioned formula as the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int power( int X, int n) {
int res = 1;
while (n) {
if (n & 1)
res = res * X;
X = X * X;
n = n >> 1;
}
return res;
}
int closestgtNum( int N) {
int n = log10 (N) + 1;
int P = power(10, n - 1);
int Y = N % P;
int res = N + (P - Y);
return res;
}
int main()
{
int N = 120;
cout<<closestgtNum(N);
}
|
Java
import java.io.*;
class GFG{
static int power( int X, int n)
{
int res = 1 ;
while (n != 0 )
{
if ((n & 1 ) != 0 )
res = res * X;
X = X * X;
n = n >> 1 ;
}
return res;
}
static int closestgtNum( int N)
{
int n = ( int ) Math.log10(N) + 1 ;
int P = power( 10 , n - 1 );
int Y = N % P;
int res = N + (P - Y);
return res;
}
public static void main (String[] args)
{
int N = 120 ;
System.out.print(closestgtNum(N));
}
}
|
Python3
import math
def power(X, n):
res = 1
while (n ! = 0 ):
if (n & 1 ! = 0 ):
res = res * X
X = X * X
n = n >> 1
return res
def closestgtNum(N):
n = int (math.log10(N) + 1 )
P = power( 10 , n - 1 )
Y = N % P
res = N + (P - Y)
return res
N = 120
print (closestgtNum(N))
|
C#
using System;
class GFG{
static int power( int X, int n)
{
int res = 1;
while (n != 0)
{
if ((n & 1) != 0)
res = res * X;
X = X * X;
n = n >> 1;
}
return res;
}
static int closestgtNum( int N)
{
int n = ( int ) Math.Log10(N) + 1;
int P = power(10, n - 1);
int Y = N % P;
int res = N + (P - Y);
return res;
}
public static void Main ()
{
int N = 120;
Console.Write(closestgtNum(N));
}
}
|
Javascript
<script>
function power(X, n)
{
var res = 1;
while (n != 0)
{
if ((n & 1) != 0)
res = res * X;
X = X * X;
n = n >> 1;
}
return res;
}
function closestgtNum(N)
{
var n = parseInt( Math.log10(N) + 1);
var P = power(10, n - 1);
var Y = N % P;
var res = N + (P - Y);
return res;
}
var N = 120;
document.write(closestgtNum(N));
</script>
|
Time Complexity: O(log2N)
Auxiliary Space: O(log10N)
Efficient Approach:The idea is to increment the value of the first digit of the given integer by 1 and initialize the resultant string to the first digit of the given integer. Finally, append (N – 1) 0s at the end of the resultant string and return the resultant string.
- Initialize a string, say res to store the closest greater number with st most one non-zero digit.
- First append the value str[0] + 1 at the resultant string and then append (N – 1) 0s at the end of resultant string.
- Print the value of res
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string closestgtNum(string str)
{
string res = "" ;
int n = str.length();
if (str[0] < '9' ) {
res.push_back(str[0] + 1);
}
else {
res.push_back( '1' );
res.push_back( '0' );
}
for ( int i = 0; i < n - 1; i++)
{
res.push_back( '0' );
}
return res;
}
int main()
{
string str = "120" ;
cout<<closestgtNum(str);
}
|
Java
import java.util.*;
class GFG{
static String closestgtNum(String str)
{
String res = "" ;
int n = str.length();
if (str.charAt( 0 ) < '9' )
{
res += ( char )(str.charAt( 0 ) + 1 );
}
else
{
res += ( char )( '1' );
res += ( char )( '0' );
}
for ( int i = 0 ; i < n - 1 ; i++)
{
res += ( char )( '0' );
}
return res;
}
public static void main(String[] args)
{
String str = "120" ;
System.out.print(closestgtNum(str));
}
}
|
Python3
def closestgtNum( str ):
res = "";
n = len ( str );
if ( str [ 0 ] < '9' ):
res + = ( chr )( ord ( str [ 0 ]) + 1 );
else :
res + = ( chr )( '1' );
res + = ( chr )( '0' );
for i in range (n - 1 ):
res + = ( '0' );
return res;
if __name__ = = '__main__' :
str = "120" ;
print (closestgtNum( str ));
|
C#
using System;
class GFG{
public static string closestgtNum( string str)
{
string res = "" ;
int n = str.Length;
if (str[0] < '9' )
{
res = res + ( char )(str[0] + 1);
}
else
{
res = res + '1' ;
res = res + '0' ;
}
for ( int i = 0; i < n - 1; i++)
{
res = res + '0' ;
}
return res;
}
static void Main()
{
string str = "120" ;
Console.WriteLine(closestgtNum(str));
}
}
|
Javascript
<script>
function closestgtNum(str)
{
var res = "" ;
var n = str.length;
if (str[0] < '9' )
{
res = res + String.fromCharCode(str[0].charCodeAt(0) + 1);
}
else
{
res = res + '1' ;
res = res + '0' ;
}
for ( var i = 0; i < n - 1; i++)
{
res = res + '0' ;
}
return res;
}
var str = "120" ;
document.write(closestgtNum(str));
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(log10N)