
The first line of input contains an integer ‘T’, denoting the number of test cases. The test cases follow.
The first line of each test case contains three space-separated integers ‘N’, ‘M’, and ‘K’, which denotes the number of applicants, the number of free apartments, and the maximum allowed difference.
The second line of each test case contains 'N' space-separated integers denoting the elements of the array ‘desiredSize’.
The third line of each test case contains 'M' space-separated integers denoting the elements of the array ‘apartmentSize’.
For each test case, print the number of apartments Ninja can sell.
Print the output of each test case in a separate line.
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= N, M <= 10^3
0 <= K <= 10^9
1 <= desiredSize[i], apartmentSize[j] <= 10^9
Where 'desiredSize[i]' denotes the desired size of the apartment of i’th applicant, 'apartmentSize[i]' denotes the size of the apartment at index ‘i’.
Time Limit: 1 sec
The idea is to sort both the arrays and then start searching from the beginning. If the size of the apartment lies in the range of the desired size, then we will give the apartment to the person. Otherwise, if the size of the desired apartment is too big, then we will move to the next apartment to search for, and if the size of the apartment is too small, then we will skip the apartment and move to the next applicant. The intuition behind the approach is that we have the sizes of all the desired apartments and the given size of the apartments in ascending order which means instead of searching over all the apartments for a single person, we are iterating over both the arrays. If it suits the person, sell it to the current person. Otherwise, change the person or apartment accordingly. In this approach, we don’t need to check all the apartments for a single person.
Algorithm :