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.

AnswerBot
4mo

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

Vaishu Sumthankar
3y

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!
Select
Add answer anonymously...

Amazon Program Analyst interview questions & answers

A Program Analyst was asked Q. Longest Common Prefix Problem Statement You are given an array ‘ARR’ consisting ...read more
A Program Analyst was asked Q. Longest Common Subsequence Problem Statement Given two strings STR1 and STR2, de...read more
A Program Analyst was asked Q. Closest Sum Problem Statement Given an array of integers ARR of size N and an in...read more

Popular interview questions of Program Analyst

A Program Analyst was asked Q1. Longest Common Prefix Problem Statement You are given an array ‘ARR’ consisting ...read more
A Program Analyst was asked Q2. Longest Common Subsequence Problem Statement Given two strings STR1 and STR2, de...read more
A Program Analyst was asked Q3. Closest Sum Problem Statement Given an array of integers ARR of size N and an in...read more
Amazon Program Analyst Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits