New update is available. Click here to update.

Last Updated: 11 Jan, 2021

Difficulty: Moderate

```
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next 2*T represents the ‘T’ test cases.
The first line of each test case contains an integer ‘n’ denoting the number of intervals.
The next ‘n’ lines of each test case contain two space-separated non-negative integers denoting the starting and end of an interval respectively.
```

```
For each test case, return the size of the maximal set of mutually disjoint sets.
```

```
You do not need to print anything. It has already been taken care of. Just implement the given function.
```

```
1 <= T <= 50
1 <= n <= 10^4
1 <= a < b <= 10^9
Where ‘T’ is the total number of test cases, ‘n’ is the number of intervals and ‘a’, ‘b’ are the starting and ending of an interval.
Time Limit: 1 sec
```

- Sort the given interval set by their starting points.
- Traverse the sorted set from the start and for each interval calculate the maximal number of disjoint intervals ending before it.
- The recurrence relation is dp[i] = 1 + max(dp[j]) (0 <= j < i and ending(j)<starting(i))
- The answer is max(dp[0], dp[1], dp[2],.....,dp[n-1]).

- Sort the given intervals with respect to their ending points.
- The next step is to traverse the intervals from starting. In case we have two overlapping intervals, then we prefer to choose the one with a smaller value of the endpoint.
- We choose interval with the lower endpoint in case of overlapping intervals because it will ensure we can further accommodate the intervals without any overlap.
- We traverse all the intervals using the above-mentioned criteria and return the number of intervals satisfying the given criteria.

SIMILAR PROBLEMS

Randomly Sorted

Posted: 13 Nov, 2022

Difficulty: Moderate

Search In A Sorted 2D Matrix

Posted: 23 Nov, 2022

Difficulty: Moderate

Spiral Matrix

Posted: 24 Nov, 2022

Difficulty: Easy

Ninja And The Strictly Increasing Array

Posted: 27 Nov, 2022

Difficulty: Moderate

Maximize

Posted: 9 Dec, 2022

Difficulty: Easy

Popular Interview Problems: