Check if All the Integers in a Range Are Covered - Problem

You are given a 2D integer array ranges and two integers left and right. Each ranges[i] = [starti, endi] represents an inclusive interval between starti and endi.

Return true if each integer in the inclusive range [left, right] is covered by at least one interval in ranges. Return false otherwise.

An integer x is covered by an interval ranges[i] = [starti, endi] if starti <= x <= endi.

Input & Output

Example 1 — Basic Coverage
$ Input: ranges = [[1,2],[3,4],[5,6]], left = 2, right = 5
Output: true
💡 Note: Every integer 2,3,4,5 is covered: 2 is in [1,2], 3 is in [3,4], 4 is in [3,4], 5 is in [5,6]
Example 2 — Gap in Coverage
$ Input: ranges = [[1,10],[10,20]], left = 1, right = 21
Output: false
💡 Note: Numbers 1-20 are covered by the ranges, but 21 is not covered by any range
Example 3 — Single Number Range
$ Input: ranges = [[1,1],[2,2],[3,3]], left = 1, right = 3
Output: true
💡 Note: Each number 1,2,3 is exactly covered by its corresponding single-number range

Constraints

  • 1 ≤ ranges.length ≤ 50
  • 1 ≤ starti ≤ endi ≤ 50
  • 1 ≤ left ≤ right ≤ 50

Visualization

Tap to expand
Check if All Integers in Range Are Coveredranges = [[1,2],[3,4],[5,6]], left = 2, right = 5[1,2][3,4][5,6]Available RangesTarget Range [2, 5]Check: 2∈[1,2]✓ 3∈[3,4]✓ 4∈[3,4]✓ 5∈[5,6]✓Result: true
Understanding the Visualization
1
Input
Ranges [[1,2],[3,4],[5,6]] and target [2,5]
2
Process
Check if 2,3,4,5 are all covered
3
Output
All numbers covered → true
Key Takeaway
🎯 Key Insight: Check if every number in the target range falls within at least one of the given intervals
Asked in
Amazon 15 Google 12
35.0K Views
Medium Frequency
~15 min Avg. Time
890 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