Maximum Product of Two Elements in an Array LeetCode solution in Java
1class Solution {2public int maxProduct(int[] nums) {3int max = Integer.MIN_VALUE;4int maxi = -1;5for (int i = 0; i < nums.length; ++i) {6if (nums[i] > max) {7max = nums[i];8maxi = i;9}10}11nums[maxi] = Integer.MIN_VALUE;12int nextmax = Integer.MIN_VALUE;13for (int i = 0; i < nums.length; ++i) {14if (nums[i] > nextmax) nextmax = nums[i];15}16return max * nextmax - max - nextmax + 1;17}18}