Count of N digit Numbers having no pair of equal consecutive Digits
Last Updated :
14 Jun, 2021
Given an integer N, the task is to find the total count of N digit numbers such that no two consecutive digits are equal.
Examples:
Input: N = 2
Output: 81
Explanation:
Count possible 2-digit numbers, i.e. the numbers in the range [10, 99] = 90
All 2-digit numbers having equal consecutive digits are {11, 22, 33, 44, 55, 66, 77, 88, 99}.
Therefore, the required count = 90 – 9 = 81
Input: N = 1
Output: 10
Naive Approach: The simplest approach to solve the problem is to iterate over all possible N-digit numbers and check for every number if any two consecutive digits are equal or not.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void count( int N)
{
if (N == 1)
{
cout << 10 << endl;
return ;
}
int l = pow (10, N - 1);
int r = pow (10, N) - 1;
int ans = 0;
for ( int i = l; i <= r; i++)
{
string s = to_string(i);
int flag = 0;
for ( int j = 1; j < N; j++)
{
if (s[j] == s[j - 1])
{
flag = 1;
break ;
}
}
if (flag == 0)
ans++;
}
cout << ans << endl;
}
int main()
{
int N = 2;
count(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void count( int N)
{
if (N == 1 ) {
System.out.println( 10 );
return ;
}
int l = ( int )Math.pow( 10 , N - 1 );
int r = ( int )Math.pow( 10 , N) - 1 ;
int ans = 0 ;
for ( int i = l; i <= r; i++) {
String s = Integer.toString(i);
int flag = 0 ;
for ( int j = 1 ; j < N; j++) {
if (s.charAt(j) == s.charAt(j - 1 )) {
flag = 1 ;
break ;
}
}
if (flag == 0 )
ans++;
}
System.out.println(ans);
}
public static void main(String[] args)
{
int N = 2 ;
count(N);
}
}
|
Python3
def count(N):
if (N = = 1 ):
print ( 10 );
return ;
l = int ( pow ( 10 , N - 1 ));
r = int ( pow ( 10 , N) - 1 );
ans = 0 ;
for i in range (l, r + 1 ):
s = str (i);
flag = 0 ;
for j in range ( 1 , N):
if (s[j] = = s[j - 1 ]):
flag = 1 ;
break ;
if (flag = = 0 ):
ans + = 1 ;
print (ans);
if __name__ = = '__main__' :
N = 2 ;
count(N);
|
C#
using System;
class GFG{
public static void count( int N)
{
if (N == 1)
{
Console.WriteLine(10);
return ;
}
int l = ( int )Math.Pow(10, N - 1);
int r = ( int )Math.Pow(10, N) - 1;
int ans = 0;
for ( int i = l; i <= r; i++)
{
String s = i.ToString();
int flag = 0;
for ( int j = 1; j < N; j++)
{
if (s[j] == s[j - 1])
{
flag = 1;
break ;
}
}
if (flag == 0)
ans++;
}
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int N = 2;
count(N);
}
}
|
Javascript
<script>
function count(N)
{
if (N == 1)
{
document.write(10 + "<br>" );
return ;
}
var l = Math.pow(10, N - 1);
var r = Math.pow(10, N) - 1;
var ans = 0;
for ( var i = l; i <= r; i++)
{
var s = (i.toString());
var flag = 0;
for ( var j = 1; j < N; j++)
{
if (s[j] == s[j - 1])
{
flag = 1;
break ;
}
}
if (flag == 0)
ans++;
}
document.write( ans + "<br>" );
}
var N = 2;
count(N);
</script>
|
Time Complexity: O(N * (10N), where N is the given integer.
Auxiliary Space: O(1)
Dynamic Programming Approach: The above approach can be optimized using Dynamic Programming approach. Follow the steps below to solve the problem:
- Initialize DP[][], where DP[i][j] stores the count of numbers having i digits, and ending with j.
- Iterate from 2 to N and follow the steps:
- Calculate the total count of valid i-1 digit numbers by adding all the values of DP[i-1][j] where j ranges from 0 to 9, and store it in temp.
- Update DP[i][j] = temp – DP[i-1][j], where j ranges from 0 to 9.
- The result is the sum of DP[N][j], where j ranges from 0 to 9
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void count( int N)
{
if (N == 1)
{
cout << (10) << endl;
return ;
}
int dp[N][10];
memset (dp, 0, sizeof (dp));
for ( int i = 1; i < 10; i++)
dp[0][i] = 1;
for ( int i = 1; i < N; i++)
{
int temp = 0;
for ( int j = 0; j < 10; j++)
temp += dp[i - 1][j];
for ( int j = 0; j < 10; j++)
dp[i][j] = temp - dp[i - 1][j];
}
int ans = 0;
for ( int i = 0; i < 10; i++)
ans += dp[N - 1][i];
cout << ans << endl;
}
int main()
{
int N = 2;
count(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void count( int N)
{
if (N == 1 ) {
System.out.println( 10 );
return ;
}
int dp[][] = new int [N][ 10 ];
for ( int i = 1 ; i < 10 ; i++)
dp[ 0 ][i] = 1 ;
for ( int i = 1 ; i < N; i++) {
int temp = 0 ;
for ( int j = 0 ; j < 10 ; j++)
temp += dp[i - 1 ][j];
for ( int j = 0 ; j < 10 ; j++)
dp[i][j] = temp - dp[i - 1 ][j];
}
int ans = 0 ;
for ( int i = 0 ; i < 10 ; i++)
ans += dp[N - 1 ][i];
System.out.println(ans);
}
public static void main(String[] args)
{
int N = 2 ;
count(N);
}
}
|
Python3
def count(N):
if (N = = 1 ):
print ( 10 );
return ;
dp = [[ 0 for i in range ( 10 )]
for j in range (N)]
for i in range ( 1 , 10 ):
dp[ 0 ][i] = 1 ;
for i in range ( 1 , N):
temp = 0 ;
for j in range ( 10 ):
temp + = dp[i - 1 ][j];
for j in range ( 10 ):
dp[i][j] = temp - dp[i - 1 ][j];
ans = 0 ;
for i in range ( 10 ):
ans + = dp[N - 1 ][i];
print (ans);
if __name__ = = '__main__' :
N = 2 ;
count(N);
|
C#
using System;
class GFG{
public static void count( int N)
{
if (N == 1)
{
Console.WriteLine(10);
return ;
}
int [,]dp = new int [N, 10];
for ( int i = 1; i < 10; i++)
dp[0, i] = 1;
for ( int i = 1; i < N; i++)
{
int temp = 0;
for ( int j = 0; j < 10; j++)
temp += dp[i - 1, j];
for ( int j = 0; j < 10; j++)
dp[i, j] = temp - dp[i - 1, j];
}
int ans = 0;
for ( int i = 0; i < 10; i++)
ans += dp[N - 1, i];
Console.WriteLine(ans);
}
public static void Main(String[] args)
{
int N = 2;
count(N);
}
}
|
Javascript
<script>
function count(N)
{
if (N == 1)
{
document.write((10) + "<br>" );
return ;
}
var dp = Array.from(Array(N),
()=> Array(10).fill(0));
for ( var i = 1; i < 10; i++)
dp[0][i] = 1;
for ( var i = 1; i < N; i++)
{
var temp = 0;
for ( var j = 0; j < 10; j++)
temp += dp[i - 1][j];
for ( var j = 0; j < 10; j++)
dp[i][j] = temp - dp[i - 1][j];
}
var ans = 0;
for ( var i = 0; i < 10; i++)
ans += dp[N - 1][i];
document.write(ans);
}
var N = 2;
count(N);
</script>
|
Time Complexity: O(N), where N is the given integer
Auxiliary Space: O(N)
Efficient Approach: The above approach can be further optimized by observing that for any N digit number, the required answer is 9N which can be calculated using Binary Exponentiation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int power( int x, int y, int mod)
{
int res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
void count( int N)
{
if (N == 1)
{
cout << 10 << endl;
return ;
}
cout << (power(9, N, 1000000007)) << endl;
}
int main()
{
int N = 3;
count(N);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int power( int x, int y, int mod)
{
int res = 1 ;
x = x % mod;
if (x == 0 )
return 0 ;
while (y > 0 ) {
if ((y & 1 ) == 1 )
res = (res * x) % mod;
y = y >> 1 ;
x = (x * x) % mod;
}
return res;
}
public static void count( int N)
{
if (N == 1 ) {
System.out.println( 10 );
return ;
}
System.out.println(power( 9 , N,
1000000007 ));
}
public static void main(String[] args)
{
int N = 3 ;
count(N);
}
}
|
Python3
def power(x, y, mod):
res = 1 ;
x = x % mod;
if (x = = 0 ):
return 0 ;
while (y > 0 ):
if ((y & 1 ) = = 1 ):
res = (res * x) % mod;
y = y >> 1 ;
x = (x * x) % mod;
return res;
def count(N):
if (N = = 1 ):
print ( 10 );
return ;
print (power( 9 , N, 1000000007 ));
if __name__ = = '__main__' :
N = 3 ;
count(N);
|
C#
using System;
class GFG{
static int power( int x, int y, int mod)
{
int res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
public static void count( int N)
{
if (N == 1)
{
Console.WriteLine(10);
return ;
}
Console.WriteLine(power(9, N,
1000000007));
}
public static void Main(String[] args)
{
int N = 3;
count(N);
}
}
|
Javascript
<script>
function power(x, y, mod)
{
let res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) == 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
function count(N)
{
if (N == 1)
{
document.write(10);
return ;
}
document.write(power(9, N, 1000000007));
}
let N = 3;
count(N);
</script>
|
Time Complexity: O(logN)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...