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.
Find the pair of elements with the smallest absolute difference from two unsorted arrays.
Sort both arrays to simplify finding the pair with the smallest difference.
Use two pointers approach to iterate...read more
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
Top Amazon Program Analyst interview questions & answers
Popular interview questions of Program Analyst
Reviews
Interviews
Salaries
Users/Month