Flowers Beauty

You are given an integer array nums, where nums[i] represents the beauty value of the ith flower in a garden.

You need to choose a pair of flowers such that the absolute difference in their beauty values is as large as possible.

Your task is to calculate and return an array of two integers where:

  • The first element is the maximum possible beauty difference between any two flowers.
  • The second element is the total number of distinct pairs of flowers that yield this exact maximum difference.

Example 1:

Input: nums = [1, 2, 3, 1, 4]

Output: [3, 2 ]

Explanation:

The maximum difference is between 4 and 1 (4 - 1 = 3). There are two '1's and one '4'. We can pair the '4' with the first '1', or the '4' with the second '1'. Total valid pairs = 2.

Example 2:

Input: nums = [1, 4, 1, 4]

Output: [3, 4]

Explanation:

The maximum difference is 3. There are two '1's and two '4's. Every '1' can be paired with every '4'. Total valid pairs = 2 * 2 = 4.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [5, 5, 5, 5, 5]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 2 <= nums.length <= 2 x 105
  • 1 <= nums[i] <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
vector<long long> maxBeautyDifference(vector<int>& nums) {
}
};
Test Case

Input:

Nums