Apartments

You are given two integer arrays applicants and apartments representing the desired apartment sizes of applicants and the actual sizes of available apartments, respectively.

You are also given an integer k. An applicant with desired size x will accept an apartment of size y if |x - y| <= k.

Each applicant can get at most one apartment, and each apartment can be given to at most one applicant. Return the maximum number of applicants who can get an apartment.

Example 1:

Input: applicants = [60, 45, 80, 60], apartments = [30, 60, 75], k = 5

Output: 2

Explanation:

Applicant 60 takes apartment 60. Applicant 80 takes apartment 75.

Example 2:

Input: applicants = [10, 20, 30], apartments = [15, 25], k = 5

Output: 2

Explanation:

Applicant 10 takes apartment 15. Applicant 20 takes apartment 25.

Now Your Turn!

Pick the correct output for the given input

Input: applicants = [10, 10, 10], apartments = [9, 11, 10], k = 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= applicants.length, apartments.length <= 2 x 105
  • 0 <= k <= 109
  • 1 <= applicants[i], apartments[j] <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int Apartments(vector<int>& applicants, vector<int>& apartments, int k) {
// Write your code here
}
};
Test Case

Input:

Apartments
Applicants
K