# Maximum product quadruple (sub-sequence of size 4) in array

Given an integer array, find a maximum product of a quadruple in the array.

Examples:

```Input:  [10, 3, 5, 6, 20]
Output: 6000
Multiplication of 10, 5, 6 and 20

Input:  [-10, -3, -5, -6, -20]
Output: 6000

Input:  [1, -4, 3, -6, 7, 0]
Output: 504
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach 1 (Naive, O() time, O(1) Space)
A simple solution is to check for every quadruple using four nested loops. Below is its implementation-

## C++

 `// A C++ program to find a maximum product of a ` `// quadruple in array of integers ` `#include ` `using` `namespace` `std; ` ` `  `/* Function to find a maximum product of a  ` `   ``quadruple in array of integers of size n */` `int` `maxProduct(``int` `arr[], ``int` `n) ` `{ ` `    ``// if size is less than 4, no quadruple exists ` `    ``if` `(n < 4) ` `        ``return` `-1; ` ` `  `    ``// will contain max product ` `    ``int` `max_product = INT_MIN; ` ` `  `    ``for` `(``int` `i = 0; i < n - 3; i++) ` `        ``for` `(``int` `j = i + 1; j < n - 2; j++) ` `            ``for` `(``int` `k = j + 1; k < n - 1; k++) ` `                ``for` `(``int` `l = k + 1; l < n; l++) ` `                    ``max_product = max(max_product, ` `                   ``arr[i] * arr[j] * arr[k] * arr[l]); ` ` `  `    ``return` `max_product; ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` `    ``int` `arr[] = { 10, 3, 5, 6, 20 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``int` `max = maxProduct(arr, n); ` `    ``if` `(max == -1) ` `        ``cout << ``"No Quadruple Exists"``; ` `    ``else` `        ``cout << ``"Maximum product is "` `<< max; ` `    ``return` `0; ` `} `

## Java

 `// A Java program to find  ` `// a maximum product of a ` `// quadruple in array of  ` `// integers ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` ` `  `/* Function to find a ` `maximum product of a  ` `quadruple in array of  ` `integers of size n */` `static` `int` `maxProduct(``int` `arr[], ` `                      ``int` `n) ` `{ ` `    ``// if size is less than 4,  ` `    ``// no quadruple exists ` `    ``if` `(n < ``4``) ` `        ``return` `-``1``; ` ` `  `    ``// will contain ` `    ``// max product ` `    ``int` `max_product = Integer.MIN_VALUE; ` ` `  `    ``for` `(``int` `i = ``0``;  ` `             ``i < n - ``3``; i++) ` `        ``for` `(``int` `j = i + ``1``;  ` `                 ``j < n - ``2``; j++) ` `            ``for` `(``int` `k = j + ``1``; ` `                     ``k < n - ``1``; k++) ` `                ``for` `(``int` `l = k + ``1``;  ` `                         ``l < n; l++) ` `                    ``max_product = Math.max(max_product, ` `                                            ``arr[i] * arr[j] *  ` `                                            ``arr[k] * arr[l]); ` ` `  `    ``return` `max_product; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `arr[] = { ``10``, ``3``, ``5``, ``6``, ``20` `}; ` `    ``int` `n = arr.length; ` `    ``int` `max = maxProduct(arr, n); ` `    ``if` `(max == -``1``) ` `        ``System.out.println(``"No Quadruple Exists"``); ` `    ``else` `        ``System.out.println(``"Maximum product is "` `+ max); ` `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67 `

## Python3

 `# Python3 program to find a  ` `# maximum product of a  ` `# quadruple in array of  ` `# integers ` `import` `sys ` ` `  `# Function to find a maximum  ` `# product of a quadruple in ` `# array of integers of size n  ` `def` `maxProduct(arr, n): ` `     `  `    ``# if size is less than  ` `    ``# 4, no quadruple exists ` `    ``if` `(n < ``4``): ` `        ``return` `-``1``; ` ` `  `    ``# will contain max product ` `    ``max_product ``=` `-``sys.maxsize; ` ` `  `    ``for` `i ``in` `range``(n ``-` `3``): ` `        ``for` `j ``in` `range``(i ``+` `1``, n ``-` `2``): ` `            ``for` `k ``in` `range``(j ``+` `1``, n ``-` `1``): ` `                ``for` `l ``in` `range``(k ``+` `1``, n): ` `                    ``max_product ``=` `max``(max_product,  ` `                                      ``arr[i] ``*` `arr[j] ``*`  `                                      ``arr[k] ``*` `arr[l]); ` ` `  `    ``return` `max_product; ` ` `  `# Driver Code ` `arr ``=` `[``10``, ``3``, ``5``, ``6``, ``20``]; ` `n ``=` `len``(arr); ` `max` `=` `maxProduct(arr, n); ` ` `  `if` `(``max` `=``=` `-``1``): ` `    ``print``(``"No Quadruple Exists"``); ` `else``: ` `    ``print``(``"Maximum product is"``, ``max``); ` ` `  `# This code is contributed ` `# by rahul `

