Mobile Numeric Keypad Problem | Set 2 | Java implementation of the approach ; Function to return the count of numbers possible ; Array of list storing possible direction for each number from 0 to 9 list [ i ] stores possible moves from index i ; Initializing list ; Storing values for n = 1 ; To store the values for n = i ; Loop to iterate through each index ; For each move possible from j Increment the value of possible move positions by Arr [ j ] ; Update Arr [ ] for next iteration ; Find the count of numbers possible ; Driver code
class GFG { static int MAX = 10 ; static int getCount ( int n ) { int [ ] [ ] list = new int [ MAX ] [ ] ; list [ 0 ] = new int [ ] { 0 , 8 } ; list [ 1 ] = new int [ ] { 1 , 2 , 4 } ; list [ 2 ] = new int [ ] { 2 , 1 , 3 , 5 } ; list [ 3 ] = new int [ ] { 3 , 6 , 2 } ; list [ 4 ] = new int [ ] { 4 , 1 , 7 , 5 } ; list [ 5 ] = new int [ ] { 5 , 4 , 6 , 2 , 8 } ; list [ 6 ] = new int [ ] { 6 , 3 , 5 , 9 } ; list [ 7 ] = new int [ ] { 7 , 4 , 8 } ; list [ 8 ] = new int [ ] { 8 , 5 , 0 , 7 , 9 } ; list [ 9 ] = new int [ ] { 9 , 6 , 8 } ; int Arr [ ] = new int [ ] { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int i = 2 ; i <= n ; i ++ ) { int Arr2 [ ] = new int [ MAX ] ; for ( int j = 0 ; j < MAX ; j ++ ) { for ( int x = 0 ; x < list [ j ] . length ; x ++ ) { Arr2 [ list [ j ] [ x ] ] += Arr [ j ] ; } } for ( int j = 0 ; j < MAX ; j ++ ) Arr [ j ] = Arr2 [ j ] ; } int sum = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) sum += Arr [ i ] ; return sum ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( getCount ( n ) ) ; } }