Count of N digit Numbers having no pair of equal consecutive Digits | C # Program to implement of the above approach ; Function to count the number of N - digit numbers with no equal pair of consecutive digits ; Base Case ; Calculate the total count of valid ( i - 1 ) - digit numbers ; Update [ , ] dp table ; Calculate the count of required N - digit numbers ; Driver Code
using System ; class GFG { public static void count ( int N ) { if ( N == 1 ) { Console . WriteLine ( 10 ) ; return ; } int [ , ] dp = new int [ N , 10 ] ; for ( int i = 1 ; i < 10 ; i ++ ) dp [ 0 , i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { int temp = 0 ; for ( int j = 0 ; j < 10 ; j ++ ) temp += dp [ i - 1 , j ] ; for ( int j = 0 ; j < 10 ; j ++ ) dp [ i , j ] = temp - dp [ i - 1 , j ] ; } int ans = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) ans += dp [ N - 1 , i ] ; Console . WriteLine ( ans ) ; } public static void Main ( String [ ] args ) { int N = 2 ; count ( N ) ; } }