Skip to content

Instantly share code, notes, and snippets.

@ShivamGoyal1899
Created February 4, 2020 20:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ShivamGoyal1899/f05aeb3eb1f80cc33d42225391df1cd1 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Solution
{
public:
long MaxProduct(vector<int> &nums)
{
long first = 0, second = 0;
for (int i = 0; i < nums.size(); i++)
{
if (nums[i] > first)
{
second = first;
first = nums[i];
}
else if (nums[i] > second)
{
second = nums[i];
}
}
return first*second;
}
};
int main() {
vector<int> vec;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
vec.push_back(x);
}
Solution s;
cout << s.MaxProduct(vec) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment