
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum product quadruple (sub-sequence of size 4) in array in C++
In this problem, we are given an array arr[]. Our task is to create a program to find the Maximum product quadruple (sub-sequence of size 4) in array in C++.
Code description − Here, we need to find a quadruple (sub-sequence of size 4) such that the product of all elements is maximum.
Let’s take an example to understand the problem,
Input
arr[] = {4, -2, 5, -6, 8}
Output
840
Explanation
The quadruple, (-3, 5, -7, 8) with product 840.
Solution Approach
There can be multiple solutions to a given problem.
One simple solution is using the direct method by traversing the array. Then finding all possible quadruples in the array. Finding their product and then comparing it to find the maximum product quadruple.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; int max(int a, int b){ if(a > b) return a; return b; } int findMaxProdQuad(int arr[], int n){ int maxProd = 0; int prod = 1; for (int i = 0; i <= n - 4; i++) for (int j = i + 1; j <= n - 3; j++) for (int k = j + 1; k <= n - 2; k++) for (int l = k + 1; l <= n - 1; l++) { prod = arr[i] * arr[j] * arr[k] * arr[l]; maxProd = max(maxProd, prod); prod = 1; } return maxProd; } int main(){ int arr[] = {4, -2, 5, -6, 8}; int n = sizeof(arr) / sizeof(arr[0]); cout<<"Maximum product of quadruple is "<<findMaxProdQuad(arr, n); return 0; }
Output
Maximum product of quadruple is 480
Another way to find the quadruple with maximum product is to find four maximum elements of the array and four minimum elements of the array.
Let’s say mx1, mx2, mx3, mx4 are the first four maximum numbers. And mn1, mn2, mn3, mn4 are the first four minimum numbers of the array. Then find the values of
1. mx1 * mx2 * mx3 * mx4 2. mn1 * mn2 * mn3 * mn4 3. mx1 * mx2 * mn1 * mn2
And return the maximum of these three product values which will give the maximum product quadruple. And all cases are considered.
Program to show the implementation of our algorithm
Example
#include <iostream> using namespace std; int max(int a, int b){ if(a > b) return a; return b; } int findMaxProdQuad(int arr[], int n) { int mx1 = -1000, mx2 = -1000, mx3 = -10000, mx4 = -1000; int mn1 = 1000, mn2 = 1000, mn3 = 1000, mn4 = 1000; for (int i = 0; i < n; i++) { if(arr[i] < mn1){ mn4 = mn3; mn3 = mn2; mn2 = mn1; mn1 = arr[i]; } else if(arr[i] < mn2){ mn4 = mn3; mn3 = mn2; mn2 = arr[i]; } else if(arr[i] < mn3){ mn4 = mn3; mn3 = arr[i]; } else if(arr[i] < mn4){ mn4 = arr[i]; } if(arr[i] > mx1){ mx4 = mx3; mx3 = mx2; mx2 = mx1; mx1 = arr[i]; } else if(arr[i] > mx2){ mx4 = mx3; mx3 = mx2; mx2 = arr[i]; } else if(arr[i] > mx3){ mx4 = mx3; mx3 = arr[i]; } else if(arr[i] > mx4){ mx4 = arr[i]; } } int maxVal = max ((mx1 * mx2 * mx3 * mx4), (mn1 * mn2 * mn3 * mn4)); maxVal = max(maxVal, (mx1 * mx2 * mn1 * mn2)); return maxVal; } int main() { int arr[] = {4, -2, 5, -6, 8}; int n = sizeof(arr) / sizeof(arr[0]); cout<<"Maximum product of quadruple is "<<findMaxProdQuad(arr, n); return 0; }
Output
Maximum product of quadruple is 480
One more approach could be sorting the array. Then the four maximums and four minimums will be the end and start of the array respectively. Then solve as in the above solution by finding the max of three combinations of maximum and minimums.
Program to show the implementation of our approach
Example
#include <bits/stdc++.h> using namespace std; int findMaxProdQuad(int arr[], int n){ sort(arr, arr + n); int maxVal = max((arr[n-1] * arr[n-2] * arr[n-3] * arr[n-4]), (arr[0] * arr[1] * arr[2] * arr[3])); maxVal = max(maxVal, (arr[n-1] * arr[n-2] * arr[0] * arr[1])); return maxVal; } int main(){ int arr[] = {4, -2, 5, -6, 8}; int n = sizeof(arr) / sizeof(arr[0]); cout<<"Maximum product of quadruple is "<<findMaxProdQuad(arr, n); return 0; }
Output
Maximum product of quadruple is 480
- Related Articles
- Maximum size of sub-array that satisfies the given condition in C++
- Find Maximum XOR value of a sub-array of size k in C++
- Maximum size of sub-array that satisfies the given condition in C++ program
- Maximum sum Bi-tonic Sub-sequence in C++
- Maximum product of a triplet (subsequence of size 3) in array in C++
- Maximum product of a triplet (subsequence of size 3) in array in C++ Program.
- Maximum product of subsequence of size k in C++
- Print maximum sum square sub-matrix of given size in C Program.
- Maximum product of an increasing subsequence of size 3 in C++
- Program to find maximum size of any sequence of given array where every pair is nice in Python
- Maximum size rectangle binary sub-matrix with all 1s in C++
- Maximum product subset of an array in C++
- Maximum product of an increasing subsequence of size 3 in C++ program
- Maximum product of 4 adjacent elements in matrix in C++
- Maximum length of a sub-array with ugly numbers in C++
