InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Difference of Candies

Problem Description

Given N bags containing candies. Each bag has distinct number of candies.

Find the pairs of bag that have the smallest absolute difference of candies in them.

NOTE:

  • Output pair (a, b) such that a <= b.
  • If multiple pairs have minimum absolute difference, output all of them in ascending order.



Problem Constraints

2 <= N <= 100000

1 <= Number of candies in a bag <= 109



Input Format

First and only argument is an integer array A.



Output Format

Return an 2-d array containing the pairs having minimum absolute difference.



Example Input

Input 1:

 A = [1, 2, 10, 3]

Input 2:

 A = [5, 10, 15]



Example Output

Output 1:

 [
    [1, 2]
    [2, 3]
 ]

Output 2:

 [
    [5, 10]
    [10, 15]
 ]



Example Explanation

Explanation 1:

 Minimum difference possible between 2 elements is 1.
 Pairs having difference 1 are: {(1, 2), (2, 3)}.
 

Explanation 2:

 2 pairs exists with minimum difference 5: {(5, 10), (10, 15)}.



NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Difference of Candies on Interview Code Editor
Sign Up
to access hints and editorial solutions for Difference of Candies

Discussion


Loading...
Click here to start solving coding interview questions