HackerRank Maximize It! problem solution in python programming

In this Maximize It! problem we need to develop a python program that can read an integer input separated with lines and then we need to print the maximum value on the output screen.

HackerRank Maximize It! solution in python

Problem solution in Python 2 programming.

# 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
        

Problem solution in Python 3 programming.

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

Problem solution in pypy programming.

import itertools

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

L = list()
for i in range(K):
    l = map(int, raw_input().split())
    n = l[0]
    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

Problem solution in pypy3 programming.

# 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[1] for x in product(*[map(int,input().split()[1:]) for _ in range(args[0])])]))