Ninja is organizing a meeting in an office that starts at a time ‘0’ and will end at a time ‘LAST’. There are ‘N’ presentations that are going to take place in that meeting whose start and end times are given, i.e., ‘ith’ presentation starts at ‘START[i]’ and ends at ‘END[i]’. The presentations don’t overlap with each other. Ninja wants to reschedule at most ‘K’ presentations keeping the original order intact so that the longest period in which there is no presentation scheduled during the meeting is maximized.
Since Ninja is busy with other office work, your task is to help Ninja to reschedule at most ‘K’ presentations.
Note:The presentation’s duration can’t be changed. You can only change the start and end times.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains three positive integers, ‘N’, ‘K’, and ‘LAST’, as described in the problem statement.
The following ‘N’ line of each test case contains two non-negative integers denoting ‘start’ and ‘end’ time of ith presentation.
Output Format:
For each test case, print the duration of the longest period in which there is no presentation scheduled during the meeting after rescheduling, not more than ‘K’ meetings.
Output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5000
0 <= K <= N
0 <= LAST <= 10^9
START[i] < END[i], for 1 <= i <= N.
END[i] <= START[i+1], for 1 <= i < N.
Time Limit: 1 second
1
2 1 4
0 1
2 3
2
If we reschedule the second meeting from 2 to 1, then the duration from 2 to 4 will be free, and it is the longest among all possible answers.
1
3 0 11
2 4
4 5
8 10
3
Maximum sum subarray of size ‘K+1’.
Algorithm:
O(N ^ 2), where ‘N’ is the number of the presentations.
We are calculating the subarray sum of all subarray of size K + 1. So for each element, we are calculating the subarray sum. As calculating subarray sum it takes O(N) time, and for each element, it will go to O(N^2).
O(N), where ‘N’ is the number of the presentations.
As we are using an extra array to store the empty slots, which can go up to the size ‘N’. Hence, space complexity will be O(N).