Find largest multiple for a number set

1.3k views Asked by At

An array of digits(0-9) of size N is provided as input. A set of numbers(N1,...,Nm) of size m with the numbers separated by space is also as the input. The program has to print the largest number that can be formed using the digits in the array of size N that is divisible by the numbers N1,..,Nm

Example Input/Output1:

INPUT:

160

2 3 5

OUTPUT

60

Explanation 60 is the largest number that can be formed using the digits 1,6,0 which is divisible by 2,3,5

Example Input/Output2:

Input

91028

17 5 9

Output

9180

Boundary Conditions

1<=m<=5

2<=N<=50

Can somebody explain how to approach this problem.
1

There are 1 answers

0
DrKoch On

Partial answer:

Try all permutations of all subsets of your digits, probably starting with the largest candidates.

If your factors contain 5 the last digit must be 0or 5

If your factors contain 3 or 6 or 9 the sum of all candidate digits muts be a multiple of 3

If your factors contain 2 or 4 or 6 or 8 the last digit must be even.

And so on.