# Maximize It! Python HackerRank Solution

## Maximize It! Python HackerRank Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Maximize It Python Problem with a very easy explanation. In this article, you will get more than one approach to solve this problem. So let's start-

### HackerRank Python Maximize It Solution - Problem Statement

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 contain 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 elements 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 {codeBox}

Sample Output

206 {codeBox}

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! HackerRank Python Solution

### Python Maximize It - Hacker Rank Solution

Approach I: Maximize It HackerRank Python Solution

``` ```# ========================
#       Information
# ========================

# Name: Maximize It! Python HackerRank Problem
# Difficulty: Hard
# Max Score: 50
# Language: Python

# ========================
#         Solution Start
# ========================

import itertools

NUMBER_OF_LISTS, MODULUS = map(int, input().split())
LISTS_OF_LISTS = []

for i in range(0, NUMBER_OF_LISTS):
new_list = list(map(int, input().split()))
del new_list[0]
LISTS_OF_LISTS.append(new_list)

def squared(element):
return element**2

COMBS = list(itertools.product(*LISTS_OF_LISTS))
RESULTS = []

for i in COMBS:
result1 = sum(map(squared, [a for a in i]))
result2 = result1 % MODULUS
RESULTS.append(result2)

print(max(RESULTS))

# Maximize It in Python - HackerRank Solution END
# MyEduWaves``````

Approach II: Maximize It HackerRank Python Solution

``````# ========================
#       Information
# ========================

# Name: Maximize It! Python HackerRank Problem
# Difficulty: Hard
# Max Score: 50
# Language: Python

# ========================
#         Solution Start
# ========================

from itertools import product

K, M = map(int, raw_input().strip().split(' '))

lists = []
for _ in range(K):
lists.append(map(lambda x: x ** 2, map(int, raw_input().strip().split(" ")[1:])))

print max(map(lambda x: x % M, map(sum, product(*lists))))

# Maximize It in Python - HackerRank Solution END
# MyEduWaves``````

Disclaimer: The above Problem ( Python Maximize It ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python Maximize It Hackerrank Problem and try to solve it again.

All the Best!