# Minimal rely of subintervals to cowl a given time length

Given an array of intervals[], the duty is to search out the minimal rely of subintervals wanted to cowl a given time length. Every interval represents a phase of time that begins at a particular time limit (begin time) and ends at one other particular time limit (finish time). The intervals could overlap and have various lengths. If it’s not possible to cowl the whole length, return -1.

Examples:

Enter: Intervals[] = [[0, 1], [6, 8], [0, 2], [5, 6], [0, 4], [0, 3], [6, 7], [1, 3], [4, 7], [1, 4], [2, 5], [2, 6], [3, 4], [4, 5], [5, 7], [6, 9]], timeDuration = 9
Output: 3
Clarification: We are able to take subintervals [0, 4], [4, 7], and [6, 9]. There are additionally many subintervals however that is the utmost amongst them.

Enter: Intervals[] = [[0, 2], [4, 6], [8, 10], [1, 9], [1, 5], [5, 9]], timeDuration = 10
Output: 3
Clarification: We take the subintervals [0, 2], [8, 10], [1, 9]; a complete of three subintervals.

Method: This may be solved with the next concept:

Utilizing Dynamic Programming, and sorting the intervals on the idea of the beginning time, we will get what number of intervals it can cowl if began from a selected time as much as time length.

Beneath are the steps concerned within the implementation of the code:

• First, we kind the given intervals in non-decreasing order of their begin occasions.
• We create a 1-D DP array dp with measurement time+1, the place time is the given time length.
• We initialize all components of dp to INT_MAX – 1, besides dp = 0, since we will cowl 0-time length with 0 intervals.
• If the present time length falls in any interval, we replace dp[i] as min(dp[i], dp[interval] + 1), the place interval is the beginning time of the interval and dp[interval] + 1 means we’re including the present interval to the variety of subintervals wanted to cowl time length i.
• Lastly, we return dp[time] if dp[time] < INT_MAX – 1, since dp[time] will retailer the minimal variety of subintervals wanted to cowl the given time length. In any other case, we return -1, indicating that it isn’t potential to cowl the given time length utilizing the given intervals.

Beneath is the implementation of the above concept:

## C++

 `#embody ` `utilizing` `namespace` `std;` ` `  `int` `CountSubintervals(vector >& Intervals,` `                      ``int` `time``)` `{` ` `  `    ` `    ` `    ``kind(Intervals.start(), Intervals.finish());` ` `  `    ` `    ``vector<``int``> dp(``time` `+ 1, INT_MAX - 1);` ` `  `    ``dp = 0;` `    ``for` `(``int` `i = 1; i <= ``time``; i++) {` `        ``for` `(``auto``& interval : Intervals) {` ` `  `            ` `            ` `            ` `            ` `            ` `            ` `            ` `            ``if` `(interval <= i && i <= interval) {` `                ``dp[i] = min(dp[i], dp[interval] + 1);` `            ``}` `        ``}` `    ``}` ` `  `    ` `    ` `    ` `    ``return` `dp[``time``] == INT_MAX - 1 ? -1 : dp[``time``];` `}` ` `  `int` `primary()` `{` `    ``vector > Intervals` `        ``= { { 0, 1 }, { 6, 8 }, { 0, 2 }, { 5, 6 }, { 0, 4 }, { 0, 3 }, { 6, 7 }, { 1, 3 }, { 4, 7 }, { 1, 4 }, { 2, 5 }, { 2, 6 }, { 3, 4 }, { 4, 5 }, { 5, 7 }, { 6, 9 } };` `    ``int` `timeDuration = 9;` ` `  `    ` `    ``int` `end result = CountSubintervals(Intervals, timeDuration);` `    ``cout << end result << endl;` `    ``return` `0;` `}`

Time Complexity: O(T*N)
Auxiliary Area: O(T)