Mid/SeniorArraySorting

Count Ways to Group Overlapping Ranges

You are given a 2D integer array ranges where ranges[i] = [starti, endi] denotes that all integers between starti and endi (both inclusive) are contained in the i^th range.

You are to split ranges into two possibly empty groups such that:

  • Each range belongs to exactly one group.
  • Any two overlapping ranges must belong to the same group.

Two ranges are said to be overlapping if there exists at least one integer that is present in both ranges.

Return the total number of ways to split ranges into two groups. Since the answer may be very large, return it modulo 10^9 + 7.

Example 1
Inputranges = [[6,10],[5,15]]
Output2
The two ranges overlap, so they must stay together, and that combined component can be placed in either group.
Example 2
Inputranges = [[1,3],[10,20],[2,5],[4,8]]
Output4
The ranges [1,3], [2,5], and [4,8] form one overlapping component, while [10,20] is separate, giving four group assignments.

Constraints

  • 1 <= ranges.length <= 10^5
  • ranges[i].length == 2
  • 0 <= starti <= endi <= 10^9

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate