- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Number of Maximum Product Quadruples in C++
Suppose we have one integer array with n elements. We have to find the maximum product of a quadruple in the array. So if the array is like [3, 5, 20, 6, 10], then final product is 6000, and elements in quadruple is 10, 5, 6, 20
To solve this, we will follow these steps −
- Sort the array in ascending order
- Suppose x be the product of last four elements, y be the product of first four elements, and z be the product of first two and second two elements
- Return the max of x, y and z.
Example
#include<iostream> #include<algorithm> using namespace std; int maxQuadProduct(int arr[], int n) { if (n < 4) return -1; sort(arr, arr + n); int last_four = arr[n - 1] * arr[n - 2] * arr[n - 3] * arr[n - 4]; int first_four = arr[0] * arr[1] * arr[2] * arr[3]; int two_first_last = arr[0] * arr[1] * arr[n - 1] * arr[n - 2]; return max(last_four, max(first_four, two_first_last)); } int main() { int arr[] = { -10, -3, 5, 6, -20 }; int n = sizeof(arr) / sizeof(arr[0]); int maximum_val = maxQuadProduct(arr, n); if (maximum_val == -1) cout << "No Quadruple Exists"; else cout << "Maximum product is " << maximum_val; }
Output
Maximum product is 6000
- Related Articles
- Program to find number of quadruples for which product of first and last pairs are same in Python
- Maximum number with same digit factorial product in C++
- Maximum number of trailing zeros in the product of the subsets of size k in C++
- Maximum sum and product of the M consecutive digits in a number in C++
- Find maximum level product in Binary Tree in C++
- Find a pair with maximum product in array of Integers in C++
- Maximum Product of Word Lengths in C++
- Maximum Product of Three Numbers in C++
- Find the Number of quadruples where the first three terms are in AP and last three terms are in GP using C++
- Maximum product of an increasing subsequence in C++
- Maximum product subset of an array in C++
- Maximum Product Subarray | Added negative product case in C++
- Maximum product of subsequence of size k in C++
- Find the row with maximum number of 1s using C++
- Queries to find maximum product pair in range with updates in C++

Advertisements