int countNumbersWithUniqueDigits(int n)
{
if (n<1) return 1;
// For single digit, we have 10 numbers with unique digits
int totalUnique = 10;
//After first digit, the choice for first digit is only 9 digits,
int prevMultiplier = 9;
for (int i = 1; i < n; i++)
{
prevMultiplier *= (10i);
totalUnique += prevMultiplier;
}
return totalUnique;
}
Super short & simple C++ 0ms solution


@rushabhmshah99 Why though? Here n is the number of digits. So, the for condition where the actual number is less than 10 is never required to be checked.