Given a number n, find the Largest number smaller than or equal to n and digits in non-decreasing order.
Examples:
Input : n = 200
Output : 199
If the given number is 200, the largest
number which is smaller or equal to it
having digits in non decreasing order is
199.
Input : n = 139
Output : 139
Method 1 (Brute Force)
Start from n, for every number check if its digits are in non decreasing order. If yes, then return. Else check for the next number until we find the result.
C++
#include<bits/stdc++.h>
using namespace std;
long long nondecdigits( long long n)
{
long long int x = 0;
for (x=n; x>=1; x--)
{
int no = x;
int prev_dig = 11;
bool flag = true ;
while (no != 0)
{
if (prev_dig < no%10)
{
flag = false ;
break ;
}
prev_dig = no % 10;
no /= 10;
}
if (flag == true )
break ;
}
return x;
}
int main()
{
long long n = 200;
cout << nondecdigits(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int nondecdigits( int n)
{
int x = 0 ;
for (x = n; x >= 1 ; x--)
{
int no = x;
int prev_dig = 11 ;
boolean flag = true ;
while (no != 0 )
{
if (prev_dig < no % 10 )
{
flag = false ;
break ;
}
prev_dig = no % 10 ;
no /= 10 ;
}
if (flag == true )
break ;
}
return x;
}
public static void main (String[] args)
{
int n = 200 ;
System.out.println (nondecdigits(n));
}
}
|
Python3
def nondecdigits( n):
x = 0
for x in range (n, 0 , - 1 ):
no = x
prev_dig = 11
flag = True
while (no ! = 0 ):
if (prev_dig < no % 10 ):
flag = False
break
prev_dig = no % 10
no / / = 10
if (flag = = True ):
break
return x
if __name__ = = "__main__" :
n = 200
print (nondecdigits(n))
|
C#
using System;
class GFG
{
static int nondecdigits( int n)
{
int x = 0;
for (x = n; x >= 1; x--)
{
int no = x;
int prev_dig = 11;
bool flag = true ;
while (no != 0)
{
if (prev_dig < no % 10)
{
flag = false ;
break ;
}
prev_dig = no % 10;
no /= 10;
}
if (flag == true )
break ;
}
return x;
}
static public void Main ()
{
int n = 200;
Console.WriteLine(nondecdigits(n));
}
}
|
PHP
<?php
function nondecdigits( $n )
{
$x = 0;
for ( $x = $n ; $x >= 1; $x --)
{
$no = $x ;
$prev_dig = 11;
$flag = true;
while ( $no != 0)
{
if ( $prev_dig < $no %10)
{
$flag = false;
break ;
}
$prev_dig = $no % 10;
$no /= 10;
}
if ( $flag == true)
break ;
}
return $x ;
}
$n = 200;
echo nondecdigits( $n );
?>
|
Javascript
<script>
function nondecdigits(n)
{
let x = 0;
for (x = n; x >= 1; x--)
{
let no = x;
let prev_dig = 11;
let flag = true ;
while (no != 0)
{
if (prev_dig < no % 10)
{
flag = false ;
break ;
}
prev_dig = no % 10;
no = Math.floor(no / 10);
}
if (flag == true )
break ;
}
return x;
}
let n = 200;
document.write(nondecdigits(n));
</script>
|
Output:
199
Efficient approach
The method discussed above is not much efficient as would only give results for numbers upto 10^5, but if the number is very big such that it contains 10^5 digits.
So, we will discuss an another method for such big numbers.
Step 1: Store the digits of the number in array or a vector.
Step 2: Start traversing the array from the digit from rightmost position to leftmost in given number.
Step 3: If a digit is greater than the digit in the right to it, note the index of that digit in that array and decrease that digit by one.
Step 4 : Keep updating that index until you completely traverse the array accordingly as discussed in step 3.
Step 4: Set all the digits right to that index as 9 .
Step 5 : Print the array as this is the required number.
Suppose the number is 200 the digits will be 2, 0, 0. The index at which leftmost digit is greater than the right digit is index 1 (following 1-index) so the number at index 1 will be 2 – 1 = 1 and all the digits right to it will be 9. So the final array will be 1, 9, 9. And the required number will be 199.
C++
#include<bits/stdc++.h>
using namespace std;
void nondecdigits(string s)
{
long long m = s.size();
long long a[m];
for ( long long i=0; i<m; i++)
a[i] = s[i] - '0' ;
long long level = m-1;
for ( long long i=m-1; i>0; i--)
{
if (a[i] < a[i-1])
{
a[i-1]--;
level=i-1;
}
}
if (a[0] != 0)
{
for ( long long i=0; i<=level; i++)
cout << a[i];
for ( long long i=level+1; i<m; i++)
cout << "9" ;
}
else
{
for ( long long i=1; i<level; i++)
cout << a[i];
for ( long long i=level+1; i<m; i++)
cout << "9" ;
}
}
int main()
{
string n = "200" ;
nondecdigits(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void nondecdigits(String s)
{
int m = s.length();
int [] a = new int [m + 1 ];
for ( int i = 0 ; i < m; i++)
a[i] = ( int )s.charAt(i) - ( int ) '0' ;
int level = m - 1 ;
for ( int i = m - 1 ; i > 0 ; i--)
{
if (a[i] < a[i - 1 ])
{
a[i - 1 ]--;
level = i - 1 ;
}
}
if (a[ 0 ] != 0 )
{
for ( int i = 0 ; i <= level; i++)
System.out.print(a[i]);
for ( int i = level + 1 ; i < m; i++)
System.out.print( "9" );
}
else
{
for ( int i = 1 ; i < level; i++)
System.out.print(a[i]);
for ( int i = level + 1 ; i < m; i++)
System.out.print( "9" );
}
}
public static void main(String[] args)
{
String n = "200" ;
nondecdigits(n);
}
}
|
Python3
def nondecdigits(s):
m = len (s);
a = [ 0 ] * m;
for i in range (m):
a[i] = ord (s[i]) - ord ( '0' );
level = m - 1 ;
for i in range (m - 1 , 0 , - 1 ):
if (a[i] < a[i - 1 ]):
a[i - 1 ] - = 1 ;
level = i - 1 ;
if (a[ 0 ] ! = 0 ):
for i in range (level + 1 ):
print (a[i], end = "");
for i in range (level + 1 , m):
print ( "9" , end = "");
else :
for i in range ( 1 , level):
print (a[i], end = "");
for i in range (level + 1 , m):
print ( "9" , end = "");
n = "200" ;
nondecdigits(n);
|
C#
using System;
class GFG
{
static void nondecdigits( string s)
{
int m = s.Length;
int [] a = new int [m + 1];
for ( int i = 0; i < m; i++)
a[i] = ( int )s[i] - ( int ) '0' ;
int level = m - 1;
for ( int i = m - 1; i > 0; i--)
{
if (a[i] < a[i - 1])
{
a[i - 1]--;
level = i - 1;
}
}
if (a[0] != 0)
{
for ( int i = 0; i <= level; i++)
Console.Write(a[i]);
for ( int i = level + 1; i < m; i++)
Console.Write( "9" );
}
else
{
for ( int i = 1; i < level; i++)
Console.Write(a[i]);
for ( int i = level + 1; i < m; i++)
Console.Write( "9" );
}
}
static void Main()
{
string n = "200" ;
nondecdigits(n);
}
}
|
PHP
<?php
function nondecdigits( $s )
{
$m = strlen ( $s );
$a [ $m ] = 0;
for ( $i = 0; $i < $m ; $i ++)
$a [ $i ] = $s [ $i ] - '0' ;
$level = $m - 1;
for ( $i = $m - 1; $i > 0; $i --)
{
if ( $a [ $i ] < $a [ $i - 1])
{
$a [ $i - 1]--;
$level = $i - 1;
}
}
if ( $a [0] != 0)
{
for ( $i = 0;
$i <= $level ; $i ++)
echo $a [ $i ];
for ( $i = $level + 1;
$i < $m ; $i ++)
echo "9" ;
}
else
{
for ( $i = 1; $i < $level ; $i ++)
echo $a [ $i ];
for ( $i = $level + 1;
$i < $m ; $i ++)
echo "9" ;
}
}
$n = "200" ;
nondecdigits( $n );
?>
|
Javascript
<script>
function nondecdigits(s)
{
var m = s.length;
var a = Array.from({length: m + 1}, (_, i) => 0);
for (i = 0; i < m; i++)
a[i] = s.charAt(i).charCodeAt(0) -
'0' .charCodeAt(0);
var level = m - 1;
for (i = m - 1; i > 0; i--)
{
if (a[i] < a[i - 1])
{
a[i - 1]--;
level = i - 1;
}
}
if (a[0] != 0)
{
for (i = 0; i <= level; i++)
document.write(a[i]);
for (i = level + 1; i < m; i++)
document.write( "9" );
}
else
{
for (i = 1; i < level; i++)
document.write(a[i]);
for (i = level + 1; i < m; i++)
document.write( "9" );
}
}
var n = "200" ;
nondecdigits(n);
</script>
|
Output:
199
Time Complexity Time complexity is O(d) where d is no. of digits in the number.
This article is contributed by Ayush Jha. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.