There is a standard numeric keypad on a mobile phone. You can only press the current button or buttons that are directly up, left, right, or down from it (for ex if you press 5, then pressing 2, 4, 6 & 8 are allowed). Diagonal movements and pressing the bottom row corner buttons (* and #) are prohibited.Given a number n, find the number of possible unique sequences of length n that you can create by pressing buttons. You can start from any digit.
Question
There is a standard numeric keypad on a mobile phone. You can only press the current button or buttons that are directly up, left, right, or down from it (for ex if you press 5, then pressing 2, 4, 6 & 8 are allowed). Diagonal movements and pressing the bottom row corner buttons (* and #) are prohibited.Given a number n, find the number of possible unique sequences of length n that you can create by pressing buttons. You can start from any digit.
Solution
This problem can be solved using dynamic programming. Here are the steps:
-
First, we need to define the keypad layout. We can represent it as a 2D array:
int keypad[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {-1, 0, -1}};Here, -1 represents the * and # keys which are not allowed.
-
Next, we need to define a 3D array to store the number of sequences of a given length starting from a particular key. The dimensions of the array will be 10 (for the 10 digits), n+1 (for sequence lengths from 0 to n), and 5 (for the 5 possible moves - stay, up, down, left, right). Initialize this array to -1 to indicate that we haven't computed any values yet.
-
Now, we define a recursive function that takes the current position (i, j) on the keypad, the remaining length of the sequence, and the 3D array as input. This function will return the number of sequences of the given length starting from the current key.
-
In this function, first we check if the remaining length is 0. If it is, we return 1 because there is exactly one sequence of length 0 (the empty sequence).
-
If the remaining length is not 0, we check if we have already computed the number of sequences for the current key and remaining length. If we have, we return the computed value.
-
If we haven't computed the value yet, we calculate it by making all possible moves from the current key and recursively calling the function for the new key and remaining length minus 1. We add up all these values to get the total number of sequences.
-
Finally, we store the computed value in the 3D array and return it.
-
To get the total number of sequences of length n, we call the function for each key on the keypad and add up the results.
This algorithm runs in O(n) time and uses O(n) space.
Similar Questions
There is a standard numeric keypad on a mobile phone. You can only press the current button or buttons that are directly up, left, right, or down from it (for ex if you press 5, then pressing 2, 4, 6 & 8 are allowed). Diagonal movements and pressing the bottom row corner buttons (* and #) are prohibited.Given a number n, find the number of possible unique sequences of length n that you can create by pressing buttons. You can start from any digit.
Aarithra’s teacher asked her to practice the keystrokes of numeric keypad. She noticed that the keypad works in dual mode. Identify the key that helps in changing the mode from numeric to arrow keys:*1 pointScroll LockCaps LockNum LockAlt Key
______________ is an input device used to enter characters and functions into the computer system by pressing buttons.*1 pointMouseKeyboardTypewriterOffice buttonsNumeric keys are also called___________*1 pointfunction keysspecial keyscalculator keysalphanumeric keysThe following are examples of special keys except *1 pointF10 keyShift keyCtrl keyTab keyWhich keys are located at the central part of the keyboard*1 pointkeyboardAlphanumeric keysArrow keysEsc keysWhich key is use to scroll up and down your computer environment*1 pointAlt keysShift keyArrow keysF1 - F12 keys
On a computer keyboard, keys ___ and ___ are called guide keys.
Which laptop feature allows to overcome standard keyboard size restrictions? Touchpad Numeric keypad Fn key Digitizer
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.