Mobile Keypad

Given the mobile numeric keypad. You can only press buttons that are up, left, right or down to the current button. You are not allowed to press bottom row corner buttons (i.e. * and # ).
Given a number N, find out the number of possible numbers of given length.

Examples:
For N=1, number of possible numbers would be 10 (0, 1, 2, 3, …., 9)
For N=2, number of possible numbers would be 36
Possible numbers: 00,08 11,12,14 22,21,23,25 and so on.
If we start with 0, valid numbers will be 00, 08 (count: 2)
If we start with 1, valid numbers will be 11, 12, 14 (count: 3)
If we start with 2, valid numbers will be 22, 21, 23,25 (count: 4)
If we start with 3, valid numbers will be 33, 32, 36 (count: 3)
If we start with 4, valid numbers will be 44,41,45,47 (count: 4)
If we start with 5, valid numbers will be 55,54,52,56,58 (count: 5)
………………………………
………………………………

We need to print the count of possible numbers.

[ad type=”banner”]

We strongly recommend to minimize the browser and try this yourself first.

N = 1 is trivial case, number of possible numbers would be 10 (0, 1, 2, 3, …., 9)
For N > 1, we need to start from some button, then move to any of the four direction (up, left, right or down) which takes to a valid button (should not go to *, #). Keep doing this until N length number is obtained (depth first traversal).

Recursive Solution:
Mobile Keypad is a rectangular grid of 4X3 (4 rows and 3 columns)
Lets say Count(i, j, N) represents the count of N length numbers starting from position (i, j)

If N = 1
  Count(i, j, N) = 10  
Else
  Count(i, j, N) = Sum of all Count(r, c, N-1) where (r, c) is new 
                   position after valid move of length 1 from current 
                   position (i, j)

Following is C implementation of above recursive formula.

[pastacode lang=”c” manual=”%2F%2F%20A%20Naive%20Recursive%20C%20program%20to%20count%20number%20of%20possible%20numbers%0A%2F%2F%20of%20given%20length%0A%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20left%2C%20up%2C%20right%2C%20down%20move%20from%20current%20location%0Aint%20row%5B%5D%20%3D%20%7B0%2C%200%2C%20-1%2C%200%2C%201%7D%3B%0Aint%20col%5B%5D%20%3D%20%7B0%2C%20-1%2C%200%2C%201%2C%200%7D%3B%0A%20%0A%2F%2F%20Returns%20count%20of%20numbers%20of%20length%20n%20starting%20from%20key%20position%0A%2F%2F%20(i%2C%20j)%20in%20a%20numeric%20keyboard.%0Aint%20getCountUtil(char%20keypad%5B%5D%5B3%5D%2C%20int%20i%2C%20int%20j%2C%20int%20n)%0A%7B%0A%20%20%20%20if%20(keypad%20%3D%3D%20NULL%20%7C%7C%20n%20%3C%3D%200)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%0A%20%20%20%20%2F%2F%20From%20a%20given%20key%2C%20only%20one%20number%20is%20possible%20of%20length%201%0A%20%20%20%20if%20(n%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20return%201%3B%0A%20%0A%20%20%20%20int%20k%3D0%2C%20move%3D0%2C%20ro%3D0%2C%20co%3D0%2C%20totalCount%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20move%20left%2C%20up%2C%20right%2C%20down%20from%20current%20location%20and%20if%0A%20%20%20%20%2F%2F%20new%20location%20is%20valid%2C%20then%20get%20number%20count%20of%20length%0A%20%20%20%20%2F%2F%20(n-1)%20from%20that%20new%20position%20and%20add%20in%20count%20obtained%20so%20far%0A%20%20%20%20for%20(move%3D0%3B%20move%3C5%3B%20move%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20ro%20%3D%20i%20%2B%20row%5Bmove%5D%3B%0A%20%20%20%20%20%20%20%20co%20%3D%20j%20%2B%20col%5Bmove%5D%3B%0A%20%20%20%20%20%20%20%20if%20(ro%20%3E%3D%200%20%26%26%20ro%20%3C%3D%203%20%26%26%20co%20%3E%3D0%20%26%26%20co%20%3C%3D%202%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20keypad%5Bro%5D%5Bco%5D%20!%3D%20’*’%20%26%26%20keypad%5Bro%5D%5Bco%5D%20!%3D%20’%23′)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20totalCount%20%2B%3D%20getCountUtil(keypad%2C%20ro%2C%20co%2C%20n-1)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20return%20totalCount%3B%0A%7D%0A%20%0A%2F%2F%20Return%20count%20of%20all%20possible%20numbers%20of%20length%20n%0A%2F%2F%20in%20a%20given%20numeric%20keyboard%0Aint%20getCount(char%20keypad%5B%5D%5B3%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Base%20cases%0A%20%20%20%20if%20(keypad%20%3D%3D%20NULL%20%7C%7C%20n%20%3C%3D%200)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20if%20(n%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20return%2010%3B%0A%20%0A%20%20%20%20int%20i%3D0%2C%20j%3D0%2C%20totalCount%20%3D%200%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%3C4%3B%20i%2B%2B)%20%20%2F%2F%20Loop%20on%20keypad%20row%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(j%3D0%3B%20j%3C3%3B%20j%2B%2B)%20%20%20%2F%2F%20Loop%20on%20keypad%20column%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Process%20for%200%20to%209%20digits%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(keypad%5Bi%5D%5Bj%5D%20!%3D%20’*’%20%26%26%20keypad%5Bi%5D%5Bj%5D%20!%3D%20’%23′)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Get%20count%20when%20number%20is%20starting%20from%20key%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20position%20(i%2C%20j)%20and%20add%20in%20count%20obtained%20so%20far%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20totalCount%20%2B%3D%20getCountUtil(keypad%2C%20i%2C%20j%2C%20n)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%20%20return%20totalCount%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main(int%20argc%2C%20char%20*argv%5B%5D)%0A%7B%0A%20%20%20char%20keypad%5B4%5D%5B3%5D%20%3D%20%7B%7B’1’%2C’2’%2C’3’%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B’4’%2C’5’%2C’6’%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B’7’%2C’8’%2C’9’%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B’*’%2C’0’%2C’%23’%7D%7D%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%201%2C%20getCount(keypad%2C%201))%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%202%2C%20getCount(keypad%2C%202))%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%203%2C%20getCount(keypad%2C%203))%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%204%2C%20getCount(keypad%2C%204))%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%205%2C%20getCount(keypad%2C%205))%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

Count for numbers of length 1: 10
Count for numbers of length 2: 36
Count for numbers of length 3: 138
Count for numbers of length 4: 532
Count for numbers of length 5: 2062
[ad type=”banner”]

Dynamic Programming
There are many repeated traversal on smaller paths (traversal for smaller N) to find all possible longer paths (traversal for bigger N). See following two diagrams for example. In this traversal, for N = 4 from two starting positions (buttons ‘4’ and ‘8’), we can see there are few repeated traversals for N = 2 (e.g. 4 -> 1, 6 -> 3, 8 -> 9, 8 -> 7 etc).

Mobile Keypad 1

Mobile Keypad 2

Since the problem has both properties: Optimal Substructure and Overlapping Subproblems, it can be efficiently solved using dynamic programming.

[ad type=”banner”]

Following is C program for dynamic programming implementation.

[pastacode lang=”c” manual=”%2F%2F%20A%20Dynamic%20Programming%20based%20C%20program%20to%20count%20number%20of%0A%2F%2F%20possible%20numbers%20of%20given%20length%0A%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20Return%20count%20of%20all%20possible%20numbers%20of%20length%20n%0A%2F%2F%20in%20a%20given%20numeric%20keyboard%0Aint%20getCount(char%20keypad%5B%5D%5B3%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20if(keypad%20%3D%3D%20NULL%20%7C%7C%20n%20%3C%3D%200)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20if(n%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20return%2010%3B%0A%20%0A%20%20%20%20%2F%2F%20left%2C%20up%2C%20right%2C%20down%20move%20from%20current%20location%0A%20%20%20%20int%20row%5B%5D%20%3D%20%7B0%2C%200%2C%20-1%2C%200%2C%201%7D%3B%0A%20%20%20%20int%20col%5B%5D%20%3D%20%7B0%2C%20-1%2C%200%2C%201%2C%200%7D%3B%0A%20%0A%20%20%20%20%2F%2F%20taking%20n%2B1%20for%20simplicity%20-%20count%5Bi%5D%5Bj%5D%20will%20store%0A%20%20%20%20%2F%2F%20number%20count%20starting%20with%20digit%20i%20and%20length%20j%0A%20%20%20%20int%20count%5B10%5D%5Bn%2B1%5D%3B%0A%20%20%20%20int%20i%3D0%2C%20j%3D0%2C%20k%3D0%2C%20move%3D0%2C%20ro%3D0%2C%20co%3D0%2C%20num%20%3D%200%3B%0A%20%20%20%20int%20nextNum%3D0%2C%20totalCount%20%3D%200%3B%0A%20%0A%20%20%20%20%2F%2F%20count%20numbers%20starting%20with%20digit%20i%20and%20of%20lengths%200%20and%201%0A%20%20%20%20for%20(i%3D0%3B%20i%3C%3D9%3B%20i%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20count%5Bi%5D%5B0%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20count%5Bi%5D%5B1%5D%20%3D%201%3B%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Bottom%20up%20-%20Get%20number%20count%20of%20length%202%2C%203%2C%204%2C%20…%20%2C%20n%0A%20%20%20%20for%20(k%3D2%3B%20k%3C%3Dn%3B%20k%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(i%3D0%3B%20i%3C4%3B%20i%2B%2B)%20%20%2F%2F%20Loop%20on%20keypad%20row%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(j%3D0%3B%20j%3C3%3B%20j%2B%2B)%20%20%20%2F%2F%20Loop%20on%20keypad%20column%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Process%20for%200%20to%209%20digits%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(keypad%5Bi%5D%5Bj%5D%20!%3D%20’*’%20%26%26%20keypad%5Bi%5D%5Bj%5D%20!%3D%20’%23′)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Here%20we%20are%20counting%20the%20numbers%20starting%20with%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20digit%20keypad%5Bi%5D%5Bj%5D%20and%20of%20length%20k%20keypad%5Bi%5D%5Bj%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20will%20become%201st%20digit%2C%20and%20we%20need%20to%20look%20for%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20(k-1)%20more%20digits%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20num%20%3D%20keypad%5Bi%5D%5Bj%5D%20-%20’0’%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20count%5Bnum%5D%5Bk%5D%20%3D%200%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20move%20left%2C%20up%2C%20right%2C%20down%20from%20current%20location%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20and%20if%20new%20location%20is%20valid%2C%20then%20get%20number%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20count%20of%20length%20(k-1)%20from%20that%20new%20digit%20and%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20add%20in%20count%20we%20found%20so%20far%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20for%20(move%3D0%3B%20move%3C5%3B%20move%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ro%20%3D%20i%20%2B%20row%5Bmove%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20co%20%3D%20j%20%2B%20col%5Bmove%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(ro%20%3E%3D%200%20%26%26%20ro%20%3C%3D%203%20%26%26%20co%20%3E%3D0%20%26%26%20co%20%3C%3D%202%20%26%26%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20keypad%5Bro%5D%5Bco%5D%20!%3D%20’*’%20%26%26%20keypad%5Bro%5D%5Bco%5D%20!%3D%20’%23′)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20nextNum%20%3D%20keypad%5Bro%5D%5Bco%5D%20-%20’0’%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20count%5Bnum%5D%5Bk%5D%20%2B%3D%20count%5BnextNum%5D%5Bk-1%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Get%20count%20of%20all%20possible%20numbers%20of%20length%20%22n%22%20starting%0A%20%20%20%20%2F%2F%20with%20digit%200%2C%201%2C%202%2C%20…%2C%209%0A%20%20%20%20totalCount%20%3D%200%3B%0A%20%20%20%20for%20(i%3D0%3B%20i%3C%3D9%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20totalCount%20%2B%3D%20count%5Bi%5D%5Bn%5D%3B%0A%20%20%20%20return%20totalCount%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main(int%20argc%2C%20char%20*argv%5B%5D)%0A%7B%0A%20%20%20char%20keypad%5B4%5D%5B3%5D%20%3D%20%7B%7B’1’%2C’2’%2C’3’%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B’4’%2C’5’%2C’6’%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B’7’%2C’8’%2C’9’%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B’*’%2C’0’%2C’%23’%7D%7D%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%201%2C%20getCount(keypad%2C%201))%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%202%2C%20getCount(keypad%2C%202))%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%203%2C%20getCount(keypad%2C%203))%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%204%2C%20getCount(keypad%2C%204))%3B%0A%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%205%2C%20getCount(keypad%2C%205))%3B%0A%20%0A%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

Count for numbers of length 1: 10
Count for numbers of length 2: 36
Count for numbers of length 3: 138
Count for numbers of length 4: 532
Count for numbers of length 5: 2062
[ad type=”banner”]

A Space Optimized Solution:
The above dynamic programming approach also runs in O(n) time and requires O(n) auxiliary space, as only one for loop runs n times, other for loops runs for constant time. We can see that nth iteration needs data from (n-1)th iteration only, so we need not keep the data from older iterations. We can have a space efficient dynamic programming approach with just two arrays of size 10. Thanks to Nik for suggesting this solution.

[pastacode lang=”c” manual=”%2F%2F%20A%20Space%20Optimized%20C%20program%20to%20count%20number%20of%20possible%20numbers%0A%2F%2F%20of%20given%20length%0A%23include%20%3Cstdio.h%3E%0A%20%0A%2F%2F%20Return%20count%20of%20all%20possible%20numbers%20of%20length%20n%0A%2F%2F%20in%20a%20given%20numeric%20keyboard%0Aint%20getCount(char%20keypad%5B%5D%5B3%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20if(keypad%20%3D%3D%20NULL%20%7C%7C%20n%20%3C%3D%200)%0A%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20if(n%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20return%2010%3B%0A%20%0A%20%20%20%20%2F%2F%20odd%5Bi%5D%2C%20even%5Bi%5D%20arrays%20represent%20count%20of%20numbers%20starting%0A%20%20%20%20%2F%2F%20with%20digit%20i%20for%20any%20length%20j%0A%20%20%20%20int%20odd%5B10%5D%2C%20even%5B10%5D%3B%0A%20%20%20%20int%20i%20%3D%200%2C%20j%20%3D%200%2C%20useOdd%20%3D%200%2C%20totalCount%20%3D%200%3B%0A%20%0A%20%20%20%20for%20(i%3D0%3B%20i%3C%3D9%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20odd%5Bi%5D%20%3D%201%3B%20%20%2F%2F%20for%20j%20%3D%201%0A%20%0A%20%20%20%20for%20(j%3D2%3B%20j%3C%3Dn%3B%20j%2B%2B)%20%2F%2F%20Bottom%20Up%20calculation%20from%20j%20%3D%202%20to%20n%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20useOdd%20%3D%201%20-%20useOdd%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Here%20we%20are%20explicitly%20writing%20lines%20for%20each%20number%200%0A%20%20%20%20%20%20%20%20%2F%2F%20to%209.%20But%20it%20can%20always%20be%20written%20as%20DFS%20on%204X3%20grid%0A%20%20%20%20%20%20%20%20%2F%2F%20using%20row%2C%20column%20array%20valid%20moves%0A%20%20%20%20%20%20%20%20if(useOdd%20%3D%3D%201)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B0%5D%20%3D%20odd%5B0%5D%20%2B%20odd%5B8%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B1%5D%20%3D%20odd%5B1%5D%20%2B%20odd%5B2%5D%20%2B%20odd%5B4%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B2%5D%20%3D%20odd%5B2%5D%20%2B%20odd%5B1%5D%20%2B%20odd%5B3%5D%20%2B%20odd%5B5%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B3%5D%20%3D%20odd%5B3%5D%20%2B%20odd%5B2%5D%20%2B%20odd%5B6%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B4%5D%20%3D%20odd%5B4%5D%20%2B%20odd%5B1%5D%20%2B%20odd%5B5%5D%20%2B%20odd%5B7%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B5%5D%20%3D%20odd%5B5%5D%20%2B%20odd%5B2%5D%20%2B%20odd%5B4%5D%20%2B%20odd%5B8%5D%20%2B%20odd%5B6%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B6%5D%20%3D%20odd%5B6%5D%20%2B%20odd%5B3%5D%20%2B%20odd%5B5%5D%20%2B%20odd%5B9%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B7%5D%20%3D%20odd%5B7%5D%20%2B%20odd%5B4%5D%20%2B%20odd%5B8%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B8%5D%20%3D%20odd%5B8%5D%20%2B%20odd%5B0%5D%20%2B%20odd%5B5%5D%20%2B%20odd%5B7%5D%20%2B%20odd%5B9%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20even%5B9%5D%20%3D%20odd%5B9%5D%20%2B%20odd%5B6%5D%20%2B%20odd%5B8%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B0%5D%20%3D%20even%5B0%5D%20%2B%20even%5B8%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B1%5D%20%3D%20even%5B1%5D%20%2B%20even%5B2%5D%20%2B%20even%5B4%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B2%5D%20%3D%20even%5B2%5D%20%2B%20even%5B1%5D%20%2B%20even%5B3%5D%20%2B%20even%5B5%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B3%5D%20%3D%20even%5B3%5D%20%2B%20even%5B2%5D%20%2B%20even%5B6%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B4%5D%20%3D%20even%5B4%5D%20%2B%20even%5B1%5D%20%2B%20even%5B5%5D%20%2B%20even%5B7%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B5%5D%20%3D%20even%5B5%5D%20%2B%20even%5B2%5D%20%2B%20even%5B4%5D%20%2B%20even%5B8%5D%20%2B%20even%5B6%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B6%5D%20%3D%20even%5B6%5D%20%2B%20even%5B3%5D%20%2B%20even%5B5%5D%20%2B%20even%5B9%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B7%5D%20%3D%20even%5B7%5D%20%2B%20even%5B4%5D%20%2B%20even%5B8%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B8%5D%20%3D%20even%5B8%5D%20%2B%20even%5B0%5D%20%2B%20even%5B5%5D%20%2B%20even%5B7%5D%20%2B%20even%5B9%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20odd%5B9%5D%20%3D%20even%5B9%5D%20%2B%20even%5B6%5D%20%2B%20even%5B8%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Get%20count%20of%20all%20possible%20numbers%20of%20length%20%22n%22%20starting%0A%20%20%20%20%2F%2F%20with%20digit%200%2C%201%2C%202%2C%20…%2C%209%0A%20%20%20%20totalCount%20%3D%200%3B%0A%20%20%20%20if(useOdd%20%3D%3D%201)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(i%3D0%3B%20i%3C%3D9%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20totalCount%20%2B%3D%20even%5Bi%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20for%20(i%3D0%3B%20i%3C%3D9%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20%20%20%20%20totalCount%20%2B%3D%20odd%5Bi%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%20return%20totalCount%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20char%20keypad%5B4%5D%5B3%5D%20%3D%20%7B%7B’1’%2C’2’%2C’3’%7D%2C%0A%20%20%20%20%20%20%20%20%7B’4’%2C’5’%2C’6’%7D%2C%0A%20%20%20%20%20%20%20%20%7B’7’%2C’8’%2C’9’%7D%2C%0A%20%20%20%20%20%20%20%20%7B’*’%2C’0’%2C’%23’%7D%0A%20%20%20%20%7D%3B%0A%20%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%201%2C%20getCount(keypad%2C%201))%3B%0A%20%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%202%2C%20getCount(keypad%2C%202))%3B%0A%20%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%203%2C%20getCount(keypad%2C%203))%3B%0A%20%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%204%2C%20getCount(keypad%2C%204))%3B%0A%20%20%20%20printf(%22Count%20for%20numbers%20of%20length%20%25d%3A%20%25d%5Cn%22%2C%205%2C%20getCount(keypad%2C%205))%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”C” highlight=”” provider=”manual”/]

Output:

Count for numbers of length 1: 10
Count for numbers of length 2: 36
Count for numbers of length 3: 138
Count for numbers of length 4: 532
Count for numbers of length 5: 2062
[ad type=”banner”]