Dear readers today read in this article how to maximize the product of the number three in the C ++ program?
If you have an integer array; We will find three numbers whose product maximum and then return the maximum product.
Therefore, if the input is like [1,1,2,3,3], the output will be 18, because the three elements are [2,3,3].
Example for Maximum Product of Three Numbers in C++
#include <bits/stdc++.h> using namespace std; class MaxProdNumProg{ public: int maximumProduct(vector<int>& nums) { sort(nums.begin(), nums.end()); int l = nums.size(); int a = nums[l - 1], b = nums[l - 2], c = nums[l - 3], d = nums[0], e = nums[1]; return max(a * b * c, d * e * a); } }; main(){ namespace ob; vector<int> v = {1,1,2,3,3}; cout << (ob.maximumProduct(v)); }
Output
18