Created
February 4, 2020 20:50
-
-
Save shyvum/f05aeb3eb1f80cc33d42225391df1cd1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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