Solve a coding problem involving array manipulation or interval merging, such as LeetCode 56 (Merge Intervals). Given a list of integer intervals, return a new list after sorting and merging any intervals that overlap or have a gap of at most 2 units. Intervals separated by a larger gap remain distinct. Implement merge_intervals(intervals), where the input and output are lists of two-element lists.
def merge_intervals(intervals):