Problem Name: 1913. Maximum Product Difference Between Two Pairs
Problem Link: https://leetcode.com/problems/maximum-product-difference-between-two-pairs/
Difficulty: Easy
Tag: Array | Sorting
Language: C# | C++
OJ: LeetCode
Algorithm:
1.Sorting:
The Array.Sort(nums) method is used to sort the array nums in ascending order.
2.Calculating Maximum Product Difference:
The maximum product difference is calculated by taking the product of the two largest numbers (nums[nums.Length - 1] * nums[nums.Length - 2]) and subtracting the product of the two smallest numbers (nums[0] * nums[1]) & returned as the output of the MaxProductDifference method.
Code(C#)
public class Solution {
public int MaxProductDifference(int[] nums) {
Array.Sort(nums);
return (nums[nums.Length-1]*nums[nums.Length-2])-(nums[0]*nums[1]);
}
}
Code(C++)
Upcoming