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
Approach 1 (Naive, O(
Steps to solve this problem:
1.check if n is smaller than n than return -1.
2.declare a variable max_product=INT_MIN.
3.iterate through i=0 till n:
*iterate through j=i+1 till n-2:
*iterate through k=j+1 till n-1:
*iterate through l-k+1 till n:
*update max_product to max of max_product and arr[i]*arr[j]*arr[k]*arr[l].
4.return max_product.
A simple solution is to check for every quadruple using four nested loops. Below is its implementation:
Implementation:
// A C++ program to find a maximum product of a // quadruple in array of integers #include <bits/stdc++.h> 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;
} |
// 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 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 |
// 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 // PHP 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 function maxProduct( $arr , $n )
{ // if size is less than
// 4, no quadruple exists
if ( $n < 4)
return -1;
// will contain max product
$max_product = PHP_INT_MIN;
for ( $i = 0; $i < $n - 3; $i ++)
for ( $j = $i + 1; $j < $n - 2; $j ++)
for ( $k = $j + 1; $k < $n - 1; $k ++)
for ( $l = $k + 1; $l < $n ; $l ++)
$max_product = max( $max_product ,
$arr [ $i ] * $arr [ $j ] *
$arr [ $k ] * $arr [ $l ]);
return $max_product ;
} // Driver Code $arr = array (10, 3, 5, 6, 20);
$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 anuj_67 ?> |
<script> // A Javascript 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 */ function maxProduct(arr,n)
{
// if size is less than 4,
// no quadruple exists
if (n < 4)
return -1;
// will contain
// max product
let max_product = Number.MIN_VALUE;
for (let i = 0;
i < n - 3; i++)
for (let j = i + 1;
j < n - 2; j++)
for (let k = j + 1;
k < n - 1; k++)
for (let 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
let arr=[10, 3, 5, 6, 20 ];
let n = arr.length;
let max = maxProduct(arr, n);
if (max == -1)
document.write( "No Quadruple Exists" );
else
document.write( "Maximum product is " + max);
// This code is contributed by avanitrachhadiya2155
</script> |
Output
Maximum product is 6000
Approach 2: O(nlogn) Time, O(1) Space
- Sort the array using some efficient in-place sorting algorithm in ascending order.
- Let x be the product of last four elements.
- Let y be the product of first four elements.
- Let z be the product of first two elements and last two elements.
- Return the maximum of x, y and z.
Below is its implementation
// A C++ program to find a maximum product of a // quadruple in array of integers #include <bits/stdc++.h> 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;
} |
// 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 |
# 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 |
// 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 // A PHP 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 */
function maxProduct( $arr , $n )
{ // if size is less than 4,
// no quadruple exists
if ( $n < 4)
return -1;
// Sort the array
// in ascending order
sort( $arr );
$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 $arr = array (-10, -3, 5, 6, -20);
$n = sizeof( $arr );
$max = maxProduct( $arr , $n );
if ( $max == -1)
echo "No Quadruple Exists" ;
else
echo "Maximum product is " . $max ;
// This code is contributed // by Akanksha Rai(Abby_akku) |
<script> // A Javascript 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 */ function 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( function (a,b){ return a-b;});
let x = arr[n - 1] * arr[n - 2] *
arr[n - 3] * arr[n - 4];
let y = arr[0] * arr[1] *
arr[2] * arr[3];
let 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
let arr=[-10, -3, 5, 6, -20];
let n = arr.length;
let max = maxProduct(arr, n);
if (max == -1)
document.write( "No Quadruple Exists" );
else
document.write( "Maximum product is " +
max);
// This code is contributed by rag2127 </script> |
Output
Maximum product is 6000
Approach 3: O(n) Time, O(1) Space
- Scan the array and compute Maximum, second maximum, third maximum, and fourth maximum element present in the array.
- Scan the array and compute Minimum, second minimum, third minimum, and fourth minimum element present in the array.
- 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
// A O(n) C++ program to find maximum quadruple in // an array. #include <bits/stdc++.h> 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;
} |
// 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 |
# 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 |
// 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 // A O(n) PHP program to find maximum // quadruple in an array. /* Function to find a maximum product of a quadruple in array of integers of size n */ function 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 = -2147483648; $maxB = -2147483648;
$maxC = -2147483648; $maxD = -2147483648;
// Initialize Minimum, second minimum, third
// minimum and fourth minimum element
$minA = 2147483647; $minB = 2147483647;
$minC = 2147483647; $minD = 2147483647;
for ( $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
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 ?> |
<script> // A O(n) Javascript program to find maximum
// quadruple in an array.
/* Function to find a maximum product of a
quadruple in array of integers of size n */
function 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
let maxA = Number.MIN_VALUE,
maxB = Number.MIN_VALUE,
maxC = Number.MIN_VALUE,
maxD = Number.MIN_VALUE;
// Initialize Minimum, second minimum, third
// minimum and fourth minimum element
let minA = Number.MAX_VALUE,
minB = Number.MAX_VALUE,
minC = Number.MAX_VALUE,
minD = Number.MAX_VALUE;
for (let 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];
}
}
let x = maxA * maxB * maxC * maxD;
let y = minA * minB * minC * minD;
let z = minA * minB * maxA * maxB;
// Return the maximum of x, y and z
return Math.max(x, Math.max(y, z));
}
let arr = [ 1, -4, 3, -6, 7, 0 ];
let n = arr.length;
let max = maxProduct(arr, n);
if (max == -1)
document.write( "No Quadruple Exists" );
else
document.write( "Maximum product is " + max);
// This code is contributed by divyeshrabadiya07. </script> |
Output
Maximum product is 504