/**
* Problem: LeetCode 1464 - Maximum Product of Two Elements in an Array
* Difficulty: Easy
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int maxProduct(int[] nums) {
int max1 = 0;
int max2 = 0;
for (int num : nums) {
if (num >= max1) {
max2 = max1;
max1 = num;
} else if (num > max2) {
max2 = num;
}
}
return (max1 - 1) * (max2 - 1);
}
}
"""
Problem: LeetCode 1464 - Maximum Product of Two Elements in an Array
Difficulty: Easy
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def maxProduct(self, nums: List[int]) -> int:
max1 = 0
max2 = 0
for num in nums:
if num >= max1:
max2 = max1
max1 = num
elif num > max2:
max2 = num
return (max1 - 1) * (max2 - 1)
/**
* Problem: LeetCode 1464 - Maximum Product of Two Elements in an Array
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
using namespace std;
/**
* Problem: LeetCode 1464 - Maximum Product of Two Elements in an Array
* Difficulty: Easy
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
int maxProduct(vector<int>& nums) {
int max1 = 0;
int max2 = 0;
for (int num : nums) {
if (num >= max1) {
max2 = max1;
max1 = num;
} else if (num > max2) {
max2 = num;
}
}
return (max1 - 1) * (max2 - 1);
}
};
/**
* Problem: LeetCode 1464 - Maximum Product of Two Elements in an Array
* Difficulty: Easy
* Language: JavaScript
* Author: CodeByTushu
*/
var maxProduct = function(nums) {
let max1 = 0;
let max2 = 0;
for (const num of nums) {
if (num >= max1) {
max2 = max1;
max1 = num;
} else if (num > max2) {
max2 = num;
}
}
return (max1 - 1) * (max2 - 1);
};