# 18.12.2023 [1913. Maximum Product Difference Between Two Pairs]
max * second_max - min * second_min
18.12.2023
1913. Maximum Product Difference Between Two Pairs easy
blog post
youtube
Join me on Telegram
https://t.me/leetcode_daily_unstoppable/443
Problem TLDR
max * second_max - min * second_min
Intuition
We can sort an array, or just find max and second max in a linear way.
Complexity
Time complexity:
O(n)Space complexity:
O(1)
Code
fun maxProductDifference(nums: IntArray): Int {
var (a, b, c, d) = listOf(0, 0, Int.MAX_VALUE, Int.MAX_VALUE)
for (x in nums) {
if (x > a) b = a.also { a = x } else if (x > b) b = x
if (x < d) c = d.also { d = x } else if (x < c) c = x
}
return a * b - c * d
}