## C#

 `// A C# program to find  ` `// a maximum product of a ` `// quadruple in array of  ` `// integers ` `using` `System; ` ` `  `class` `GFG ` `{ ` ` `  `/* Function to find a ` `maximum product of a  ` `quadruple in array of  ` `integers of size n */` `static` `int` `maxProduct(``int` `[]arr, ` `                      ``int` `n) ` `{ ` `    ``// if size is less than 4,  ` `    ``// no quadruple exists ` `    ``if` `(n < 4) ` `        ``return` `-1; ` ` `  `    ``// will contain ` `    ``// max product ` `    ``int` `max_product = ``int``.MinValue; ` ` `  `    ``for` `(``int` `i = 0;  ` `             ``i < n - 3; i++) ` `        ``for` `(``int` `j = i + 1;  ` `                 ``j < n - 2; j++) ` `            ``for` `(``int` `k = j + 1; ` `                     ``k < n - 1; k++) ` `                ``for` `(``int` `l = k + 1;  ` `                         ``l < n; l++) ` `                    ``max_product = Math.Max(max_product, ` `                                           ``arr[i] * arr[j] *  ` `                                           ``arr[k] * arr[l]); ` ` `  `    ``return` `max_product; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main ()  ` `{ ` `    ``int` `[]arr = {10, 3, 5, 6, 20}; ` `    ``int` `n = arr.Length; ` `    ``int` `max = maxProduct(arr, n); ` `    ``if` `(max == -1) ` `        ``Console.WriteLine(``"No Quadruple Exists"``); ` `    ``else` `        ``Console.WriteLine(``"Maximum product is "` `+ max); ` `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67 `

## PHP

 ` `

Output :

`Maximum product is 6000`

Approach 2: O(nlogn) Time, O(1) Space

1. Sort the array using some efficient in-place sorting algorithm in ascending order.
2. Let x be the product of last four elements.
3. Let y be the product of first four elements.
4. Let z be the product of first two elements and last two elements.
5. Return the maximum of x, y and z.

Below is its implementation–

## C++

 `// A C++ program to find a maximum product of a ` `// quadruple in array of integers ` `#include ` `using` `namespace` `std; ` ` `  `/* Function to find a maximum product of a quadruple ` `   ``in array of integers of size n */` `int` `maxProduct(``int` `arr[], ``int` `n) ` `{ ` `    ``// if size is less than 4, no quadruple exists ` `    ``if` `(n < 4) ` `        ``return` `-1; ` ` `  `    ``// Sort the array in ascending order ` `    ``sort(arr, arr + n); ` ` `  `    ``int` `x = arr[n - 1] * arr[n - 2] * arr[n - 3] * arr[n - 4]; ` `    ``int` `y = arr[0] * arr[1] * arr[2] * arr[3]; ` `    ``int` `z = arr[0] * arr[1] * arr[n - 1] * arr[n - 2]; ` ` `  `    ``// Return the maximum of x, y and z ` `    ``return` `max(x, max(y, z)); ` `} ` ` `  `// Driver program to test above functions ` `int` `main() ` `{ ` `    ``int` `arr[] = { -10, -3, 5, 6, -20 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``int` `max = maxProduct(arr, n); ` `    ``if` `(max == -1) ` `        ``cout << ``"No Quadruple Exists"``; ` `    ``else` `        ``cout << ``"Maximum product is "` `<< max; ` ` `  `    ``return` `0; ` `} `

