Maximum Product Difference Between Two Pairs LeetCode solution in C++
1class Solution {2public:3int maxProductDifference(vector<int> &nums) {4// we have to return the result of5// (firstMax*secondMax) - (firstMin*secondMin)6int max1 = INT_MIN;7int max2 = INT_MIN;8int min1 = INT_MAX;9int min2 = INT_MAX;10for (int i = 0; i < nums.size(); i++) {11if (nums[i] > max1) {12// assign the second max to max213max2 = max1;14max1 = nums[i];15} else if (nums[i] > max2) {16// it can become second max17max2 = nums[i];18}1920// check for the minimum21if (nums[i] < min1) {22// it can become first minimum23min2 = min1;24min1 = nums[i];25} else if (nums[i] < min2) {26// it can become second minimum27min2 = nums[i];28}29}30return (max1 * max2) - (min1 * min2);31}32};