Thursday, November 7, 2019

Maximize It! - Hacker Rank Solution

You are given a function . You are also given  lists. The  list consists of  elements.
You have to pick one element from each list so that the value from the equation below
is maximized:

%
 denotes the element picked from the  list . Find the maximized value  obtained.
 denotes the modulo operator.
Note that you need to take exactly one element from each list, not necessarily the largest element. You add the squares of the chosen elements and perform the modulo operation. The maximum value that you can obtain, will be the answer to the problem.
Input Format
The first line contains  space separated integers  and .
The next  lines each contains an integer , denoting the number of elements in the  list, followed by  space separated integers denoting the elements in the list.
Constraints



Output Format
Output a single integer denoting the value .
Sample Input
3 1000
2 5 4
3 7 8 9 
5 5 7 8 9 10 
Sample Output
206
Explanation
Picking  from the st list,  from the nd list and  from the rd list gives the maximum  value equal to % = .

Maximize It! - Hacker Rank Solution

Cartesian product of input iterables.

Learn more about the tool product here: https://docs.python.org/2/library/itertools.html#itertools.product

from itertools import *

K, M = map(int, raw_input().split())
N = []

for _ in xrange(K):
     N.append(map(int, raw_input().split())[1:])        
MAX = -1
for i in product(*N):
    MAX = max(sum(map(lambda x: x**2, i))%M, MAX)
    
print MAX

No comments:

Post a Comment

Powered by Blogger.