Print a number strictly less than a given number such that all its digits are distinct . | CPP program to find a number less than n such that all its digits are distinct ; Function to find a number less than n such that all its digits are distinct ; looping through numbers less than n ; initializing a hash array ; creating a copy of i ; initializing variables to compare lengths of digits ; counting frequency of the digits ; checking if each digit is present once ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findNumber ( int n ) { for ( int i = n - 1 ; > = 0 ; i -- ) { int count [ 10 ] = { 0 } ; int x = i ; int count1 = 0 , count2 = 0 ; while ( x ) { count [ x % 10 ] ++ ; x /= 10 ; count1 ++ ; } for ( int j = 0 ; j < 10 ; j ++ ) { if ( count [ j ] == 1 ) count2 ++ ; } if ( count1 == count2 ) return i ; } } int main ( ) { int n = 8490 ; cout << findNumber ( n ) ; return 0 ; }