Print a number strictly less than a given number such that all its digits are distinct.
Given a positive number n, print a number less than n such that all its digits are distinct.
Examples:
Input : 1134
Output : 1098
1098 is the largest number smaller than
1134 such that all digits are distinct.
Input : 4559
Output : 4539
The problem can easily be solved by using counting. Firstly, loop through numbers less than n and for each number count the frequency of the digits using count array. If all the digits occur only once than we print that number. The answer always exists so there is no problem of infinite loop.
C++
#include <bits/stdc++.h>
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;
}
|
Java
class GFG{
static int findNumber( int n)
{
for ( int i = n - 1 ;i >= 0 ; i--) {
int [] count= new int [ 10 ];
int x = i;
int count1 = 0 , count2 = 0 ;
while (x> 0 ) {
count[x % 10 ]++;
x /= 10 ;
count1++;
}
for ( int j = 0 ; j < 10 ; j++) {
if (count[j] == 1 )
count2++;
}
if (count1 == count2)
return i;
}
return - 1 ;
}
public static void main(String[] args)
{
int n = 8490 ;
System.out.println(findNumber(n));
}
}
|
Python3
def findNumber(n):
i = n - 1
while (i> = 0 ):
count = [ 0 for i in range ( 10 )]
x = i
count1 = 0
count2 = 0
while (x):
count[x % 10 ] + = 1
x = int (x / 10 )
count1 + = 1
for j in range ( 0 , 10 , 1 ):
if (count[j] = = 1 ):
count2 + = 1
if (count1 = = count2):
return i
i - = 1
if __name__ = = '__main__' :
n = 8490
print (findNumber(n))
|
C#
using System;
class GFG
{
static int findNumber( int n)
{
for ( int i = n - 1; i >= 0; i--)
{
int [] count = new int [10];
int x = i;
int count1 = 0, count2 = 0;
while (x > 0)
{
count[x % 10]++;
x /= 10;
count1++;
}
for ( int j = 0; j < 10; j++)
{
if (count[j] == 1)
count2++;
}
if (count1 == count2)
return i;
}
return -1;
}
static public void Main ()
{
int n = 8490;
Console.WriteLine(findNumber(n));
}
}
|
PHP
<?php
function findNumber( $n )
{
for ( $i = $n - 1; $i >= 0 ; $i --)
{
$count = array_fill (0, 10, 0);
$x = $i ;
$count1 = 0; $count2 = 0;
while ( $x )
{
$count [ $x % 10]++;
$x = (int)( $x / 10);
$count1 ++;
}
for ( $j = 0; $j < 10; $j ++)
{
if ( $count [ $j ] == 1)
$count2 ++;
}
if ( $count1 == $count2 )
return $i ;
}
}
$n = 8490;
echo findNumber( $n );
?>
|
Javascript
<script>
function findNumber(n)
{
for (i = n - 1;i >=0 ; i--) {
var count=Array.from({length: 10}, (_, i) => 0);
var x = i;
var count1 = 0, count2 = 0;
while (x>0) {
count[x % 10]++;
x = parseInt(x/10);
count1++;
}
for (j = 0; j < 10; j++) {
if (count[j] == 1)
count2++;
}
if (count1 == count2)
return i;
}
return -1;
}
var n = 8490;
document.write(findNumber(n));
</script>
|
Time Complexity: O(N*log10N) where n is number of elements in given array. As, we are using a loop to traverse N times so it will cost us O(N) time and we are using a while loop which will cost O (logN) as we are decrement by floor division of 10 each time.
Auxiliary Space: O(1), as we are not using any extra space.
Last Updated :
29 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...