
- 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
Maximum Product of Three Numbers in C++
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[0], e := nums[1]
return maximum of a * b * c and d * e * a
Example
Let us see the following implementation to get better understanding −
#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[0], e = nums[1]; 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
- Related Articles
- Find the greatest product of three numbers in JavaScript
- Java program to find maximum of three numbers
- Maximum Product of Two Numbers in a List of Integers in JavaScript
- Python program to find the maximum of three numbers
- C# program to find the maximum of three numbers
- The sum of three numbers in A.P. is $3$ and their product is $- 35$. Find the numbers.
- Three numbers are in A.P. If the sum of these numbers be 27 and the product 648, find the numbers.
- Find maximum product of digits among numbers less than or equal to N in C++
- Maximum Product of Word Lengths in C++
- Python program maximum of three.
- Maximum Product Subarray in Python
- Maximum Product Subarray | Added negative product case in C++
- Maximum product of an increasing subsequence in C++
- Maximum product subset of an array in C++
- Maximum length product of unique words in JavaScript

Advertisements