534. Find Maximal Uncovered Ranges

You are provided a 0-indexed 2D-array ranges, which is a list of sub-ranges of nums (sub-ranges may overlap), and an integer n, which is the length of a 0-indexed array nums.

There are precisely two cells in each row ranges[i]:

  • ranges[i][0], which displays the ith range's beginning (inclusive)
  • ranges[i][1], which displays the ith range's end (inclusive).

Some nums cells are covered by these ranges, while other cells remain uncovered. Finding every undiscovered range with the maximum length is your responsibility.

Provide a 2D-array response with the uncovered ranges arranged in ascending order by starting point.

Example 1:

Input : n = 10, ranges = [ [2, 3], [5, 5], [7, 9] ]

Output : [ [0, 1], [4, 4], [6, 6] ]

Explanation :

The ranges covered are [2, 3], [5, 5], [7, 9]. If converted to binary array then it becomes [0, 0, 1, 1, 0, 1, 0, 1, 1, 1] where 1 represents the covered cells.

In the binary array we can observe the ranges [0, 1], [4, 4], [6, 6] are uncovered.

Example 2:

Input : n = 10, ranges = [ [0, 3], [7, 9] ]

Output : [ [4, 6] ]

Explanation :

The ranges covered are [0, 3], [7, 9]. If converted to binary array then it becomes [1, 1, 1, 0, 0, 0, 0, 1, 1, 1] where 1 represents the covered cells.

In the binary array we can observe the ranges [4, 6] is uncovered.

Now Your Turn!

Pick the correct output for the given input

Input : n = 12, ranges = [[2, 5], [9, 10], [4, 4], [3, 7]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 109
  • 0 <= ranges.length <= 106
  • ranges[i].length = 2
  • 0 <= ranges[i][j] <= n - 1
  • ranges[i][0] <= ranges[i][1]

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
vector<vector<int>> findMaximalUncoveredRanges(int n, vector<vector<int>>& ranges) {
//your code goes here
}
};
Test Case

Input:

N
Ranges