Leetcode 1306 Solution

This article provides solution to leetcode question 1306 (minimum-absolute-difference)

https://leetcode.com/problems/minimum-absolute-difference

Solution

class Solution:
    def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
        arr.sort()

        ans = []
        curr_dist = sys.maxsize

        for i in range(len(arr) - 1):
            dist = arr[i + 1] - arr[i]

            if dist == curr_dist:
                ans.append((arr[i], arr[i + 1]))
            elif dist < curr_dist:
                curr_dist = dist
                ans = [(arr[i], arr[i + 1])]

        return ans