Algorithm


Problem Name: Python - Maximize It!

Problem Link: https://www.hackerrank.com/challenges/maximize-it/problem?isFullScreen=true  

In this HackerRank Functions in PYTHON problem solution,

You are given a function f(X) = X**2 . You are also given K ists. The i**th list consists of Ni elements. You have to pick one element from each list so that the value from the equation below is maximized:

S = (f(X1) + f(X2) + ... + f(Xk))%M

Xi denotes the element picked from the i**th list . Find the maximized value Smax 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 2 space separated integers K and M.

The next K lines each contains an integer Ni , denoting the number of elements in the i**th list, followed by Ni space separated integers denoting the elements in the list.

Constraints

1 <= K <= 7

1 <= M <= 1000

1 <= Ni <= 7

1 <= Magnitude of element in list <= 10**7

Output Format

Output a single integer denoting the value Smax.

 

Sample Input

3 1000
2 5 4
3 7 8 9 
5 5 7 8 9 10 

Sample Output

206

 

 

Code Examples

#1 Code Example with Python Programming

Code - Python Programming


from itertools import product
x, m = map(int, input().split())
l = []
p = []
q = []
sum = 0
for i in range(x):
    l.append(list(map(int,input().split())))
    l[i].remove(l[i][0])
p = list(product(*l))
for i in range(len(p)):
    sum = 0
    for j in range(x):
        sum += pow(p[i][j], 2)
    q.append(sum%m)
print(max(q))
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Default Arguments in PYTHON solution in Hackerrank
Next
[Solved] Validating Postal Codes in PYTHON solution in Hackerrank