Given an integer N, the task is to count the numbers less than or equal to N such that each number contains at least one repeated digit.
Examples:
Input: N = 20
Output: 1
Explanation:
Numbers containing at least one repeated digit and less than or equal to N(= 20) are {11}.
Therefore, the required output is 1.
Input: N = 100
Output: 10
Explanation:
Numbers containing at least one repeated digit and less than or equal to N(= 100) are {11, 22, 33, 44, 55, 66, 77, 88, 99, 100}.
Therefore, the required output is 10.
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say X, to store the total count of digits in N.
- Initialize a variable, say cntNumbers, to store the total count of numbers less than or equal to N consisting of unique digits.
- Calculate the total count of X-digit numbers which contains unique digits and update cntNumbers. Below is the formula to calculate total count of X-digit numbers with all unique digits.
Total count of X-digit numbers with all unique digits = {(9 * factorial(9)) / factorial(10 – X)}
- Calculate total count of X-digit numbers less than or equal to N which contains unique digits by checking all possible values at each possible digits of a number less than or equal to N and update cntNumbers.
- Finally, print the value of (N – cntNumbers).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long factorial( int n)
{
return (n == 1 || n == 0) ? 1 :
n * factorial(n - 1);
}
long NPR( int n, int k)
{
return factorial(n) / factorial(n - k);
}
int cntNumLessThanEqualNRepeatedDigits( int N)
{
int temp = N;
int X = 0;
vector< int > nums;
while (temp)
{
X += 1;
nums.push_back(temp % 10);
temp /= 10;
}
reverse(nums.begin(), nums.end());
int cntNumbers = 0;
for ( int i = 1; i < X; i++)
{
cntNumbers += 9 * (factorial(9) /
factorial(10 - i));
}
unordered_set< int > vis;
for ( int i = 0; i < nums.size(); i++)
{
int k = 0;
int Min = 0;
if (i == 0)
{
Min = 1;
}
else
{
Min = 0;
}
int Max = 0;
if (i == X - 1)
{
Max = nums[i];
}
else
{
Max = nums[i] - 1;
}
for ( int j = Min; j <= Max; j++)
{
if (vis.find(j) != vis.end())
{
continue ;
}
k += 1;
}
cntNumbers += k * (NPR(9 - i,
X - i - 1));
if (vis.find(nums[i]) != vis.end())
{
break ;
}
vis.insert(nums[i]);
}
return (N - cntNumbers);
}
int main()
{
int N = 100;
cout << cntNumLessThanEqualNRepeatedDigits(N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
public static int factorial( int n)
{
return (n == 1 || n == 0 ) ? 1 :
n * factorial(n - 1 );
}
public static int NPR( int n, int k)
{
return factorial(n) / factorial(n - k);
}
public static int cntNumLessThanEqualNRepeatedDigits( int N)
{
int temp = N;
int X = 0 ;
List<Integer> nums = new ArrayList<>();
while (temp > 0 )
{
X += 1 ;
nums.add(temp % 10 );
temp /= 10 ;
}
Collections.reverse(nums);
int cntNumbers = 0 ;
for ( int i = 1 ; i < X; i++)
{
cntNumbers += 9 * (factorial( 9 ) /
factorial( 10 - i));
}
Set<Integer> vis= new HashSet<>();
for ( int i = 0 ; i < nums.size(); i++)
{
int k = 0 ;
int Min = 0 ;
if (i == 0 )
{
Min = 1 ;
}
else
{
Min = 0 ;
}
int Max = 0 ;
if (i == X - 1 )
{
Max = nums.get(i);
}
else
{
Max = nums.get(i) - 1 ;
}
for ( int j = Min; j <= Max; j++)
{
if (vis.contains(j))
{
continue ;
}
k += 1 ;
}
cntNumbers += k * (NPR( 9 - i,
X - i - 1 ));
if (vis.contains(nums.get(i)))
{
break ;
}
vis.add(nums.get(i));
}
return (N - cntNumbers);
}
public static void main(String[] args)
{
int N = 100 ;
System.out.print(
cntNumLessThanEqualNRepeatedDigits(N));
}
}
|
Python3
import math
def NPR(n, k):
return (math.factorial(n) / /
math.factorial(n - k))
def cntNumLessThanEqualNRepeatedDigits(N):
temp = N
X = 0 ;
nums = []
while (temp):
X + = 1
nums.append(temp % 10 )
temp / / = 10
nums.reverse()
cntNumbers = 0
for i in range ( 1 , X):
cntNumbers + = 9 * (math.factorial( 9 ) / /
math.factorial( 10 - i))
vis = set ()
for i, val in enumerate (nums):
k = 0
Min = 0 ;
if i = = 0 :
Min = 1
else :
Min = 0
Max = 0 ;
if i = = X - 1 :
Max = val
else :
Max = val - 1 ;
for j in range ( Min , Max + 1 ):
if (j in vis):
continue
k + = 1
cntNumbers + = k * (NPR( 9 - i,
X - i - 1 ))
if val in vis:
break
vis.add(val)
return (N - cntNumbers)
if __name__ = = '__main__' :
N = 100
print (cntNumLessThanEqualNRepeatedDigits(N))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int factorial( int n)
{
return (n == 1 || n == 0) ? 1 :
n * factorial(n - 1);
}
static int NPR( int n, int k)
{
return factorial(n) / factorial(n - k);
}
static int cntNumLessThanEqualNRepeatedDigits( int N)
{
int temp = N;
int X = 0;
List< int > nums = new List< int >();
while (temp > 0)
{
X += 1;
nums.Add(temp % 10);
temp /= 10;
}
nums.Reverse();
int cntNumbers = 0;
for ( int i = 1; i < X; i++)
{
cntNumbers += 9 * (factorial(9) /
factorial(10 - i));
}
HashSet< int > vis = new HashSet< int >();
for ( int i = 0; i < nums.Count; i++)
{
int k = 0;
int Min = 0;
if (i == 0)
{
Min = 1;
}
else
{
Min = 0;
}
int Max = 0;
if (i == X - 1)
{
Max = nums[i];
}
else
{
Max = nums[i] - 1;
}
for ( int j = Min; j <= Max; j++)
{
if (vis.Contains(j))
{
continue ;
}
k += 1;
}
cntNumbers += k * (NPR(9 - i,
X - i - 1));
if (vis.Contains(nums[i]))
{
break ;
}
vis.Add(nums[i]);
}
return (N - cntNumbers);
}
static void Main()
{
int N = 100;
Console.WriteLine(cntNumLessThanEqualNRepeatedDigits(N));
}
}
|
Javascript
<script>
function factorial(n)
{
return (n == 1 || n == 0) ? 1 :
n * factorial(n - 1);
}
function NPR(n, k)
{
return factorial(n) / factorial(n - k);
}
function cntNumLessThanEqualNRepeatedDigits(N)
{
var temp = N;
var X = 0;
var nums = [];
while (temp)
{
X += 1;
nums.push(temp % 10);
temp = parseInt(temp/10);
}
nums.reverse();
var cntNumbers = 0;
for ( var i = 1; i < X; i++)
{
cntNumbers += 9 * (factorial(9) /
factorial(10 - i));
}
var vis = new Set();
for ( var i = 0; i < nums.length; i++)
{
var k = 0;
var Min = 0;
if (i == 0)
{
Min = 1;
}
else
{
Min = 0;
}
var Max = 0;
if (i == X - 1)
{
Max = nums[i];
}
else
{
Max = nums[i] - 1;
}
for ( var j = Min; j <= Max; j++)
{
if (vis.has(j))
{
continue ;
}
k += 1;
}
cntNumbers += k * (NPR(9 - i,
X - i - 1));
if (vis.has(nums[i]))
{
break ;
}
vis.add(nums[i]);
}
return (N - cntNumbers);
}
var N = 100;
document.write( cntNumLessThanEqualNRepeatedDigits(N));
</script>
|
Time Complexity: O((log10N)2)
Auxiliary Space: O(1)