# Maximum Product of Three Numbers in C++

C++Server Side ProgrammingProgramming

Suppose we have an integer array; we have to find three numbers whose product is maximum then return the maximum product.

So, if the input is like [1,1,2,3,3], then the output will be 18, as the three elements are [2,3,3].

To solve this, we will follow these steps −

• sort the array nums

• l := size of nums

• a := nums[l - 1], b := nums[l - 2], c := nums[l - 3], d := nums, e := nums

• return maximum of a * b * c and d * e * a

## Example

Let us see the following implementation to get better understanding −

Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
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, e = nums;
return max(a * b * c, d * e * a);
}
};
main(){
Solution ob;
vector<int> v = {1,1,2,3,3};
cout << (ob.maximumProduct(v));
}

## Input

{1,1,2,3,3}

## Output

18