Leetcode Problem 1913. Maximum Product Difference Between Two Pairs

1913. Maximum Product Difference Between Two Pairs

Leetcode Solutions

Track the Two Biggest and the Two Smallest Elements

  1. Initialize biggest and secondBiggest to the minimum possible integer value.
  2. Initialize smallest and secondSmallest to the maximum possible integer value.
  3. Iterate over each element num in nums.
    • If num is greater than biggest, update secondBiggest to biggest and biggest to num.
    • Else if num is greater than secondBiggest, update secondBiggest to num.
    • If num is less than smallest, update secondSmallest to smallest and smallest to num.
    • Else if num is less than secondSmallest, update secondSmallest to num.
  4. Return the product of biggest and secondBiggest minus the product of smallest and secondSmallest.
UML Thumbnail

Sort and Select Elements

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...