Posts

Search

Highest Possible Product

Given an array of N integers, your task is to print the highest possible product by multiplying any three numbers from the array.

Input Format

First line contains an integer N.
Next line contains N space separated integers.

Constraints

1 <= N <= 106
0 <= |Ai| <= 103

Output Format

Output one numbers, the maximum product.

Sample Input 0

6
0 -1 3 100 70 50

Sample Output 0

350000

Solution :

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int findTriplet(vector<int> A)
{
    sort(A.begin(), A.end());
    int n = A.size();
    if (n <= 2) {
        printf("No triplet exists since the vector has less than 3 elements");
    }
    if (A[n-1] * A[n-2] * A[n-3] > A[0] * A[1] * A[n-1]) {
        cout<<A[n-1]*A[n-2]*A[n-3];
    }
    else {
        cout<<A[0]*A[1]*A[n-1];
    }
    return -1;
}
int main()
{
    int a;
    cin>>a;
    int input;
    
    vector<int> A ;
    while (a>0){
        cin >> input;
        A.push_back(input);
        a--;
    }
    findTriplet(A);

    return 0;
}