This page explains Java solution to problem `Maximum Number of Achievable Transfer Requests`

using `Bit Manipulation`

.

We have `n`

buildings numbered from `0`

to `n - 1`

. Each building has a number of employees. It's transfer season, and some employees want to change the building they reside in.

You are given an array requests where `requests[i] = [from`

represents an employee's request to transfer from building from_{i}, to_{i}]_{i} to building to_{i}.

All buildings are full, so a list of requests is achievable only if for each building, the net change in employee transfers is zero. This means the number of employees leaving is equal to the number of employees moving in. For example if `n = 3`

and two employees are leaving building `0`

, one is leaving building `1`

, and one is leaving building `2`

, there should be two employees moving to building `0`

, one employee moving to building `1`

, and one employee moving to building `2`

.

Return the maximum number of achievable requests

Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]Output: 5Explanation: Let's see the requests:

From building 0 we have employees x and y and both want to move to building 1.

From building 1 we have employees a and b and they want to move to buildings 2 and 0 respectively.

From building 2 we have employee z and they want to move to building 0.

From building 3 we have employee c and they want to move to building 4.

From building 4 we don't have any requests.

We can achieve the requests of users x and b by swapping their places.

We can achieve the requests of users y, a and z by swapping the places in the 3 buildings.

Input: n = 3, requests = [[0,0],[1,2],[2,1]]Output: 3Explanation: Let's see the requests:

From building 0 we have employee x and they want to stay in the same building 0.

From building 1 we have employee y and they want to move to building 2.

From building 2 we have employee z and they want to move to building 1.

We can achieve all the requests.

Input: n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]Output: 4

```
package com.vc.hard;
class MaximumNumberOfAchievableTransferRequests {
public int maximumRequests(int n, int[][] requests) {
int r = requests.length;
int max = 0;
for(int subset = 1; subset < (1 << r); subset++) {
int[] buildings = new int[n];
int completedRequest = 0;
for(int bit = 0; bit < r; bit++) {
if(((subset >> bit) & 1) == 1) {
int from = requests[r - bit - 1][0];
int to = requests[r - bit - 1][1];
buildings[from]--;
buildings[to]++;
completedRequest++;
}
}
if(isValid(buildings)) max = Math.max(max, completedRequest);
}
return max;
}
private boolean isValid(int[] buildings) {
for(int building: buildings) if(building != 0) return false;
return true;
}
}
```

O(R) Where

2^{R}is number requests in request input array

O(N) Where

N is number buildings