Problem Statement
You will be given some decimal digits in a
Definition
- Class:
- FewestFactors
- Method:
- number
- Parameters:
- int[]
- Returns:
- int
- Method signature:
- int number(int[] digits)
- (be sure your method is public)
Notes
- A factor of an integer n is an integer k, such that n % k = 0 (% being the modulo operator).
- The digit 0 can also be used as a leading zero, see example 1.
Constraints
- digits will have between 1 and 5 elements, inclusive.
- Each element of digits will be between 0 and 9, inclusive.
- At least one element of digits will be non-zero.
Examples
{1, 2}
Returns: 21
Using the digits 1 and 2 we can build the numbers 12 (which has 6 factors: 1, 2, 3, 4, 6 and 12) and 21 (which has 4 factors: 1, 3, 7 and 21). So we return 21, which is the number among them having the smallest number of factors.
{6, 0}
Returns: 6
Note that we can use 0 as a leading zero, giving the number 6 that has 4 factors (1, 2, 3 and 6), less than the alternative 60 that has 12 factors.
{4, 7, 4}
Returns: 447
Note that digits may contain duplicate digits. We have to use each digit as many times as it appears in the input.
{1, 3, 7, 9}
Returns: 1973
{7, 5, 4, 3, 6}
Returns: 36457
{1}
Returns: 1
{8}
Returns: 8
{9}
Returns: 9
{0, 0, 0, 0, 1}
Returns: 1
{0, 1}
Returns: 1
{0, 6}
Returns: 6
{2, 4}
Returns: 24
Both 24 and 42 have 8 factors (24 has the factors 1, 2, 3, 4, 6, 8, 12 and 24, while 42 has the factors 1, 2, 3, 6, 7, 14, 21 and 42). In this case we have to use the tie-breaker and return
{2, 6}
Returns: 26
{8, 1}
Returns: 81
{0, 1, 0}
Returns: 1
{0, 2, 4}
Returns: 24
{2, 3, 4}
Returns: 243
{3, 4, 3}
Returns: 433
{1, 1, 2}
Returns: 211
{2, 5, 9}
Returns: 529
{4, 4, 8}
Returns: 844
{9, 8, 7}
Returns: 789
{3, 3, 1, 1}
Returns: 1133
{1, 2, 3, 4}
Returns: 1423
{2, 4, 6, 8}
Returns: 2846
{1, 3, 7, 9}
Returns: 1973
{0, 4, 8, 6, 2}
Returns: 2846
{2, 4, 6, 8, 1}
Returns: 24861
{9, 8, 7, 6, 5}
Returns: 56897
{8, 6, 5, 1, 7}
Returns: 16857
{0, 3, 0, 0, 0}
Returns: 3
{2, 2, 2, 2, 2}
Returns: 22222
{4, 4, 4, 4, 1}
Returns: 44441
{1, 3, 3, 3, 3}
Returns: 31333
{1,2,4}
Returns: 241
Both 241 and 421 are prime numbers, and thus have exactly 2 factors (241 has the factors 1 and 241, while 421 has the factors 1 and 421). All other numbers that we can build from these digits (124, 142, 214 and 412) have more than 2 factors. We have to use the tie-breaker here and return the smaller of (241, 421).
{6, 0, 4}
Returns: 46
{8, 3, 2, 6, 9}
Returns: 23689
{1, 7, 1, 5}
Returns: 1571
{9, 6, 0}
Returns: 69
{9, 3, 7, 5, 8}
Returns: 35879
{4, 6, 8}
Returns: 486
{2, 8, 1}
Returns: 281
{6, 6, 5}
Returns: 566
{2, 7, 7}
Returns: 277
{1, 3, 8}
Returns: 183
{6, 8, 0, 3, 0}
Returns: 683
{8, 4, 1, 5}
Returns: 4581
{0, 5, 6, 9}
Returns: 569
{7, 6, 9}
Returns: 769
{1, 1, 6, 3, 7}
Returns: 11673
{0, 7, 4, 5}
Returns: 457
{2, 4, 9, 9, 8}
Returns: 24989
{9, 4, 3}
Returns: 349
{7, 5, 4, 2, 1}
Returns: 12457
{6, 7, 4, 7, 4}
Returns: 46477
{5, 0, 9, 4, 2}
Returns: 2459
{9, 2, 5}
Returns: 529
{2, 1, 6}
Returns: 261
{4, 2, 0, 8, 8}
Returns: 4882
{7, 6, 0}
Returns: 67
{8, 5, 9}
Returns: 859
{5, 7, 2, 2}
Returns: 5227
{2, 7, 2, 5}
Returns: 5227
{6, 9, 9, 5, 1}
Returns: 15699
{7, 4, 7, 9, 7}
Returns: 47779
{7, 5, 4, 3, 6 }
Returns: 36457
{1, 2, 4 }
Returns: 241
{9, 2, 4, 7, 8 }
Returns: 24789
{7, 5, 4, 3, 9 }
Returns: 34759
{1, 9, 3, 4, 4 }
Returns: 13449
{2, 5, 9 }
Returns: 529
{4, 2, 1 }
Returns: 241
{7, 3, 9, 3, 9 }
Returns: 33997
{1, 1, 1, 1, 1 }
Returns: 11111
{4, 0, 9 }
Returns: 409
{6, 4 }
Returns: 46
{1, 6, 9 }
Returns: 619
{1, 8 }
Returns: 81
{9, 8, 7, 6, 5 }
Returns: 56897
{5, 2, 9 }
Returns: 529
{7, 6, 7, 5, 7 }
Returns: 65777
{1, 7 }
Returns: 17
{1, 2, 1 }
Returns: 211
{1 }
Returns: 1
{8, 1 }
Returns: 81
{0, 0, 0, 2, 1 }
Returns: 21
{0, 0, 0, 1, 7 }
Returns: 17