
- 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
Find a pair with maximum product in array of Integers in C++
Consider we have an array A, there are n different elements. We have to find a pair (x, y) from the array A, such that the product of x and y is maximum. The array may contain positive or negative elements. Suppose an array is like: A = [-1, -4, -3, 0, 2, -5], then the pair will be (-4, -5) as product is maximum.
To solve this problem, we have to keep track four numbers, the positive_max, positive_second_max, negative_max, negative_second_max. At the end if the (positive_max * positive_second_max) is greater than (negative_max * negative_second_max), then return positive pairs, otherwise return negative pairs.
Example
#include<iostream> #include<cmath> using namespace std; void maxProdPair(int arr[], int n) { if (n < 2) { cout << "No pair is present"; return; } if (n == 2) { cout << "(" << arr[0] << ", " << arr[1] << ")" << endl; return; } int pos_max = INT_MIN, pos_second_max = INT_MIN; int neg_max = INT_MIN, neg_second_max = INT_MIN; for (int i = 0; i < n; i++) { if (arr[i] > pos_max) { pos_second_max = pos_max; pos_max = arr[i]; } else if (arr[i] > pos_second_max) pos_second_max = arr[i]; if (arr[i] < 0 && abs(arr[i]) > abs(neg_max)) { neg_second_max = neg_max; neg_max = arr[i]; } else if(arr[i] < 0 && abs(arr[i]) > abs(neg_second_max)) neg_second_max = arr[i]; } if (neg_max*neg_second_max > pos_max*pos_second_max) cout << "(" << neg_max << ", " << neg_second_max << ")" << endl; else cout << "(" << pos_max << ", " << pos_second_max << ")" << endl; } int main() { int arr[] = {-1, -4, -3, 0, 2, -5}; int n = sizeof(arr)/sizeof(arr[0]); maxProdPair(arr, n); }
Output
(-5, -4)
- Related Articles
- Queries to find maximum product pair in range with updates in C++
- Maximum GCD of N integers with given product in C++
- Find pair with maximum GCD in an array in C++
- Find a pair from the given array with maximum nCr value in Python
- Find a pair from the given array with maximum nCr value in C++
- Maximum Product of Two Numbers in a List of Integers in JavaScript
- Given an array of integers, find the pair of adjacent elements that has the largest product and return that product JavaScript
- Write a program in C++ to find the maximum and second maximum in a given unsorted array of integers
- Find pair with maximum difference in any column of a Matrix in C++
- Find Two Array Elements Having Maximum Product in Java?
- Write a pair of integers whose product is -12 and there lies seven integers between them.
- Find integers that divides maximum number of elements of the array in C++
- Find the Pair with a Maximum Sum in a Matrix using C++
- Maximum sum of pairwise product in an array with negative allowed in C++
- Maximum product subset of an array in C++

Advertisements