704. Beautiful Pairs

nums1 and nums2, two 0-indexed integer arrays of the same length, are presented to you.

Any pair of indices (i,j) is considered beautiful if |nums1[i] - nums1[j]| + |nums2[i] - nums2[j]| is the smallest of all possible pairings of indices where i < j.

Return to the beautiful pair. Return the pair that is the lexicographically smallest when there are several beautiful pairs.

Be aware that

  • |x| denotes the absolute value of x.
  • A pair of indices (i1, j1) is lexicographically smaller than (i2, j2) if i1 < i2 or i1 == i2 and j1 < j2.

Example 1:

Input : nums1 = [3, 2, 1, 0, 4], nums2 = [3, 1, 2, 3, 4]

Output : [0, 4]

Explanation :

The above arrays contains multiple pairs with beautiful pairs.

[0, 4] -> |3 - 4| + |3 - 4| -> 2.

[1, 2] -> |2 - 1| + |1 - 2| -> 2.

[2, 3] -> |1 - 0| + |2 - 3| -> 2.

The lexicographically smallest pair is [0, 4].

Example 2:

Input : nums1 = [4, 3, 2, 1, 0], nums2 = [0, 1, 2, 3, 4]

Output : [0, 1]

Explanation :

The above arrays contains multiple pairs with beautiful pairs but the lexicographically smallest pair is.

[0, 1] -> |4 - 3| + |0 - 1| -> 2.

Now Your Turn!

Pick the correct output for the given input

Input : num1 = [5, 1, 0, 0, 3], nums2 = [2, 4, 1, 4, 2]

Still unsure what the problem is asking ?

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

Constraints:

  • 2 <= nums1.length, nums2.length <= 105
  • nums1.length == nums2.length
  • 0 <= nums1i <= nums1.length
  • 0 <= nums2i <= nums2.length

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<int> beautifulPair(vector<int>& arr1, vector<int>& arr2) {
//your code goes here
}
};
Test Case

Input:

Nums1
Nums2