0
$\begingroup$

Given a list of $N$ digits. I have to create groups of these digits which represent a single number such that their $GCD$ is maximized.Min and max number of groups that are allowed is given by user. The number of digits in a number can't exceed $6$ and the maximum number of digits in a number is given by user.

I thought of making every group with maximum number of digits allowed and calculating their $GCD$ but it's not optimal. Is there any efficient and optimal way to do this ?

Eg- Suppose the list is - $4,5,3,4,5,6$ and maximum no. of digits are = $3$ so the numbers can be

  1. $453,456$ , $gcd(453,456)$
  2. $4,53,456$ , $gcd(4,53,456)$ and so on.
  • 0
    Can we exchange the positions of the digits within the groups ?2017-01-16
  • 1
    It sounds like it could be interesting; can you give a small example with say max digits/number = 3? Can the list of $N$ digits include repeated copies of the same digit?2017-01-16
  • 0
    @Peter No, the position of digits can't be changed within the group.2017-01-16
  • 0
    @Joffan Yes the list can contain the repeated copies of same digit.2017-01-16
  • 0
    "The number of digits in a number can't exceed $6$ and the maximum number of digits in a number is given" is a repetition and is contradictory with "maximum no. of digits are = $3$". Furthermore, you don't specify the range of the number of groups.2017-01-16
  • 0
    I don't see a much better way than brute-forcing by trying all allowed group combinations, and backtracking every time a $\gcd$ lower than the best so far is found.2017-01-16
  • 0
    "I thought of making every group with maximum number of digits allowed and calculating their $\gcd$ but it's not optimal.": how do you know ?2017-01-16
  • 0
    @YvesDaoust Maximum Number of digits in a number is given as it's not supposed to exceed $6$, that means it can be $3$(it was just a example) as the max number is given by user. The range of number of group is also user defined.2017-01-16
  • 0
    @YvesDaoust Including maximum number of digits in each number allowed will sometime be less then the minimum number of group(number) required which is unknown as it's given by user.2017-01-16
  • 0
    Usually "is given" means exactly the opposite of "is given by the user".2017-01-16
  • 0
    Sorry, I'll correct it.2017-01-16
  • 0
    This appears to be taken from a live programming contest problem, without attribution: https://www.codechef.com/JAN17/problems/DIGITSEP. See the guidelines on contest problems: http://meta.math.stackexchange.com/q/16774/14578, and on providing attribution/credit: http://cs.stackexchange.com/help/referencing.2017-01-16

0 Answers 0