This video presents a compact one-liner solution for the HackerRank Maximize It! problem using Python's itertools.product. We efficiently pick one element from each list, calculate the sum of squares, take the modulo, and determine the maximum value in a single, concise line of code.
How It Works:
Input & List Construction:
Read the number of lists K and the modulo value M.
Build each list by reading the elements (ignoring the count).
#InputHandling #ListComprehension
One-Liner Maximization:
Use itertools.product to generate combinations, calculate sum of squares, take modulo M, and compute the maximum value – all in one line.
#OneLiner #PythonTricks
Output:
Print the maximum value obtained.
#Output #Maximization
Copy and paste the code below into your Jupyter Notebook to try the compact solution!
Code for Compact One-Liner Version (Copy-Paste in Jupyter Notebook):
from itertools import product
if _name_ == '__main__':
k, m = map(int, input().split())
lists = [list(map(int, input().split()))[1:] for _ in range(k)]
print(max(sum(x**2 for x in combo) % m for combo in product(*lists)))