
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Print all n digit patterns formed by mobile Keypad in C++
In this problem, we are given a number n and we have to print all N digit patterns formed by pressing the mobile keypad button. While pressing the buttons, we can press only nearby buttons of the current button i.e. only keys left, right, up, down can be pressed.
Let’s see how the old keypad looks like −
1 | 2 ABC | 3 DEF |
4 GHI | 5 JKL | 6 MNO |
7 PQRS | 8 TUV | 9 WXYZ |
* | 0 | # |
Let’s take an example to understand the problem
Input: n=2 Output: 12 14 21 23 25 32 36 41 45 47 52 54 56 58 63 65 69 74 78 85 87 89 80 96 98
To solve this problem, we will use a depth-first search (DFS). in DFS, we will select one by one all keys of our keypad as the first digit of the number. Now, we will generate the rest digits of the number by using DFS (which allows left, right, up or down key stokes).
Example
Program to implement the above solution −
#include <bits/stdc++.h> using namespace std; bool isSafe(int x, int y, bool Visited[][3]) { return (x >= 0 && x < 4 && y >= 0 && y < 3 && !Visited[x][y]); } void searchNumber(bool visited[][3], int Keypad[][3], int n, string pattern, int x, int y) { pattern.push_back((Keypad[x][y] + '0')); if (pattern.size() == n) { cout<<pattern<<"\t"; return; } static int row[] = { 0, 1, 0, -1 }; static int col[] = { 1, 0, -1, 0 }; visited[x][y] = true; for (int k = 0; k < 4; k++) if (isSafe(x + row[k], y + col[k], visited) && Keypad[x + row[k]][y + col[k]] != -1) searchNumber(visited, Keypad, n, pattern, x + row[k], y + col[k]); visited[x][y] = false; pattern.pop_back(); } void GenerateNDigitNumber(int Keypad[][3], int n) { bool visited[4][3]; memset(visited, false, sizeof(visited)); for (int i = 0; i < 4; i++) for (int j = 0; j < 3; j++) if (Keypad[i][j] != -1) searchNumber(visited, Keypad, n, "", i, j); } int main() { int Keypad[4][3] ={ { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, { -1, 0, -1 } }; int n = 2; cout<<"All "<<n<<" digit number generated from keypad are :\n"; GenerateNDigitNumber(Keypad, n); return 0; }
Output
All 2 digit number generated from keypad are − 12 14 23 25 21 36 32 45 47 41 56 58 54 52 69 65 63 78 74 89 80 87 85 98 96 08
- Related Articles
- Mobile Numeric Keypad Problem\n
- Print all n-digit strictly increasing numbers in C++
- How to get all the combinations of the keypad value in a mobile by backtracking using C#?
- N digit numbers divisible by 5 formed from the M digits in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Convert a sentence into its equivalent mobile numeric keypad sequence in C++
- Print all distinct integers that can be formed by K numbers from a given array of N numbers in C++
- Print all safe primes below N in C++
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Print all 3 digit repeating numbers in a very large number in C++
- Python program to print sorted number formed by merging all elements in array
- Print all Proth primes up to N in C++
- Print all possible strings of length k that can be formed from a set of n characters in C++
- Print the nearest prime number formed by adding prime numbers to N
- Count n digit numbers divisible by given number in C++

Advertisements