# Program to Find Out Integers Less than n Containing Multiple Similar Digits in C++

Suppose we have an integer n, we have to find the number of positive integers that are less than or equal to n, where the integer numbers at least have a digit occurring more than once.

So, if the input is like n = 200, then the output will be 38

To solve this, we will follow these steps −

• Define an array a

• for initialize x := n, when x is non−zero, update x := x / 10, do −

• insert x mod 10 at the end of a

• reverse the array a

• ret := n

• for initialize w := 1, d := 1, when w < size of a, update (increase w by 1), do −

• d := d * min(9, 10 − w + 1)

• ret := ret − d

• Define a function go(). This takes no argument.

• b := (1 bitwise left shift 10) − 1

• for initialize i := 0, when i < size of a, update (increase i by 1), do −

• for initialize d := i < 1, when d < a[i], update (increase d by 1), do −

• ret := ret − x

• if ((1 bitwise left shift a[i]) bitwise AND b) is non−zero, then

• b := b XOR (1 bitwise left shift a[i])

• Otherwise

• return

• (decrease ret by 1)

• Call the function go()

• return ret

Let us see the following implementation to get better understanding −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int solve(int n) {
vector<int> a;
for (int x = n; x; x /= 10) a.push_back(x % 10);
reverse(a.begin(), a.end());
int ret = n;
for (int w = 1, d = 1; w < a.size(); ++w) {
d *= min(9, 10 − w + 1);
ret −= d;
}
auto go = [&]() {
int b = (1 << 10) − 1;
for (int i = 0; i < a.size(); ++i) {
for (int d = (i < 1); d < a[i]; ++d) {
int x = 0;
if ((1 << d) & b) ++x;
for (int j = i + 1; j < a.size(); ++j) x *= 10 − j;
ret −= x;
}
if ((1 << a[i]) & b)
b ^= (1 << a[i]);
else
return;
}
−−ret;
};
go();
return ret;
}
int main(){
cout << solve(200) << endl;
return 0;
}

## Input

200

## Output

38

Updated on: 26-Dec-2020

123 Views