Largest number smaller than or equal to n and digits in non | 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
< ? 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 ) ; ? >