Back to July Back to Year 2026
EASY 27 Jul 2026 View on LeetCode

1464. Maximum Product of Two Elements in an Array

</> Solution

/**
 * 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);
    }
}

TIME COMPLEXITY

O(N)

SPACE COMPLEXITY

O(1)

TOPICS

Array Heap (Priority Queue) Sorting