## Java

 `// A Java program to find a  ` `// maximum product of a ` `// quadruple in array of integers ` `import` `java.io.*; ` `import` `java.util.Arrays; ` ` `  `class` `GFG  ` `{ ` ` `  `/* Function to find a  ` `maximum product of a quadruple ` `in array of integers of size n */` `static` `int` `maxProduct(``int` `arr[],  ` `                      ``int` `n) ` `{ ` `    ``// if size is less than 4,  ` `    ``// no quadruple exists ` `    ``if` `(n < ``4``) ` `        ``return` `-``1``; ` ` `  `    ``// Sort the array  ` `    ``// in ascending order ` `    ``Arrays.sort(arr); ` ` `  `    ``int` `x = arr[n - ``1``] * arr[n - ``2``] *  ` `            ``arr[n - ``3``] * arr[n - ``4``]; ` `    ``int` `y = arr[``0``] * arr[``1``] * ` `            ``arr[``2``] * arr[``3``]; ` `    ``int` `z = arr[``0``] * arr[``1``] *  ` `            ``arr[n - ``1``] * arr[n - ``2``]; ` ` `  `    ``// Return the maximum ` `    ``// of x, y and z ` `    ``return` `Math.max(x, Math.max(y, z)); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main (String[] args)  ` `{ ` `    ``int` `arr[] = {-``10``, -``3``, ``5``, ``6``, -``20``}; ` `    ``int` `n = arr.length; ` `    ``int` `max = maxProduct(arr, n); ` `    ``if` `(max == -``1``) ` `        ``System.out.println(``"No Quadruple Exists"``); ` `    ``else` `        ``System.out.println(``"Maximum product is "` `+ ` `                                             ``max); ` `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67 `

## Python 3

 `# A Python 3 program to find a maximum  ` `# product of a quadruple in array of integers ` ` `  `# Function to find a maximum product of a  ` `# quadruple in array of integers of size n  ` `def` `maxProduct(arr, n): ` ` `  `    ``# if size is less than 4, no  ` `    ``# quadruple exists ` `    ``if` `(n < ``4``): ` `        ``return` `-``1` ` `  `    ``# Sort the array in ascending order ` `    ``arr.sort() ` ` `  `    ``x ``=` `(arr[n ``-` `1``] ``*` `arr[n ``-` `2``] ``*` `         ``arr[n ``-` `3``] ``*` `arr[n ``-` `4``]) ` `    ``y ``=` `arr[``0``] ``*` `arr[``1``] ``*` `arr[``2``] ``*` `arr[``3``] ` `    ``z ``=` `(arr[``0``] ``*` `arr[``1``] ``*`  `         ``arr[n ``-` `1``] ``*` `arr[n ``-` `2``]) ` ` `  `    ``# Return the maximum of x, y and z ` `    ``return` `max``(x, ``max``(y, z)) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``arr ``=` `[ ``-``10``, ``-``3``, ``5``, ``6``, ``-``20` `] ` `    ``n ``=` `len``(arr) ` `    ``max` `=` `maxProduct(arr, n) ` `    ``if` `(``max` `=``=` `-``1``): ` `        ``print``(``"No Quadruple Exists"``) ` `    ``else``: ` `        ``print``(``"Maximum product is"``, ``max``) ` ` `  `# This code is contributed by ita_c `

