Largest number smaller than or equal to n and digits in non | Java program for efficient approach to find largest number having digits in non - decreasing order . ; Prints the largest number smaller than s and digits in non - decreasing order . ; array to store digits of number ; conversion of characters of string int number ; variable holds the value of index after which all digits are set 9 ; Checking the condition if the digit is less than its left digit ; If first digit is 0 no need to print it ; Driver code
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 ) ; } }