Count of N | Java implementation of the approach ; Function to return the count of n - digit numbers that satisfy the given conditions ; Base case ; If 0 wasn 't chosen previously ; If 0 wasn 't chosen previously ; Driver code
class GFG { static int count_numbers ( int k , int n , boolean flag ) { if ( n == 1 ) { if ( flag ) { return ( k - 1 ) ; } else { return 1 ; } } if ( flag ) return ( k - 1 ) * ( count_numbers ( k , n - 1 , false ) + count_numbers ( k , n - 1 , true ) ) ; else return count_numbers ( k , n - 1 , true ) ; } public static void main ( String [ ] args ) { int n = 3 ; int k = 10 ; System . out . println ( count_numbers ( k , n , true ) ) ; } }