Mobile numeric keypad | Practice | GeeksforGeeks

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, the task is to find out the number of possible numbers of the given length.
Example 1:
Input: 1
Output: 10
Explanation: Number of possible numbers 
would be 10 (0, 1, 2, 3, ...., 9)  
Input: N = 2
Output: 36
Explanation: 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) 
and so on..

Your Task:  
You don't need to read input or print anything. Complete the function getCount() which takes N as input parameter and returns the integer value

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 25
0
harrypotter01 month ago
dic = {1:(1,2,4),
2:(1,3,2,5),
3:(3,6,2),
4:(1,5,4,7),
5:(5,4,2,8,6),
6:(6,5,3,9),
7:(7,4,8),
8:(5,7,9,0,8),
9:(6,8,9),
0:(8,0)}


class Solution:
def getCount(self, n):
dp = [[0 for i in range(10)] for j in range(n+1)]
for i in range(10):
dp[1][i] = 1
for i in range(2,n+1):
for j in range(10):
x =0
for k in dic[j]:
x+= dp[i-1][k]
# print(x,k)
dp[i][j] = x
print(dp)
sum=0
for i in range(10):
sum+=dp[n][i]
return sum


+1
mailmohak161 month ago
C++ || Time: O(N) || Space : O(N):
long long getCount(int N)
	{
		//STORE THE NUMBERS REACHABLE FROM EACH POSITION INDEX-WISE
	    vector<vector<int>> data = {{0,8},
	                                {1,2,4},
	                                {1,2,3,5},
	                                {2,3,6},
	                                {1,4,5,7},
	                                {2,4,5,8,6},
	                                {3,5,6,9},
	                                {4,7,8},
	                                {5,8,0,7,9},
	                                {8,6,9}};
	                                
	    //DP[i][j] = count of numbers of length 'i' ending with 'j'                           
		vector<vector<long long>> dp(N+1,vector<long long>(10,0));
		
		//BASE CONDITION
		for(int i=0;i<10;i++){
		    dp[1][i]=1;
		}
		
		for(int i=2;i<=N;i++){
		    for(int j=0;j<10;j++){
		        for(auto x:data[j]){
		            dp[i][x]+=dp[i-1][j];
		        }
		    }
		}
		long long ans = 0;
		for(auto x:dp.back())ans+=x;
		return ans;
	}
+1
aloksinghbais022 months ago
C++ solution having time complexity as O(10*N) and space complexity as O(12*N) is as follows :- 
 
Execution Time :- 0.0 / 1.7 sec
 
long long int dp[10][26];int mat[4][3] = {{1,2,3},                 {4,5,6},                 {7,8,9},                 {-1,0,-1}};bool isValid(int x,int y){    if(x < 0 || x >= 4 || y < 0 || y >= 3) return (false);    if(mat[x][y] == -1) return (false);    return (true);}long long int helper(int x,int y,int n){    if(n == 1) return (1);    if(dp[mat[x][y]][n] != -1) return dp[mat[x][y]][n];        int dx[] = {0,-1,0,1,0};    int dy[] = {0,0,1,0,-1};        long long int cnt = 0;    for(int i = 0; i < 5; i++){        int nx = x + dx[i];        int ny = y + dy[i];        if(isValid(nx,ny)){            cnt += helper(nx,ny,n-1);        }    }        return (dp[mat[x][y]][n] = cnt);}long long getCount(int N){ dp[10][N+1]; memset(dp,-1,sizeof(dp)); long long int ans = 0; for(int i = 0; i < 4; i++){     for(int j = 0; j < 3; j++){         if(mat[i][j] != -1)             ans += helper(i,j,N);     } } return (ans);}
+2
lindan1232 months ago
long long getCount(int N)
	{
		vector<long long> dp(10,1);
		vector<long long> vec(10);
		
		if(N==1)
		{
		    long long ans = 10;
		    return ans;
		}
		for(int i=1;i<N;i++)
		{
		    vec[0] = dp[8] + dp[0];
		    vec[1] = dp[1] + dp[2] + dp[4];
		    vec[2] = dp[2] + dp[1] + dp[3] + dp[5];
		    vec[3] = dp[3] + dp[6] + dp[2];
		    vec[4] = dp[4] + dp[1] + dp[5] + dp[7];
		    vec[5] = dp[5] + dp[2] + dp[4] + dp[6] + dp[8];
		    vec[6] = dp[6] + dp[3] + dp[5] + dp[9];
		    vec[7] = dp[7] + dp[4] + dp[8];
		    vec[8] = dp[8] + dp[7] + dp[9] + dp[5] + dp[0];
		    vec[9] = dp[9] + dp[8] + dp[6];
		    dp = vec;
		    vec.empty();
		}
		
		long long count = 0;
		for(int i=0;i<10;i++)
		{
		    count+=dp[i];
		}
		return count;
		
		
	}
Time Taken : 0.0
Cpp
+2
balwanyadav27012 months ago
	vector<vector<int>>v={{0,8},{1,2,4},{1,2,3,5},{2,3,6},{1,4,5,7},{2,4,5,8,6},{3,5,6,9},{4,7,8},{5,8,0,7,9},{8,6,9}};
	long long as(int n,int c,vector<vector<long long>>&dp){
	    if(n==0){
	        return 0;
	    }
	    if(n==1){
	        return 1;
	    }
	    if(dp[n][c]!=-1){
	        return dp[n][c];
	    }
	    long long y=0;
	    for(int i=0;i<v[c].size();i++){
	        y=y+as(n-1,v[c][i],dp);
	    }
	    return dp[n][c]=y;
	    
	    
	}
	long long getCount(int N)
	{
		long long ans=0;
		vector<vector<long long>>dp(N+1,vector<long long>(10,-1));
        for(int i=0;i<10;i++){
            dp[0][i]=0;
        }
        for(int i=1;i<10;i++){
            dp[1][i]=10;
        }
		for(int i=0;i<=9;i++){
		    ans=ans+as(N,i,dp);
		}
		return ans;
	}


