Count natural numbers whose all permutation are greater than that number | C # program to count the number less than N , whose all permutation is greater than or equal to the number . ; Return the count of the number having all permutation greater than or equal to the number . ; Pushing 1 to 9 because all number from 1 to 9 have this property . ; take a number from stack and add a digit smaller than last digit of it . ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int countNumber ( int n ) { int result = 0 ; Stack < int > s = new Stack < int > ( ) ; for ( int i = 1 ; i <= 9 ; i ++ ) { if ( i <= n ) { s . Push ( i ) ; result ++ ; } while ( s . Count != 0 ) { int tp = s . Peek ( ) ; s . Pop ( ) ; for ( int j = tp % 10 ; j <= 9 ; j ++ ) { int x = tp * 10 + j ; if ( x <= n ) { s . Push ( x ) ; result ++ ; } } } } return result ; } public static void Main ( String [ ] args ) { int n = 15 ; Console . WriteLine ( countNumber ( n ) ) ; } }