Fascinating Number

 Fascinating Number

PrepBuddy find any four digit number fascinating that has all the 

4

 digits unique. For example 

1234

 is a fascinating number. His friend Rahul gave him N numbers and asks him to find the minimum number which is strictly larger than the given one and has only distinct digits.


Input format

The first line of the input contains integer 

N

, denoting the count of numbers provided by Rahul.

Each of the next 

N

 lines contains one integer.


Output format

Print the next fascinating number.

Example

Input

1234

2010

Output

1235

2013


import java.util.HashSet;


public class FascinatingNumberFinder {

public static boolean validcheck(int n)

{

HashSet<Integer> set= new HashSet<>();

while(n >0)

{

int rem=n%10;

set.add(rem);

n=n/10;

}

return set.size()==4;

}


public static int nextFascinatingNumber(int n)

{

for(int i=n+1;i<=9999;i++)

{

if(validcheck(i))

{

return i; // Return the first number with unique digits

}

}

return -1; // This shouldn't happen given constraints

}

public static void main(String[] args) {

// TODO Auto-generated method stub

//int n=1234;

int n=2010;

System.out.println(nextFascinatingNumber(n));


}


}


Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence