We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

Following @manuelalejandr26 and the key point explained by @argxgd i.e.*
"
we have to take exactly one element from each list but not neccessarily the largest one. Although our target is to get the max value of sum S. Since we are dealing with modulo operator, a smaller element might yield a higher modulo then a bigger and this entirely depends on the value of M.
"*

At first we might naively select the max integer from each list. However, this will not work, as explained above. We can therefore calculate all possible outcomes, i.e. sum(x => x ** 2)%M, from all of the possible single selections from each list). Once we have all of the possible outcomes as a list, we can print the max value.

The important tool is itertools.product. Given a list of integers. we can then generate a list of all possible permutations, selecting one item from each list. We can then use list comprehensions to apply the formula (sum(x => x ** 2)%M for each permutation.

## Maximize It!

You are viewing a single comment's thread. Return to all comments â†’

Following @manuelalejandr26 and the key point explained by @argxgd i.e.* " we have to take exactly one element from each list but not neccessarily the largest one. Although our target is to get the max value of sum S. Since we are dealing with modulo operator, a smaller element might yield a higher modulo then a bigger and this entirely depends on the value of M. "*

At first we might naively select the max integer from each list. However, this will not work, as explained above. We can therefore calculate all possible outcomes, i.e. sum(x => x ** 2)%M, from all of the possible single selections from each list). Once we have all of the possible outcomes as a list, we can print the max value.

The important tool is itertools.product. Given a list of integers. we can then generate a list of all possible permutations, selecting one item from each list. We can then use list comprehensions to apply the formula (sum(x => x ** 2)%M for each permutation.