How to find the minimum product among all combinations of triplets in a vector with C++

1 Answer

0 votes
#include <iostream>
#include <algorithm>
#include <vector>
#include <climits>

int getMinimumTripletProduct(std::vector<int> v) {
    sort(v.begin(), v.end());
    
    int size = v.size();
 
    if (size <= 2) {
        return INT_MAX;
    }
 
    return std::min(v[size - 1] * v[size - 2] * v[0], v[0] * v[1] * v[2]);
}
 
int main()
{
    std::vector<int> v = { 3, 5, 8, 17, 4, 9, 7, 39, 2 };
 
    int min = getMinimumTripletProduct(v);
 
    if (min == INT_MAX) {
        std::cout << "Vector has less than 3 elements";
    }
    else {
        std::cout << "The minimum product = " << min;
    }
}




/*
run:

The minimum product = 24

*/

 



answered Aug 19, 2022 by avibootz
edited Aug 19, 2022 by avibootz
...