1913. Maximum Product Difference Between Two Pairs LeetCode Solution

In this guide, you will get 1913. Maximum Product Difference Between Two Pairs LeetCode Solution with the best time and space complexity. The solution to Maximum Product Difference Between Two Pairs problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Maximum Product Difference Between Two Pairs solution in C++
  4. Maximum Product Difference Between Two Pairs solution in Java
  5. Maximum Product Difference Between Two Pairs solution in Python
  6. Additional Resources
1913. Maximum Product Difference Between Two Pairs LeetCode Solution image

Problem Statement of Maximum Product Difference Between Two Pairs

The product difference between two pairs (a, b) and (c, d) is defined as (a * b) – (c * d).

For example, the product difference between (5, 6) and (2, 7) is (5 * 6) – (2 * 7) = 16.

Given an integer array nums, choose four distinct indices w, x, y, and z such that the product difference between pairs (nums[w], nums[x]) and (nums[y], nums[z]) is maximized.
Return the maximum such product difference.

Example 1:

Input: nums = [5,6,2,7,4]
Output: 34
Explanation: We can choose indices 1 and 3 for the first pair (6, 7) and indices 2 and 4 for the second pair (2, 4).
The product difference is (6 * 7) – (2 * 4) = 34.

Example 2:

Input: nums = [4,2,5,9,7,4,8]
Output: 64
Explanation: We can choose indices 3 and 6 for the first pair (9, 8) and indices 1 and 5 for the second pair (2, 4).
The product difference is (9 * 8) – (2 * 4) = 64.

See also  1510. Stone Game IV LeetCode Solution

Constraints:

4 <= nums.length <= 104
1 <= nums[i] <= 104

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1913. Maximum Product Difference Between Two Pairs LeetCode Solution in C++

class Solution {
 public:
  int maxProductDifference(vector<int>& nums) {
    int max1 = INT_MIN;
    int max2 = INT_MIN;
    int min1 = INT_MAX;
    int min2 = INT_MAX;

    for (const int num : nums) {
      if (num > max1) {
        max2 = max1;
        max1 = num;
      } else if (num > max2) {
        max2 = num;
      }
      if (num < min1) {
        min2 = min1;
        min1 = num;
      } else if (num < min2) {
        min2 = num;
      }
    }

    return max1 * max2 - min1 * min2;
  }
};
/* code provided by PROGIEZ */

1913. Maximum Product Difference Between Two Pairs LeetCode Solution in Java

class Solution {
  public int maxProductDifference(int[] nums) {
    int max1 = Integer.MIN_VALUE;
    int max2 = Integer.MIN_VALUE;
    int min1 = Integer.MAX_VALUE;
    int min2 = Integer.MAX_VALUE;

    for (final int num : nums) {
      if (num > max1) {
        max2 = max1;
        max1 = num;
      } else if (num > max2) {
        max2 = num;
      }
      if (num < min1) {
        min2 = min1;
        min1 = num;
      } else if (num < min2) {
        min2 = num;
      }
    }

    return max1 * max2 - min1 * min2;
  }
}
// code provided by PROGIEZ

1913. Maximum Product Difference Between Two Pairs LeetCode Solution in Python

class Solution:
  def maxProductDifference(self, nums: list[int]) -> int:
    max1 = -math.inf
    max2 = -math.inf
    min1 = math.inf
    min2 = math.inf

    for num in nums:
      if num > max1:
        max2 = max1
        max1 = num
      elif num > max2:
        max2 = num
      if num < min1:
        min2 = min1
        min1 = num
      elif num < min2:
        min2 = num

    return max1 * max2 - min1 * min2
# code by PROGIEZ

Additional Resources

See also  2512. Reward Top K Students LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.