304 North Cardinal St.
Dorchester Center, MA 02124

# Maximize It! Hacker Rank solution – Queslers

### Problem : Maximize It! Hacker Rank solution

You are given a function f(x) = x^2. You are also given k lists. The ith 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 ith 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 ith 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^9

#### 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
``````

#### Explanation :

Picking 5 from the 1st list, 9 from the 2nd list and 10 from the 3rd list gives the maximum S value equal to (5^2 + 9^2 + 10^2)%1000 =206.

### Maximize It! Hacker Rank solution in python 2

``````# Enter your code here. Read input from STDIN. Print output to STDOUT

K, M = map(int, raw_input().strip().split(' '))
ls = []
for i in range(K):
l = map(int, raw_input().strip().split(' ')[1:])
ls.append(l)

level = len(ls) - 1
res = 0
def loopf(ls, level, s, m):
global res
if level < 0:
if s > res:
res = s
return
for i in ls[level]:
loopf(ls, level-1, (s + i ** 2 % m) % m, m)
loopf(ls, level, 0, M)
print res``````

### Maximize It! Hacker Rank solution in python 3

``````# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import product

K,M = map(int,input().split())
N = (list(map(int, input().split()))[1:] for _ in range(K))
results = map(lambda x: sum(i**2 for i in x)%M, product(*N))
print(max(results))``````

### Maximize It! Hacker Rank solution in pypy

``````import itertools

(K, N) = map(int, raw_input().split())

L = list()
for i in range(K):
l = map(int, raw_input().split())
n = l
L.append(l[1:])
assert len(L[i]) == n

S_max = 0
L_max = None

for l in itertools.product(*L):
s = sum([x**2 for x in l]) % N

if s > S_max:
S_max = s
L_max = l

print S_max``````

### Maximize It! Hacker Rank solution in pypy 3

``````# Enter your code here. Read input from STDIN. Print output to STDOUT
from itertools import product
args = list(map(int, input().split()))
print(max([sum([y**2 for y in x])%args for x in product(*[map(int,input().split()[1:]) for _ in range(args)])]))``````
##### Maximize It! Hacker Rank Solution Review:

In our experience, we suggest you solve this Maximize It! Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Maximize It! is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution

##### Maximize It! Hacker Rank Solution Conclusion:

I hope this Maximize It! Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Mini-Max Sum Hacker Rank Solution

String Validators Hacker Rank Solution

Text Alignment Hacker Rank solution

String validators Hacker Rank Solution