Count of N digit Numbers having no pair of equal consecutive Digits | Java 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
import java . util . * ; class GFG { public static void count ( int N ) { if ( N == 1 ) { System . out . println ( 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 ] ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 2 ; count ( N ) ; } }