Sobes.tech
Junior — Senior

Merging intersecting segments

livecode

Task condition

Given a two-dimensional array where each element is a segment represented as a pair of numbers [start, end]. It is necessary to merge all segments that intersect or touch each other, and return the resulting array of non-overlapping segments.

// Input:  [][]int{{2,6}, {1,3}, {8,10}, {15,18}}
// Output: [][]int{{1,6}, {8,10}, {15,18}}