Algorithm


Problem Name: 1200. Minimum Absolute Difference

Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.

Return a list of pairs in ascending order(with respect to pairs), each pair [a, b] follows

  • a, b are from arr
  • a < b
  • b - a equals to the minimum absolute difference of any two elements in arr

 

Example 1:

Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.

Example 2:

Input: arr = [1,3,6,10,15]
Output: [[1,3]]

Example 3:

Input: arr = [3,8,-10,23,19,-4,-14,27]
Output: [[-14,-10],[19,23],[23,27]]

 

Constraints:

  • 2 <= arr.length <= 105
  • -106 <= arr[i] <= 106

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


class Solution {
  public List();
    for (int i = 0; i  <  arr.length - 1; i++) {
      if (arr[i + 1] - arr[i] == minDiff) {
        result.add(Arrays.asList(arr[i], arr[i + 1]));        
      }
    }
    return result;
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [4,2,1,3]

Output

x
+
cmd
[[1,2],[2,3],[3,4]]

#2 Code Example with Javascript Programming

Code - Javascript Programming


const minimumAbsDifference = function(arr) {
  arr.sort((a, b) => a - b)
  let min = Number.MAX_VALUE
  for(let i = 1, len = arr.length; i  <  len; i++) {
    if(arr[i] - arr[i - 1] < min) min = arr[i] - arr[i - 1]
  }
  const res = []
  for(let i = 1, len = arr.length; i  <  len; i++) {
    if(arr[i] - arr[i - 1] === min) res.push([arr[i - 1], arr[i]]>
  }
  return res
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [4,2,1,3]

Output

x
+
cmd
[[1,2],[2,3],[3,4]]

#3 Code Example with Python Programming

Code - Python Programming


class Solution:
    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
        arr.sort()
        mn = min(b - a for a, b in zip(arr, arr[1:]))
        return [[a, b] for a, b in zip(arr, arr[1:]) if b - a == mn]        
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [1,3,6,10,15]

Output

x
+
cmd
[[1,3]]

#4 Code Example with C# Programming

Code - C# Programming


using System;
using System.Collections.Generic;

namespace LeetCode
{
    public class _1200_MinimumAbsoluteDifference
    {
        public IList < IList<int>> MinimumAbsDifference(int[] arr)
        {
            Array.Sort(arr);

            var minDiff = int.MaxValue;
            for (int i = 0; i  <  arr.Length - 1; i++)
                minDiff = Math.Min(minDiff, Math.Abs(arr[i + 1] - arr[i]));

            var result = new List < IList<int>>();
            for (int i = 0; i  <  arr.Length - 1; i++)
            {
                if (Math.Abs(arr[i + 1] - arr[i]) == minDiff)
                    result.Add(new List<int> { arr[i], arr[i + 1] });
            }

            return result;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
arr = [1,3,6,10,15]

Output

x
+
cmd
[[1,3]]
Advertisements

Demonstration


Previous
#1192 Leetcode Critical Connections in a Network Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1201 Leetcode Ugly Number III Solution in C, C++, Java, JavaScript, Python, C# Leetcode