{"id":27207,"date":"2018-01-03T22:21:45","date_gmt":"2018-01-03T16:51:45","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27207"},"modified":"2018-01-03T22:21:45","modified_gmt":"2018-01-03T16:51:45","slug":"mobile-numeric-keypad-problem","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/mobile-numeric-keypad-problem\/","title":{"rendered":"Mobile Numeric Keypad Problem"},"content":{"rendered":"<p><img decoding=\"async\" class=\"size-full wp-image-27212 alignright\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad.png\" alt=\"Mobile Keypad\" width=\"208\" height=\"155\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad.png 208w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad-74x55.png 74w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad-111x83.png 111w\" sizes=\"(max-width: 208px) 100vw, 208px\" \/><\/p>\n<p>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 # ).<span id=\"more-131515\"><\/span><br \/>\nGiven a number N, find out the number of possible numbers of given length.<\/p>\n<p>Examples:<br \/>\nFor N=1, number of possible numbers would be 10 (0, 1, 2, 3, \u2026., 9)<br \/>\nFor N=2, number of possible numbers would be 36<br \/>\nPossible numbers: 00,08 11,12,14 22,21,23,25 and so on.<br \/>\nIf we start with 0, valid numbers will be 00, 08 (count: 2)<br \/>\nIf we start with 1, valid numbers will be 11, 12, 14 (count: 3)<br \/>\nIf we start with 2, valid numbers will be 22, 21, 23,25 (count: 4)<br \/>\nIf we start with 3, valid numbers will be 33, 32, 36 (count: 3)<br \/>\nIf we start with 4, valid numbers will be 44,41,45,47 (count: 4)<br \/>\nIf we start with 5, valid numbers will be 55,54,52,56,58 (count: 5)<br \/>\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<br \/>\n\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026\u2026<\/p>\n<p>We need to print the count of possible numbers.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>We strongly recommend to minimize the browser and try this yourself first.<\/strong><\/p>\n<p>N = 1 is trivial case, number of possible numbers would be 10 (0, 1, 2, 3, \u2026., 9)<br \/>\nFor N &gt; 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).<\/p>\n<p><strong>Recursive Solution:<\/strong><br \/>\nMobile Keypad is a rectangular grid of 4X3 (4 rows and 3 columns)<br \/>\nLets say Count(i, j, N) represents the count of N length numbers starting from position (i, j)<\/p>\n<pre>If N = 1\r\n  Count(i, j, N) = 10  \r\nElse\r\n  Count(i, j, N) = Sum of all Count(r, c, N-1) where (r, c) is new \r\n                   position after valid move of length 1 from current \r\n                   position (i, j)<\/pre>\n<p>Following is C implementation of above recursive formula.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ A Naive Recursive C program to count number of possible numbers<br\/>\/\/ of given length<br\/>#include &lt;stdio.h&gt;<br\/> <br\/>\/\/ left, up, right, down move from current location<br\/>int row[] = {0, 0, -1, 0, 1};<br\/>int col[] = {0, -1, 0, 1, 0};<br\/> <br\/>\/\/ Returns count of numbers of length n starting from key position<br\/>\/\/ (i, j) in a numeric keyboard.<br\/>int getCountUtil(char keypad[][3], int i, int j, int n)<br\/>{<br\/>    if (keypad == NULL || n &lt;= 0)<br\/>        return 0;<br\/> <br\/>    \/\/ From a given key, only one number is possible of length 1<br\/>    if (n == 1)<br\/>        return 1;<br\/> <br\/>    int k=0, move=0, ro=0, co=0, totalCount = 0;<br\/> <br\/>    \/\/ move left, up, right, down from current location and if<br\/>    \/\/ new location is valid, then get number count of length<br\/>    \/\/ (n-1) from that new position and add in count obtained so far<br\/>    for (move=0; move&lt;5; move++)<br\/>    {<br\/>        ro = i + row[move];<br\/>        co = j + col[move];<br\/>        if (ro &gt;= 0 &amp;&amp; ro &lt;= 3 &amp;&amp; co &gt;=0 &amp;&amp; co &lt;= 2 &amp;&amp;<br\/>           keypad[ro][co] != &#039;*&#039; &amp;&amp; keypad[ro][co] != &#039;#&#039;)<br\/>        {<br\/>            totalCount += getCountUtil(keypad, ro, co, n-1);<br\/>        }<br\/>    }<br\/> <br\/>    return totalCount;<br\/>}<br\/> <br\/>\/\/ Return count of all possible numbers of length n<br\/>\/\/ in a given numeric keyboard<br\/>int getCount(char keypad[][3], int n)<br\/>{<br\/>    \/\/ Base cases<br\/>    if (keypad == NULL || n &lt;= 0)<br\/>        return 0;<br\/>    if (n == 1)<br\/>        return 10;<br\/> <br\/>    int i=0, j=0, totalCount = 0;<br\/>    for (i=0; i&lt;4; i++)  \/\/ Loop on keypad row<br\/>    {<br\/>        for (j=0; j&lt;3; j++)   \/\/ Loop on keypad column<br\/>        {<br\/>            \/\/ Process for 0 to 9 digits<br\/>            if (keypad[i][j] != &#039;*&#039; &amp;&amp; keypad[i][j] != &#039;#&#039;)<br\/>            {<br\/>                \/\/ Get count when number is starting from key<br\/>                \/\/ position (i, j) and add in count obtained so far<br\/>                totalCount += getCountUtil(keypad, i, j, n);<br\/>            }<br\/>        }<br\/>    }<br\/>    return totalCount;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main(int argc, char *argv[])<br\/>{<br\/>   char keypad[4][3] = {{&#039;1&#039;,&#039;2&#039;,&#039;3&#039;},<br\/>                        {&#039;4&#039;,&#039;5&#039;,&#039;6&#039;},<br\/>                        {&#039;7&#039;,&#039;8&#039;,&#039;9&#039;},<br\/>                        {&#039;*&#039;,&#039;0&#039;,&#039;#&#039;}};<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 1, getCount(keypad, 1));<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 2, getCount(keypad, 2));<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 3, getCount(keypad, 3));<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 4, getCount(keypad, 4));<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 5, getCount(keypad, 5));<br\/> <br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Count for numbers of length 1: 10\r\nCount for numbers of length 2: 36\r\nCount for numbers of length 3: 138\r\nCount for numbers of length 4: 532\r\nCount for numbers of length 5: 2062\r\n<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Dynamic Programming<\/strong><br \/>\nThere 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 \u20184\u2019 and \u20188\u2019), we can see there are few repeated traversals for N = 2 (e.g. 4 -&gt; 1, 6 -&gt; 3, 8 -&gt; 9, 8 -&gt; 7 etc).<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-27214 aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad-1.png\" alt=\"Mobile Keypad 1\" width=\"383\" height=\"265\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad-1.png 383w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad-1-300x208.png 300w\" sizes=\"(max-width: 383px) 100vw, 383px\" \/><\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-27215 aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad-2.png\" alt=\"Mobile Keypad 2\" width=\"371\" height=\"263\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad-2.png 371w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/06\/Mobile-Keypad-2-300x213.png 300w\" sizes=\"(max-width: 371px) 100vw, 371px\" \/><\/p>\n<p>Since the problem has both properties: Optimal Substructure and Overlapping Subproblems, it can be efficiently solved using dynamic programming.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p>Following is C program for dynamic programming implementation.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ A Dynamic Programming based C program to count number of<br\/>\/\/ possible numbers of given length<br\/>#include &lt;stdio.h&gt;<br\/> <br\/>\/\/ Return count of all possible numbers of length n<br\/>\/\/ in a given numeric keyboard<br\/>int getCount(char keypad[][3], int n)<br\/>{<br\/>    if(keypad == NULL || n &lt;= 0)<br\/>        return 0;<br\/>    if(n == 1)<br\/>        return 10;<br\/> <br\/>    \/\/ left, up, right, down move from current location<br\/>    int row[] = {0, 0, -1, 0, 1};<br\/>    int col[] = {0, -1, 0, 1, 0};<br\/> <br\/>    \/\/ taking n+1 for simplicity - count[i][j] will store<br\/>    \/\/ number count starting with digit i and length j<br\/>    int count[10][n+1];<br\/>    int i=0, j=0, k=0, move=0, ro=0, co=0, num = 0;<br\/>    int nextNum=0, totalCount = 0;<br\/> <br\/>    \/\/ count numbers starting with digit i and of lengths 0 and 1<br\/>    for (i=0; i&lt;=9; i++)<br\/>    {<br\/>        count[i][0] = 0;<br\/>        count[i][1] = 1;<br\/>    }<br\/> <br\/>    \/\/ Bottom up - Get number count of length 2, 3, 4, ... , n<br\/>    for (k=2; k&lt;=n; k++)<br\/>    {<br\/>        for (i=0; i&lt;4; i++)  \/\/ Loop on keypad row<br\/>        {<br\/>            for (j=0; j&lt;3; j++)   \/\/ Loop on keypad column<br\/>            {<br\/>                \/\/ Process for 0 to 9 digits<br\/>                if (keypad[i][j] != &#039;*&#039; &amp;&amp; keypad[i][j] != &#039;#&#039;)<br\/>                {<br\/>                    \/\/ Here we are counting the numbers starting with<br\/>                    \/\/ digit keypad[i][j] and of length k keypad[i][j]<br\/>                    \/\/ will become 1st digit, and we need to look for<br\/>                    \/\/ (k-1) more digits<br\/>                    num = keypad[i][j] - &#039;0&#039;;<br\/>                    count[num][k] = 0;<br\/> <br\/>                    \/\/ move left, up, right, down from current location<br\/>                    \/\/ and if new location is valid, then get number<br\/>                    \/\/ count of length (k-1) from that new digit and<br\/>                    \/\/ add in count we found so far<br\/>                    for (move=0; move&lt;5; move++)<br\/>                    {<br\/>                        ro = i + row[move];<br\/>                        co = j + col[move];<br\/>                        if (ro &gt;= 0 &amp;&amp; ro &lt;= 3 &amp;&amp; co &gt;=0 &amp;&amp; co &lt;= 2 &amp;&amp;<br\/>                           keypad[ro][co] != &#039;*&#039; &amp;&amp; keypad[ro][co] != &#039;#&#039;)<br\/>                        {<br\/>                            nextNum = keypad[ro][co] - &#039;0&#039;;<br\/>                            count[num][k] += count[nextNum][k-1];<br\/>                        }<br\/>                    }<br\/>                }<br\/>            }<br\/>        }<br\/>    }<br\/> <br\/>    \/\/ Get count of all possible numbers of length &quot;n&quot; starting<br\/>    \/\/ with digit 0, 1, 2, ..., 9<br\/>    totalCount = 0;<br\/>    for (i=0; i&lt;=9; i++)<br\/>        totalCount += count[i][n];<br\/>    return totalCount;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main(int argc, char *argv[])<br\/>{<br\/>   char keypad[4][3] = {{&#039;1&#039;,&#039;2&#039;,&#039;3&#039;},<br\/>                        {&#039;4&#039;,&#039;5&#039;,&#039;6&#039;},<br\/>                        {&#039;7&#039;,&#039;8&#039;,&#039;9&#039;},<br\/>                        {&#039;*&#039;,&#039;0&#039;,&#039;#&#039;}};<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 1, getCount(keypad, 1));<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 2, getCount(keypad, 2));<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 3, getCount(keypad, 3));<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 4, getCount(keypad, 4));<br\/>   printf(&quot;Count for numbers of length %d: %d\\n&quot;, 5, getCount(keypad, 5));<br\/> <br\/>   return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Count for numbers of length 1: 10\r\nCount for numbers of length 2: 36\r\nCount for numbers of length 3: 138\r\nCount for numbers of length 4: 532\r\nCount for numbers of length 5: 2062<\/pre>\n[ad type=&#8221;banner&#8221;]\n<p><strong>A Space Optimized Solution:<\/strong><br \/>\nThe 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.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">\/\/ A Space Optimized C program to count number of possible numbers<br\/>\/\/ of given length<br\/>#include &lt;stdio.h&gt;<br\/> <br\/>\/\/ Return count of all possible numbers of length n<br\/>\/\/ in a given numeric keyboard<br\/>int getCount(char keypad[][3], int n)<br\/>{<br\/>    if(keypad == NULL || n &lt;= 0)<br\/>        return 0;<br\/>    if(n == 1)<br\/>        return 10;<br\/> <br\/>    \/\/ odd[i], even[i] arrays represent count of numbers starting<br\/>    \/\/ with digit i for any length j<br\/>    int odd[10], even[10];<br\/>    int i = 0, j = 0, useOdd = 0, totalCount = 0;<br\/> <br\/>    for (i=0; i&lt;=9; i++)<br\/>        odd[i] = 1;  \/\/ for j = 1<br\/> <br\/>    for (j=2; j&lt;=n; j++) \/\/ Bottom Up calculation from j = 2 to n<br\/>    {<br\/>        useOdd = 1 - useOdd;<br\/> <br\/>        \/\/ Here we are explicitly writing lines for each number 0<br\/>        \/\/ to 9. But it can always be written as DFS on 4X3 grid<br\/>        \/\/ using row, column array valid moves<br\/>        if(useOdd == 1)<br\/>        {<br\/>            even[0] = odd[0] + odd[8];<br\/>            even[1] = odd[1] + odd[2] + odd[4];<br\/>            even[2] = odd[2] + odd[1] + odd[3] + odd[5];<br\/>            even[3] = odd[3] + odd[2] + odd[6];<br\/>            even[4] = odd[4] + odd[1] + odd[5] + odd[7];<br\/>            even[5] = odd[5] + odd[2] + odd[4] + odd[8] + odd[6];<br\/>            even[6] = odd[6] + odd[3] + odd[5] + odd[9];<br\/>            even[7] = odd[7] + odd[4] + odd[8];<br\/>            even[8] = odd[8] + odd[0] + odd[5] + odd[7] + odd[9];<br\/>            even[9] = odd[9] + odd[6] + odd[8];<br\/>        }<br\/>        else<br\/>        {<br\/>            odd[0] = even[0] + even[8];<br\/>            odd[1] = even[1] + even[2] + even[4];<br\/>            odd[2] = even[2] + even[1] + even[3] + even[5];<br\/>            odd[3] = even[3] + even[2] + even[6];<br\/>            odd[4] = even[4] + even[1] + even[5] + even[7];<br\/>            odd[5] = even[5] + even[2] + even[4] + even[8] + even[6];<br\/>            odd[6] = even[6] + even[3] + even[5] + even[9];<br\/>            odd[7] = even[7] + even[4] + even[8];<br\/>            odd[8] = even[8] + even[0] + even[5] + even[7] + even[9];<br\/>            odd[9] = even[9] + even[6] + even[8];<br\/>        }<br\/>    }<br\/> <br\/>    \/\/ Get count of all possible numbers of length &quot;n&quot; starting<br\/>    \/\/ with digit 0, 1, 2, ..., 9<br\/>    totalCount = 0;<br\/>    if(useOdd == 1)<br\/>    {<br\/>        for (i=0; i&lt;=9; i++)<br\/>            totalCount += even[i];<br\/>    }<br\/>    else<br\/>    {<br\/>        for (i=0; i&lt;=9; i++)<br\/>            totalCount += odd[i];<br\/>    }<br\/>    return totalCount;<br\/>}<br\/> <br\/>\/\/ Driver program to test above function<br\/>int main()<br\/>{<br\/>    char keypad[4][3] = {{&#039;1&#039;,&#039;2&#039;,&#039;3&#039;},<br\/>        {&#039;4&#039;,&#039;5&#039;,&#039;6&#039;},<br\/>        {&#039;7&#039;,&#039;8&#039;,&#039;9&#039;},<br\/>        {&#039;*&#039;,&#039;0&#039;,&#039;#&#039;}<br\/>    };<br\/>    printf(&quot;Count for numbers of length %d: %d\\n&quot;, 1, getCount(keypad, 1));<br\/>    printf(&quot;Count for numbers of length %d: %d\\n&quot;, 2, getCount(keypad, 2));<br\/>    printf(&quot;Count for numbers of length %d: %d\\n&quot;, 3, getCount(keypad, 3));<br\/>    printf(&quot;Count for numbers of length %d: %d\\n&quot;, 4, getCount(keypad, 4));<br\/>    printf(&quot;Count for numbers of length %d: %d\\n&quot;, 5, getCount(keypad, 5));<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p>Output:<\/p>\n<pre>Count for numbers of length 1: 10\r\nCount for numbers of length 2: 36\r\nCount for numbers of length 3: 138\r\nCount for numbers of length 4: 532\r\nCount for numbers of length 5: 2062<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Mobile Numeric Keypad Problem &#8211; Dynamic Programming Given the mobile numeric keypad. You can only press buttons that are up, left, right or down <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,70145,83849],"tags":[61375,61366,72847,81075,72842,72845,70483,72848,81074,81076,81094,81098,81097,81091,72855,72853],"class_list":["post-27207","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-dynamic-programming","category-mobile-numeric-keypad-problem","tag-best-keypad-mobile","tag-best-keypad-phone","tag-concept-of-dynamic-programming","tag-count-of-possible-words-from-mobile-keypad","tag-define-dynamic-programming","tag-definition-of-dynamic-programming","tag-dynamic-programming","tag-dynamic-programming-code-generation-algorithm","tag-mobile-numeric-keypad-problem","tag-mobile-numeric-keypad-problem-solution-in-java","tag-mobile-phones-with-keypad","tag-mobile-with-keypad","tag-nokia-keypad-mobile","tag-nokia-keypad-phones","tag-simple-dynamic-programming-example","tag-types-of-dynamic-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27207","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27207"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27207\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}