Count of N | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_numbers ( int k , int n , bool flag ) { if ( n == 1 ) { if ( flag ) { return ( k - 1 ) ; } else { return 1 ; } } if ( flag ) return ( k - 1 ) * ( count_numbers ( k , n - 1 , 0 ) + count_numbers ( k , n - 1 , 1 ) ) ; else return count_numbers ( k , n - 1 , 1 ) ; } int main ( ) { int n = 3 ; int k = 10 ; cout << count_numbers ( k , n , true ) ; return 0 ; }