## C#

 `// A C# program to find a  ` `// maximum product of a ` `// quadruple in array of  ` `// integers ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `/* Function to find a  ` `maximum product of a  ` `quadruple in array of ` `integers of size n */` `static` `int` `maxProduct(``int` `[]arr,  ` `                      ``int` `n) ` `{ ` `    ``// if size is less than 4,  ` `    ``// no quadruple exists ` `    ``if` `(n < 4) ` `        ``return` `-1; ` ` `  `    ``// Sort the array  ` `    ``// in ascending order ` `    ``Array.Sort(arr); ` ` `  `    ``int` `x = arr[n - 1] * arr[n - 2] *  ` `            ``arr[n - 3] * arr[n - 4]; ` `    ``int` `y = arr[0] * arr[1] * ` `            ``arr[2] * arr[3]; ` `    ``int` `z = arr[0] * arr[1] *  ` `            ``arr[n - 1] * arr[n - 2]; ` ` `  `    ``// Return the maximum ` `    ``// of x, y and z ` `    ``return` `Math.Max(x, Math.Max(y, z)); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main ()  ` `{ ` `    ``int` `[]arr = {-10, -3, 5, 6, -20}; ` `    ``int` `n = arr.Length; ` `    ``int` `max = maxProduct(arr, n); ` `    ``if` `(max == -1) ` `        ``Console.WriteLine(``"No Quadruple Exists"``); ` `    ``else` `        ``Console.WriteLine(``"Maximum product is "` `+ ` `                                            ``max); ` ` `  `} ` `} ` ` `  `// This code is contributed ` `// by anuj_67 `

## PHP

 `

Output :

`Maximum product is 6000`

Approach 3: O(n) Time, O(1) Space

1. Scan the array and compute Maximum, second maximum, third maximum and fourth maximum element present in the array.
2. Scan the array and compute Minimum, second minimum, third minimum and fourth minimum element present in the array.
3. Return the maximum of (product of Maximum, second maximum, third maximum and fourth maximum), (product of Minimum, second minimum, third minimum and fourth minimum) and (product of Maximum, second maximum, Minimum, second minimum).

Note – Step 1 and Step 2 can be done in single traversal of the array.

Below is its implementation–

## C++

 `// A O(n) C++ program to find maximum quadruple in ` `// an array. ` `#include ` `using` `namespace` `std; ` ` `  `/* Function to find a maximum product of a quadruple ` `   ``in array of integers of size n */` `int` `maxProduct(``int` `arr[], ``int` `n) ` `{ ` `    ``// if size is less than 4, no quadruple exists ` `    ``if` `(n < 4) ` `        ``return` `-1; ` ` `  `    ``// Initialize Maximum, second maximum, third ` `    ``// maximum and fourth maximum element ` `    ``int` `maxA = INT_MIN, maxB = INT_MIN, ` `        ``maxC = INT_MIN, maxD = INT_MIN; ` ` `  `    ``// Initialize Minimum, second minimum, third ` `    ``// minimum and fourth minimum element ` `    ``int` `minA = INT_MAX, minB = INT_MAX,  ` `        ``minC = INT_MAX, minD = INT_MAX; ` ` `  `    ``for` `(``int` `i = 0; i < n; i++) { ` ` `  `        ``// Update Maximum, second maximum, third ` `        ``// maximum and fourth maximum element ` `        ``if` `(arr[i] > maxA) { ` `            ``maxD = maxC; ` `            ``maxC = maxB; ` `            ``maxB = maxA; ` `            ``maxA = arr[i]; ` `        ``} ` ` `  `        ``// Update second maximum, third maximum ` `        ``// and fourth maximum element ` `        ``else` `if` `(arr[i] > maxB) { ` `            ``maxD = maxC; ` `            ``maxC = maxB; ` `            ``maxB = arr[i]; ` `        ``} ` ` `  `        ``// Update third maximum and ` `        ``// fourth maximum element ` `        ``else` `if` `(arr[i] > maxC) { ` `            ``maxD = maxC; ` `            ``maxC = arr[i]; ` `        ``} ` ` `  `        ``// Update fourth maximum element ` `        ``else` `if` `(arr[i] > maxD) ` `            ``maxD = arr[i]; ` ` `  `        ``// Update Minimum, second minimum ` `        ``// third minimum and fourth minimum element ` `        ``if` `(arr[i] < minA) { ` `            ``minD = minC; ` `            ``minC = minB; ` `            ``minB = minA; ` `            ``minA = arr[i]; ` `        ``} ` ` `  `        ``// Update second minimum, third ` `        ``// minimum and fourth minimum element ` `        ``else` `if` `(arr[i] < minB) { ` `            ``minD = minC; ` `            ``minC = minB; ` `            ``minB = arr[i]; ` `        ``} ` ` `  `        ``// Update third minimum and ` `        ``// fourth minimum element ` `        ``else` `if` `(arr[i] < minC) { ` `            ``minD = minC; ` `            ``minC = arr[i]; ` `        ``} ` ` `  `        ``// Update fourth minimum element ` `        ``else` `if` `(arr[i] < minD) ` `            ``minD = arr[i]; ` `    ``} ` ` `  `    ``int` `x = maxA * maxB * maxC * maxD; ` `    ``int` `y = minA * minB * minC * minD; ` `    ``int` `z = minA * minB * maxA * maxB; ` `    ``// Return the maximum of x, y and z ` `    ``return` `max(x, max(y, z)); ` `} ` ` `  `// Driver program to test above function ` `int` `main() ` `{ ` `    ``int` `arr[] = { 1, -4, 3, -6, 7, 0 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]); ` `    ``int` `max = maxProduct(arr, n); ` `    ``if` `(max == -1) ` `        ``cout << ``"No Quadruple Exists"``; ` `    ``else` `        ``cout << ``"Maximum product is "` `<< max; ` `    ``return` `0; ` `} `

## Java

 `// A O(n) Java program to find maximum  ` `// quadruple in an array. ` `class` `GFG  ` `{ ` ` `  `    ``/* Function to find a maximum product of a  ` `    ``quadruple in array of integers of size n */` `    ``static` `int` `maxProduct(``int` `arr[], ``int` `n)  ` `    ``{ ` `        ``// if size is less than 4, no quadruple exists ` `        ``if` `(n < ``4``) { ` `            ``return` `-``1``; ` `        ``} ` `         `  `        ``// Initialize Maximum, second maximum, third ` `        ``// maximum and fourth maximum element ` `        ``int` `maxA = Integer.MIN_VALUE,  ` `            ``maxB = Integer.MIN_VALUE, ` `            ``maxC = Integer.MIN_VALUE,  ` `            ``maxD = Integer.MIN_VALUE; ` ` `  `        ``// Initialize Minimum, second minimum, third ` `        ``// minimum and fourth minimum element ` `        ``int` `minA = Integer.MAX_VALUE,  ` `            ``minB = Integer.MAX_VALUE, ` `            ``minC = Integer.MAX_VALUE,  ` `            ``minD = Integer.MAX_VALUE; ` ` `  `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` ` `  `            ``// Update Maximum, second maximum, third ` `            ``// maximum and fourth maximum element ` `            ``if` `(arr[i] > maxA) { ` `                ``maxD = maxC; ` `                ``maxC = maxB; ` `                ``maxB = maxA; ` `                ``maxA = arr[i]; ` `            ``}  ` `             `  `            ``// Update second maximum, third maximum ` `            ``// and fourth maximum element ` `            ``else` `if` `(arr[i] > maxB) { ` `                ``maxD = maxC; ` `                ``maxC = maxB; ` `                ``maxB = arr[i]; ` `            ``}  ` `             `  `            ``// Update third maximum and ` `            ``// fourth maximum element ` `            ``else` `if` `(arr[i] > maxC) { ` `                ``maxD = maxC; ` `                ``maxC = arr[i]; ` `            ``}  ` `             `  `            ``// Update fourth maximum element ` `            ``else` `if` `(arr[i] > maxD) { ` `                ``maxD = arr[i]; ` `            ``} ` ` `  `            ``// Update Minimum, second minimum ` `            ``// third minimum and fourth minimum element ` `            ``if` `(arr[i] < minA) { ` `                ``minD = minC; ` `                ``minC = minB; ` `                ``minB = minA; ` `                ``minA = arr[i]; ` `            ``}  ` `             `  `            ``// Update second minimum, third ` `            ``// minimum and fourth minimum element ` `            ``else` `if` `(arr[i] < minB) { ` `                ``minD = minC; ` `                ``minC = minB; ` `                ``minB = arr[i]; ` `            ``}  ` `             `  `            ``// Update third minimum and ` `            ``// fourth minimum element ` `            ``else` `if` `(arr[i] < minC) { ` `                ``minD = minC; ` `                ``minC = arr[i]; ` `            ``}  ` `             `  `            ``// Update fourth minimum element ` `            ``else` `if` `(arr[i] < minD) { ` `                ``minD = arr[i]; ` `            ``} ` `        ``} ` ` `  `        ``int` `x = maxA * maxB * maxC * maxD; ` `        ``int` `y = minA * minB * minC * minD; ` `        ``int` `z = minA * minB * maxA * maxB; ` `         `  `        ``// Return the maximum of x, y and z ` `        ``return` `Math.max(x, Math.max(y, z)); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `arr[] = { ``1``, -``4``, ``3``, -``6``, ``7``, ``0` `}; ` `        ``int` `n = arr.length; ` `        ``int` `max = maxProduct(arr, n); ` `        ``if` `(max == -``1``) ` `            ``System.out.println(``"No Quadruple Exists"``); ` `        ``else` `            ``System.out.println(``"Maximum product is "` `+ max); ` `    ``} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python 3

 `# A O(n) Python 3 program to find maximum quadruple in# an array. ` `import` `sys ` ` `  `# Function to find a maximum product of a quadruple ` `# in array of integers of size n  ` `def` `maxProduct(arr, n): ` `    ``# if size is less than 4, no quadruple exists ` `    ``if` `(n < ``4``): ` `        ``return` `-``1` ` `  `    ``# Initialize Maximum, second maximum, third ` `    ``# maximum and fourth maximum element ` `    ``maxA ``=` `-``sys.maxsize ``-` `1` `    ``maxB ``=` `-``sys.maxsize ``-` `1` `    ``maxC ``=` `-``sys.maxsize ``-` `1` `    ``maxD ``=` `-``sys.maxsize ``-` `1` ` `  `    ``# Initialize Minimum, second minimum, third ` `    ``# minimum and fourth minimum element ` `    ``minA ``=` `sys.maxsize ` `    ``minB ``=` `sys.maxsize ` `    ``minC ``=` `sys.maxsize ` `    ``minD ``=` `sys.maxsize ` ` `  `    ``for` `i ``in` `range``(n): ` `        ``# Update Maximum, second maximum, third ` `        ``# maximum and fourth maximum element ` `        ``if` `(arr[i] > maxA): ` `            ``maxD ``=` `maxC ` `            ``maxC ``=` `maxB ` `            ``maxB ``=` `maxA ` `            ``maxA ``=` `arr[i] ` ` `  `        ``# Update second maximum, third maximum ` `        ``# and fourth maximum element ` `        ``elif` `(arr[i] > maxB): ` `            ``maxD ``=` `maxC ` `            ``maxC ``=` `maxB ` `            ``maxB ``=` `arr[i] ` ` `  `        ``# Update third maximum and ` `        ``# fourth maximum element ` `        ``elif` `(arr[i] > maxC): ` `            ``maxD ``=` `maxC ` `            ``maxC ``=` `arr[i] ` ` `  `        ``# Update fourth maximum element ` `        ``elif` `(arr[i] > maxD): ` `            ``maxD ``=` `arr[i] ` ` `  `        ``# Update Minimum, second minimum ` `        ``# third minimum and fourth minimum element ` `        ``if` `(arr[i] < minA): ` `            ``minD ``=` `minC ` `            ``minC ``=` `minB ` `            ``minB ``=` `minA ` `            ``minA ``=` `arr[i] ` ` `  `        ``# Update second minimum, third ` `        ``# minimum and fourth minimum element ` `        ``elif` `(arr[i] < minB): ` `            ``minD ``=` `minC ` `            ``minC ``=` `minB ` `            ``minB ``=` `arr[i] ` ` `  `        ``# Update third minimum and ` `        ``# fourth minimum element ` `        ``elif` `(arr[i] < minC): ` `            ``minD ``=` `minC ` `            ``minC ``=` `arr[i] ` ` `  `        ``# Update fourth minimum element ` `        ``elif` `(arr[i] < minD): ` `            ``minD ``=` `arr[i] ` ` `  `    ``x ``=` `maxA ``*` `maxB ``*` `maxC ``*` `maxD ` `    ``y ``=` `minA ``*` `minB ``*` `minC ``*` `minD ` `    ``z ``=` `minA ``*` `minB ``*` `maxA ``*` `maxB ` `    ``# Return the maximum of x, y and z ` `    ``return` `max``(x, ``max``(y, z)) ` ` `  `# Driver program to test above function ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``arr ``=` `[``1``, ``-``4``, ``3``, ``-``6``, ``7``, ``0``] ` `    ``n ``=` `len``(arr) ` `    ``max1 ``=` `maxProduct(arr, n) ` `    ``if` `(max1 ``=``=` `-``1``): ` `        ``print``(``"No Quadruple Exists"``) ` `    ``else``: ` `        ``print``(``"Maximum product is"``, max1) ` ` `  `# This code is contributed by Surendra_Gangwar `

