# Python Program to find Maximum Product Quadruple in an Array or List

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Given a list, the task is to write a python program to find the Maximum Product Quadruple in the given array or list in Python.

Examples:

Example1:

Input:

Given list =[-9, -24, 15, 3, 19, 23, 18, 11, 10, 7, 6]

Output:

The maximum product quadruple in the given list [-9, -24, 15, 3, 19, 23, 18, 11, 10, 7, 6] is :
[ 117990 ]

Example2:

Input:

Given list =[8, 11, 4, 6, 9, -7, 22, 9, 15]

Output:

The maximum product quadruple in the given list [8, 11, 4, 6, 9, -7, 22, 9, 15] is :
[ 32670 ]

## Program to find Maximum Product Quadruple in an Array or List in Python

Below are the ways to find the Maximum Product Quadruple in the given array or list in Python.

### Method #1: Using Sorting (Static Input)

Approach:

• Give the list as static input and store it in a variable.
• Sort the given list in ascending order using the built-in sort() function.
• Find the product of the last four elements in the above-sorted list and store it in a variable maxproduct1.
• Find the product of the first four elements in the above-sorted list and store it in a variable maxproduct2.
• Find the product of the first two elements and last two elements in the above-sorted list and store it in a variable maxproduct3.
• Find the maximum value of the above three variables maxproduct1,maxproduct2,maxproduct3 using the max() function.
• Print the maximum product.
• The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvnlist = [-9, -24, 15, 3, 19, 23, 18, 11, 10, 7, 6]
print('The maximum product quadruple in the given list', gvnlist, 'is :')
# Sort the given list in ascending order using the built-in sort() function.
gvnlist.sort()
# Find the product of the last four elements in the above-sorted list
# and store it in a variable maxproduct1.
maxproduct1 = gvnlist[-1]*gvnlist[-2]*gvnlist[-3]*gvnlist[-4]
# Find the product of the first four elements in the above-sorted list
# and store it in a variable
maxproduct2 = gvnlist[0]*gvnlist[1]*gvnlist[2]*gvnlist[3]
# Find the product of the first two elements and last two elements
# in the above-sorted list and store it in a variable maxproduct3.
maxproduct3 = gvnlist[0]*gvnlist[1]*gvnlist[-1]*gvnlist[-2]
# Find the maximum value of the above three variables
# maxproduct1,maxproduct2,maxproduct3 using the max() function.
maxproductval = max(maxproduct1, maxproduct2, maxproduct3)
# Print the maximum product.
print('[', maxproductval, ']')


Output:

The maximum product quadruple in the given list [-9, -24, 15, 3, 19, 23, 18, 11, 10, 7, 6] is :
[ 117990 ]

Time complexity: O(n*logn) where n is the length of the given list.

### Method #2: Using Sorting (User Input)

Approach:

• Give the list as user input using list(),map(),input(),and split() functions.
• Store it in a variable.
• Sort the given list in ascending order using the built-in sort() function.
• Find the product of the last four elements in the above-sorted list and store it in a variable maxproduct1.
• Find the product of the first four elements in the above-sorted list and store it in a variable maxproduct2.
• Find the product of the first two elements and last two elements in the above-sorted list and store it in a variable maxproduct3.
• Find the maximum value of the above three variables maxproduct1,maxproduct2,maxproduct3 using the max() function.
• Print the maximum product.
• The Exit of the Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvnlist = list(map(int, input(
'Enter some random List Elements separated by spaces = ').split()))
print('The maximum product quadruple in the given list', gvnlist, 'is :')
# Sort the given list in ascending order using the built-in sort() function.
gvnlist.sort()
# Find the product of the last four elements in the above-sorted list
# and store it in a variable maxproduct1.
maxproduct1 = gvnlist[-1]*gvnlist[-2]*gvnlist[-3]*gvnlist[-4]
# Find the product of the first four elements in the above-sorted list
# and store it in a variable
maxproduct2 = gvnlist[0]*gvnlist[1]*gvnlist[2]*gvnlist[3]
# Find the product of the first two elements and last two elements
# in the above-sorted list and store it in a variable maxproduct3.
maxproduct3 = gvnlist[0]*gvnlist[1]*gvnlist[-1]*gvnlist[-2]
# Find the maximum value of the above three variables
# maxproduct1,maxproduct2,maxproduct3 using the max() function.
maxproductval = max(maxproduct1, maxproduct2, maxproduct3)
# Print the maximum product.
print('[', maxproductval, ']')


Output:

Enter some random List Elements separated by spaces = 8 11 4 6 9 -7 22 9 15
The maximum product quadruple in the given list [8, 11, 4, 6, 9, -7, 22, 9, 15] is :
[ 32670 ]

Time complexity: O(n*logn) where n is the length of the given list.
Related Programs: