Minimize Connected Groups by Inserting Interval - Problem

You are given a 2D array intervals, where intervals[i] = [starti, endi] represents the start and the end of interval i. You are also given an integer k.

You must add exactly one new interval [startnew, endnew] to the array such that:

  • The length of the new interval, endnew - startnew, is at most k.
  • After adding, the number of connected groups in intervals is minimized.

A connected group of intervals is a maximal collection of intervals that, when considered together, cover a continuous range from the smallest point to the largest point with no gaps between them.

Examples:

  • A group of intervals [[1, 2], [2, 5], [3, 3]] is connected because together they cover the range from 1 to 5 without any gaps.
  • However, a group of intervals [[1, 2], [3, 4]] is not connected because the segment (2, 3) is not covered.

Return the minimum number of connected groups after adding exactly one new interval to the array.

Input & Output

Example 1 — Bridge Two Groups
$ Input: intervals = [[1,3],[5,7]], k = 2
Output: 1
💡 Note: Original has 2 groups: [1,3] and [5,7]. Gap between them is (3,5) with size 2. We can add [3,5] (length=2≤k) to connect both groups into one.
Example 2 — Cannot Bridge Fully
$ Input: intervals = [[1,2],[6,8]], k = 2
Output: 2
💡 Note: Gap is (2,6) with size 4 > k=2. Best we can do is add [2,4] or [4,6], but this creates 3 total groups. Better to add [1,1] (extends first group) keeping 2 groups.
Example 3 — Already Connected
$ Input: intervals = [[1,5],[3,7]], k = 1
Output: 1
💡 Note: Intervals overlap, so already 1 connected group. Adding any new interval keeps it at 1 group minimum.

Constraints

  • 0 ≤ intervals.length ≤ 104
  • intervals[i].length == 2
  • -109 ≤ starti ≤ endi ≤ 109
  • 0 ≤ k ≤ 109

Visualization

Tap to expand
Minimize Connected Groups by Inserting Interval INPUT 1 2 3 4 5 6 7 [1,3] [5,7] GAP = 2 intervals = [[1,3],[5,7]] k = 2 Initial State: Group 1 Group 2 2 Connected Groups ALGORITHM STEPS 1 Sort Intervals By start time 2 Find All Gaps Gap at [3,5] = 2 3 Check if k Bridges Gap k=2 can cover gap=2 4 Insert Optimal Interval New: [3,5] merges groups Gap Analysis: [1,3] [3,5] [5,7] NEW Sliding window approach FINAL RESULT After Insertion: [1,3]+[3,5]+[5,7] 1 3 5 7 1 Merged Group Output: 1 OK - Optimal Min groups = 1 New interval: [3,5] Length = 2 <= k Key Insight: Sort intervals and use sliding window to find the optimal position for the new interval. A new interval of length k can bridge gaps of size <= k, merging multiple groups into one. Goal: Maximize groups merged = Minimize final group count. Time: O(n log n), Space: O(n) TutorialsPoint - Minimize Connected Groups by Inserting Interval | Optimal Solution
Asked in
Google 35 Microsoft 28 Amazon 22
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen