Print a number strictly less than a given number such that all its digits are distinct . | Function to find a number less than n such that all its digits are distinct ; looping through numbers less than n ; initializing a hash array ; creating a copy of i ; initializing variables to compare lengths of digits ; counting frequency of the digits ; checking if each digit is present once ; Driver code
< ? php function findNumber ( $ n ) { for ( $ i = $ n - 1 ; $ i >= 0 ; $ i -- ) { $ count = array_fill ( 0 , 10 , 0 ) ; $ x = $ i ; $ count1 = 0 ; $ count2 = 0 ; while ( $ x ) { $ count [ $ x % 10 ] ++ ; $ x = ( int ) ( $ x / 10 ) ; $ count1 ++ ; } for ( $ j = 0 ; $ j < 10 ; $ j ++ ) { if ( $ count [ $ j ] == 1 ) $ count2 ++ ; } if ( $ count1 == $ count2 ) return $ i ; } } $ n = 8490 ; echo findNumber ( $ n ) ; ? >