Mr. X is a busy person. He has to attend 'N' meetings throughout the day. You are given the schedule of Mr. X in a 2D Matrix 'MEETINGS' having 'N' rows and 2 columns. The 'i'th element of the first column contains the starting time of the 'i'th meeting, and the 'i'th element of the second column contains the ending time of the 'i'th meeting.
Two meetings are conflicting with each other if they overlap each other for some non-zero time. If a meeting 'X' starts at the same time as the meeting 'Y' ends, then they do not conflict.
Given the schedule of the day of Mr. X. Find the index of any one conflicting meeting for each of the 'N' meetings.
In case a particular meeting does not conflict with any meeting, take -1 as the index of the conflicting meeting for that meeting.
Note :
If there are multiple conflicting meetings for a particular meeting. You can return any one of them.
Example :
Consider the matrix MEETINGS = [ [ 1, 3 ] , [ 4, 5 ] , [ 2, 5 ] ]
The array containing the Conflicting Meetings will be [ 3, 3, 1 ].
The first line of the input contains an integer 'T,’ denoting the number of test cases.
The first line of each test case contains an integer 'N', denoting the number of meetings.
The second line of each test case contains 'N' space-separated integers denoting the first column of the matrix 'MEETINGS'.
The third line of each test case contains 'N' space-separated integers denoting the second column of the matrix 'MEETINGS'.
Output Format:
For each test case, return the array of conflicting meetings.
The output is '1' if the returned conflicting meetings array is correct, otherwise, it will print '0'.
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 <= 10^4
1 <= MEETING[i][0] < 10^9
MEETING[i][0] < MEETING[i][1] <= 10^9
Time Limit : 1 sec
2
3
1 2 5
3 4 7
2
1 3
2 4
1
1
For the first test case,
The two meetings, Meeting 1 and Meeting 2 overlap with each other. Hence, they are conflicting. Meeting 3 does not overlap with any other meeting.
Therefore, the output array is : [ 2, 1, -1 ] in this case.
For the second test case,
Both the meetings do not overlap with each other. So, there are no conflicting meetings.
Therefore, the output array : [ -1, -1 ] in this case.
2
4
6 2 6 1
7 6 8 4
4
5 6 1 3
6 8 4 5
1
1
For the first test case,
The two meetings, Meeting 1 and Meeting 3, overlap with each other, and Meeting 2 and Meeting 4 overlap with each other. Therefore, the output array is : [ 3, 4, 1, 2 ] in this case.
For the second test case,
The first two meetings do not conflict with any of the meetings. The two meetings, Meeting 3 and Meeting 4, overlap with each other. Therefore, the output array is : [ -1, -1, 4, 3 ] in this case.
Try to find out the Conflicting Meeting for each of the meetings by iterating through all the meetings and checking whether the two meetings overlap.
The idea is to find the Conflicting Meeting for each meeting one by one. To find a Conflicting Meeting for any particular meeting, we will iterate through each meeting and check whether the two meetings overlap. If we reach the end of the matrix without finding any conflicting meeting, then we will set the index of the conflicting meeting as -1 for that element.
Here is the algorithm :
O(N^2), where 'N' is the number of meetings.
When none of the meetings overlap, it takes O(N) time to find to iterate the matrix ‘MEETINGS’ for each of the ‘N’ meetings. Hence, the overall Time Complexity is O(N^2).
O(1)
We are not using any extra space.Hence, the overall Space Complexity is O(1).