+9
piyushsharma99093 months ago
I can't solve this problem, please kick my ass and show mercy
+1
zaid473043 months ago
// Always go for most intuitive solution
long long helper(int i,int j,vector<vector<char>>&keypad,vector<vector<vector<long long>>>& dp,int N){
	    // boundary check
	    if(i<0 or i>=4 or j<0 or j>=3 or keypad[i][j]=='*' or keypad[i][j]=='#') return 0;
	    if(N==1) return 1;
	    // if answer already exists
	    if(dp[i][j][N]!=-1){
	        return dp[i][j][N];
	    }
	    return dp[i][j][N] = helper(i,j,keypad,dp,N-1)
	                            +helper(i+1,j,keypad,dp,N-1)
	                                +helper(i-1,j,keypad,dp,N-1)
	                                    +helper(i,j-1,keypad,dp,N-1)
	                                        +helper(i,j+1,keypad,dp,N-1);
	}
	long long getCount(int N)
	{
		// dp memoization solution
		vector<vector<char>>keypad={{'1','2','3'},{'4','5','6'},{'7','8','9'},{'*','0','#'}};
		// using 3D dp
		vector<vector<vector<long long >>> dp(4,vector<vector<long long >>(3,vector<long long>(N+1,-1)));
		long long count=0;
		for(int i=0;i<4;i++){
		    for(int j=0;j<3;j++){
		        if(keypad[i][j]!='*' and keypad[i][j]!='#')
		          count+=helper(i,j,keypad,dp,N);
		    }
		}
		return count;
	}
+1
hrithikrsgupta3 months ago
O(1) space and O(N) Time
 
long long getCount(int N){ // Your code goes here    vector<int> choice[10]={{0,8},{1,2,4},{1,2,3,5},   {2,3,6},{1,4,5,7},{2,4,5,6,8},   {3,5,6,9},{4,7,8},{5,7,8,9,0}   ,{6,8,9}};      vector<long long> dp(11); for(int i=0;i<=9;i++){     dp[i]=1; }      long long sum=0;   for(int i=2;i<=N;i++){      vector<long long> dp1(11);       for(int num = 0;num<= 9;num++){           for(auto a:choice[num]){               dp1[a]+=dp[num];           }       }       dp=dp1;   }      for(int i=0;i<=9;i++){       sum+=dp[i];   }   return sum;   }
-3
taiphanvan24034 months ago
long long getCount(int n){if (n == 1) return 10;if (n == 2) return 36;long long DP[10][n+1];for (int i = 0; i <= 9; i++) DP[i][1] = 1;for (int i = 2; i <= n; i++){ long long a[10]; a[0] = DP[0][i - 1] + DP[8][i - 1]; a[1] = DP[1][i - 1] + DP[2][i - 1] + DP[4][i - 1]; a[2]= DP[2][i - 1] + DP[1][i - 1] + DP[3][i - 1]+DP[5][i-1]; a[3]= DP[3][i - 1] + DP[2][i - 1] + DP[6][i - 1]; a[4]= DP[4][i - 1] + DP[1][i - 1] + DP[5][i - 1]+DP[7][i-1]; a[5]= DP[5][i - 1] + DP[2][i - 1] + DP[4][i - 1] + DP[8][i - 1]+DP[6][i-1]; a[6]= DP[6][i - 1] + DP[3][i - 1] + DP[5][i - 1] + DP[9][i - 1]; a[7] = DP[7][i - 1] + DP[4][i - 1] + DP[8][i - 1]; a[8]= DP[8][i - 1] + DP[0][i - 1] + DP[5][i - 1] + DP[7][i - 1] + DP[9][i - 1]; a[9]= DP[9][i - 1] + DP[6][i - 1] + DP[8][i - 1]; for (int j = 0; j <= 9; j++)  DP[j][i] = a[j];}long long sum = 0;for (int i = 0; i <= 9; i++) sum += DP[i][n];return sum;}
0
umangprayas84jee194 months ago
 vector<long long>dp1(10,1);  vector<long long>dp2(10);    for(int i=1;i<N;i++){     dp2[0]=dp1[8]+dp1[0];     dp2[1]=dp1[1]+dp1[2]+dp1[4];     dp2[2]=dp1[1]+dp1[2]+dp1[3]+dp1[5];     dp2[3]=dp1[2]+dp1[3]+dp1[6];     dp2[4]=dp1[1]+dp1[7]+dp1[5]+dp1[4];     dp2[5]=dp1[4]+dp1[2]+dp1[6]+dp1[5]+dp1[8];     dp2[6]=dp1[9]+dp1[3]+dp1[5]+dp1[6];     dp2[7]=dp1[4]+dp1[8]+dp1[7];     dp2[8]=dp1[5]+dp1[7]+dp1[9]+dp1[8]+dp1[0];     dp2[9]=dp1[6]+dp1[8]+dp1[9];          dp1=dp2;     dp2.empty(); } long long count=0; for(int i=0;i<10;i++){     count+=dp1[i]; } return count;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.