Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sorting is useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other

Sorting is useful as the first step in many different tasks. The most common task is to make finding
things easier, but there are other uses as well. In this case, it will make it easier to determine which
pair or pairs of elements have the smallest absolute difference between them.
Example
arr=[5,2,3,4,1]
Sorted, arr ?'=[1,2,3,4,5]. Several pairs have the minimum difference of 1 :
(1,2),(2,3),(3,4),(4,5). Return the array 1,2,2,3,3,4,4,5.
Note
As shown in the example, pairs may overlap.
Given a list of unsorted integers, arr , find the pair of elements that have the smallest absolute
difference between them. If there are multiple pairs, find them all.
Function Description
Complete the closestNumbers function in the editor below.
closestNumbers has the following parameter(s):
int arr[n]: an array of integers
Returns
int[]: an array of integers as described
Input Format
The first line contains a single integer n, the length of arr.
The second line contains n space-separated integers, arr[i].
Constraints
2n200000
-107arr[i]107
All a[i] are unique in arr.
Output Format
Explanation 0
(30)-(-20)=50, which is the smallest difference.
Explanation 1
(-470)-(-520)=30-(-20)=50, which is the smallest difference.
Explanation 2
Here, the minimum difference is 1. Valid pairs are (2,3),(3,4), and (4,5).
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Administration The Essential Reference

Authors: Brian Laskey, David Kreines

1st Edition

1565925165, 978-1565925168

More Books

Students also viewed these Databases questions

Question

friendliness and sincerity;

Answered: 1 week ago