![]() |
VOOZH | about |
There are N applicants and M free apartments. Your task is to distribute the apartments so that as many applicants as possible will get an apartment. Each applicant has a desired apartment size given in array A[] of size N and each apartment has a size given in array B[] of size M. An applicant will accept any apartment whose size is close enough to the desired size. The applicant will accept the apartment if the absolute difference between the size of the apartment and his desired apartment size <= K.
Examples:
Input: N = 4, M = 3, K = 5, A[] = {60, 45, 80, 60}, B[] = {30, 60, 75}
Output: 2
Explanation:
- The first applicant can take any apartment which has size in range 55 to 65. So, he will take the apartment with size 60.
- The second applicant can take any apartment which has size in range 40 to 50. So, he won't be able to take any apartment.
- The third applicant can take any apartment which has size in range 75 to 85. So, he will take the apartment with size 75.
- The fourth applicant can take any apartment which has size in range 55 to 65. So, he won't be able to take any apartment as the apartment with size 60 is already taken by second applicant.
Input: N = 3, M = 3, K = 10, A[] = {30, 40, 50}, B[] = {40, 50, 60}
Output: 3
Explanation:
- The first applicant can take any apartment which has size in range 20 to 40. So, he will take the apartment with size 40.
- The second applicant can take any apartment which has size in range 30 to 50. So, he will take the apartment with size 50.
- The third applicant can take any apartment which has size in range 40 to 60. So, he will take the apartment with size 60.
Approach: To solve the problem, follow the below idea:
The problem can be solved using a Greedy approach. We can sort both the arrays A[] and B[] in ascending order and maintain two pointers, say ptrA and ptrB to mark the current index. Now, we can run a loop till we reach the end of one of the array. According to the values of A[] and B, we can have 3 cases:
- abs(A[ptrA] - B[ptrB]) <= K: This means that the applicant at index ptrA can accept the apartment at index ptrB. So, we will increment the answer by 1 and move to the next applicant and apartment.
- A[ptrA] < B[ptrB]: This means that the apartment at index ptrB has much greater size that the applicant's desired size. Since, we have already allotted all the apartments with smaller size (as the arrays are sorted in ascending order) the only choice is to move to the next applicant whose desired size will be greater.
- A[ptrA] > B[ptrB]: This means that the apartment at index ptrB has much smaller size that the applicant's desired size. Since, all the applicants who desired a smaller apartment have already got their apartments (as the arrays are sorted in ascending order) the only choice is to move to the next apartment whose size will be greater.
Step-by-step algorithm:
Below is the implementation of the algorithm:
2
Time Complexity: O(NlogN + MlogM), where N is the number of applicants and M is the number of apartments.
Auxiliary Space: O(1)