Minimum Absolute Difference LeetCode Solution

Problem – 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

Minimum Absolute Difference LeetCode Solution in Java

    public List<List<Integer>> minimumAbsDifference(int[] arr) {
        List<List<Integer>> res = new ArrayList();
        //sort elements
        Arrays.sort(arr);
        //init our min difference value
        int min = Integer.MAX_VALUE;
        //start looping over array to find real min element. Each time we found smaller difference
        //we reset resulting list and start building it from scratch. If we found pair with the same
        //difference as min - add it to the resulting list
        for (int i = 0; i < arr.length - 1; i++) {
            int diff = arr[i + 1] - arr[i];
            if (diff < min) {
                min = diff;
                res.clear();
                res.add(Arrays.asList(arr[i], arr[i + 1]));
            } else if (diff == min) {
                res.add(Arrays.asList(arr[i], arr[i + 1]));
            }
        }
        return res;
    }

Minimum Absolute Difference LeetCode Solution in C++

vector<vector<int>> minimumAbsDifference(vector<int>& arr) {
    sort(arr.begin(), arr.end());
    
    vector<vector<int>> result;
    int min_diff = INT_MAX;
    
    // find min difference
    for (int i = 0; i < arr.size() - 1; ++i) {
        min_diff = min(arr[i + 1] - arr[i], min_diff);
    }
    
    // form a list of pairs with min difference, ascending  
    for (int i = 0; i < arr.size() - 1; ++i) {
        if (arr[i + 1] - arr[i] == min_diff)
            result.push_back({arr[i], arr[i + 1]});
    }
    return result;        
}

Minimum Absolute Difference LeetCode Solution in Python

class Solution:
    def minimumAbsDifference(self, A):
        ans, min_diff = [], 10**10
        for i in range(len(A)):
            for j in range(i+1, len(A)):
                a, b = sorted((A[i], A[j]))
                cur_diff = b - a
                if cur_diff == min_diff:
                    ans.append([a, b])
                elif cur_diff < min_diff:
                    min_diff, ans = cur_diff, [[a, b]]
        return sorted(ans)
Minimum Absolute Difference LeetCode Solution Review:

In our experience, we suggest you solve this Minimum Absolute Difference LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Minimum Absolute Difference LeetCode Solution

Find on LeetCode

Conclusion:

I hope this Minimum Absolute Difference LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published.