## C#

 `// A O(n) C# program to find maximum  ` `// quadruple in an array. ` `using` `System; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``/* Function to find a maximum product of a  ` `    ``quadruple in array of integers of size n */` `    ``static` `int` `maxProduct(``int` `[]arr, ``int` `n)  ` `    ``{ ` `        ``// if size is less than 4, no quadruple exists ` `        ``if` `(n < 4)  ` `        ``{ ` `            ``return` `-1; ` `        ``} ` `         `  `        ``// Initialize Maximum, second maximum, third ` `        ``// maximum and fourth maximum element ` `        ``int` `maxA = ``int``.MinValue,  ` `            ``maxB = ``int``.MinValue, ` `            ``maxC = ``int``.MinValue,  ` `            ``maxD = ``int``.MinValue; ` ` `  `        ``// Initialize Minimum, second minimum, third ` `        ``// minimum and fourth minimum element ` `        ``int` `minA = ``int``.MaxValue,  ` `            ``minB = ``int``.MaxValue, ` `            ``minC = ``int``.MaxValue,  ` `            ``minD = ``int``.MaxValue; ` ` `  `        ``for` `(``int` `i = 0; i < n; i++)  ` `        ``{ ` ` `  `            ``// Update Maximum, second maximum, third ` `            ``// maximum and fourth maximum element ` `            ``if` `(arr[i] > maxA)  ` `            ``{ ` `                ``maxD = maxC; ` `                ``maxC = maxB; ` `                ``maxB = maxA; ` `                ``maxA = arr[i]; ` `            ``}  ` `             `  `            ``// Update second maximum, third maximum ` `            ``// and fourth maximum element ` `            ``else` `if` `(arr[i] > maxB) ` `            ``{ ` `                ``maxD = maxC; ` `                ``maxC = maxB; ` `                ``maxB = arr[i]; ` `            ``}  ` `             `  `            ``// Update third maximum and ` `            ``// fourth maximum element ` `            ``else` `if` `(arr[i] > maxC)  ` `            ``{ ` `                ``maxD = maxC; ` `                ``maxC = arr[i]; ` `            ``}  ` `             `  `            ``// Update fourth maximum element ` `            ``else` `if` `(arr[i] > maxD)  ` `            ``{ ` `                ``maxD = arr[i]; ` `            ``} ` ` `  `            ``// Update Minimum, second minimum ` `            ``// third minimum and fourth minimum element ` `            ``if` `(arr[i] < minA)  ` `            ``{ ` `                ``minD = minC; ` `                ``minC = minB; ` `                ``minB = minA; ` `                ``minA = arr[i]; ` `            ``}  ` `             `  `            ``// Update second minimum, third ` `            ``// minimum and fourth minimum element ` `            ``else` `if` `(arr[i] < minB) ` `            ``{ ` `                ``minD = minC; ` `                ``minC = minB; ` `                ``minB = arr[i]; ` `            ``}  ` `             `  `            ``// Update third minimum and ` `            ``// fourth minimum element ` `            ``else` `if` `(arr[i] < minC) ` `            ``{ ` `                ``minD = minC; ` `                ``minC = arr[i]; ` `            ``}  ` `             `  `            ``// Update fourth minimum element ` `            ``else` `if` `(arr[i] < minD)  ` `            ``{ ` `                ``minD = arr[i]; ` `            ``} ` `        ``} ` ` `  `        ``int` `x = maxA * maxB * maxC * maxD; ` `        ``int` `y = minA * minB * minC * minD; ` `        ``int` `z = minA * minB * maxA * maxB; ` `         `  `        ``// Return the maximum of x, y and z ` `        ``return` `Math.Max(x, Math.Max(y, z)); ` `    ``} ` ` `  `    ``// Driver Code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``int` `[]arr = { 1, -4, 3, -6, 7, 0 }; ` `        ``int` `n = arr.Length; ` `        ``int` `max = maxProduct(arr, n); ` `        ``if` `(max == -1) ` `            ``Console.Write(``"No Quadruple Exists"``); ` `        ``else` `            ``Console.Write(``"Maximum product is "` `+ max); ` `    ``} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## PHP

 ` ``\$maxA``)  ` `        ``{ ` `            ``\$maxD` `= ``\$maxC``; ` `            ``\$maxC` `= ``\$maxB``; ` `            ``\$maxB` `= ``\$maxA``; ` `            ``\$maxA` `= ``\$arr``[``\$i``]; ` `        ``} ` ` `  `        ``// Update second maximum, third maximum ` `        ``// and fourth maximum element ` `        ``elseif` `(``\$arr``[``\$i``] > ``\$maxB``)  ` `        ``{ ` `            ``\$maxD` `= ``\$maxC``; ` `            ``\$maxC` `= ``\$maxB``; ` `            ``\$maxB` `= ``\$arr``[``\$i``]; ` `        ``} ` ` `  `        ``// Update third maximum and fourth  ` `        ``// maximum element ` `        ``elseif` `(``\$arr``[``\$i``] > ``\$maxC``) ` `        ``{ ` `            ``\$maxD` `= ``\$maxC``; ` `            ``\$maxC` `= ``\$arr``[``\$i``]; ` `        ``} ` ` `  `        ``// Update fourth maximum element ` `        ``elseif` `(``\$arr``[``\$i``] > ``\$maxD``) ` `            ``\$maxD` `= ``\$arr``[``\$i``]; ` ` `  `        ``// Update Minimum, second minimum, ` `        ``// third minimum and fourth minimum element ` `        ``if` `(``\$arr``[``\$i``] < ``\$minA``)  ` `        ``{ ` `            ``\$minD` `= ``\$minC``; ` `            ``\$minC` `= ``\$minB``; ` `            ``\$minB` `= ``\$minA``; ` `            ``\$minA` `= ``\$arr``[``\$i``]; ` `        ``} ` ` `  `        ``// Update second minimum, third ` `        ``// minimum and fourth minimum element ` `        ``elseif` `(``\$arr``[``\$i``] < ``\$minB``)  ` `        ``{ ` `            ``\$minD` `= ``\$minC``; ` `            ``\$minC` `= ``\$minB``; ` `            ``\$minB` `= ``\$arr``[``\$i``]; ` `        ``} ` ` `  `        ``// Update third minimum and ` `        ``// fourth minimum element ` `        ``elseif` `(``\$arr``[``\$i``] < ``\$minC``)  ` `        ``{ ` `            ``\$minD` `= ``\$minC``; ` `            ``\$minC` `= ``\$arr``[``\$i``]; ` `        ``} ` ` `  `        ``// Update fourth minimum element ` `        ``elseif` `(``\$arr``[``\$i``] < ``\$minD``) ` `            ``\$minD` `= ``\$arr``[``\$i``]; ` `    ``} ` ` `  `    ``\$x` `= ``\$maxA` `* ``\$maxB` `* ``\$maxC` `* ``\$maxD``; ` `    ``\$y` `= ``\$minA` `* ``\$minB` `* ``\$minC` `* ``\$minD``; ` `    ``\$z` `= ``\$minA` `* ``\$minB` `* ``\$maxA` `* ``\$maxB``; ` `     `  `    ``// Return the maximum of x, y and z ` `    ``return` `max(``\$x``, max(``\$y``, ``\$z``)); ` `} ` ` `  `// Driver Code ` `\$arr` `= ``array``( 1, -4, 3, -6, 7, 0 ); ` `\$n` `= ``count``(``\$arr``); ` `\$max` `= maxProduct(``\$arr``, ``\$n``); ` `if` `(``\$max` `== -1) ` `    ``echo` `"No Quadruple Exists"``; ` `else` `    ``echo` `"Maximum product is "` `. ``\$max``; ` `     `  `// This code is contributed by Rajput-Ji ` `?> `

Output :

`Maximum product is 504`

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.