Find Pair With Smallest Difference Problem Statement

Given two unsorted arrays of non-negative integers, arr1 and arr2 with sizes N and M, determine the pair of elements (one from each array) which have the smallest absolute (non-negative) difference, and return that difference.

Example:

Input:
N = 3, arr1 = [10, 20, 30]
M = 2, arr2 = [17, 15]
Output:
3
Explanation:

The pair (20, 17) has the smallest absolute difference of 3.

Constraints:

  • 1 <= T <= 10
  • 1 <= N, M <= 1000
  • 0 <= arr1[i], arr2[i] <= 10^6
  • Time Limit: 1 second
Note:

Both arrays are unsorted, and all elements are non-negative integers.

Vaishu Sumthankar
2y

Sorting the arrays Sort arrays 'arr1' and 'arr2' in ascending order. Let ‘i’ be the pointer to the first element of 'arr1', ‘j’ be the pointer to the first element of 'arr2', and ‘minDiff’ store the ...read more

Help your peers!
Add answer anonymously...
Amazon Program Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter