Maximum Number of Achievable Transfer Requests - Problem

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] = [fromi, toi] represents an employee's request to transfer from building fromi to building toi.

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.

Return the maximum number of achievable transfer requests.

Input & Output

Example 1 — Balanced Transfers
$ Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
Output: 5
💡 Note: We can accept requests [0,1],[1,0],[0,1],[1,2],[2,0]. Net changes: building 0: -1+1-1+1=1-1=0, building 1: +1-1+1-1=0, building 2: +1-1=0, buildings 3,4 unchanged=0. All buildings balanced.
Example 2 — Simple Case
$ Input: n = 3, requests = [[0,0],[1,2],[2,1]]
Output: 3
💡 Note: All requests can be accepted: [0,0] is self-transfer (no net change), [1,2] and [2,1] cancel each other out. Net changes all zero.
Example 3 — No Valid Subset
$ Input: n = 4, requests = [[0,1],[2,3]]
Output: 2
💡 Note: Both requests can be accepted since they involve different pairs of buildings. Building 0: -1, building 1: +1, building 2: -1, building 3: +1. All balanced.

Constraints

  • 1 ≤ n ≤ 20
  • 1 ≤ requests.length ≤ 16
  • requests[i].length == 2
  • 0 ≤ fromi, toi < n

Visualization

Tap to expand
Maximum Number of Achievable Transfer Requests INPUT n = 5 buildings 0 1 2 3 4 Requests Array: [0,1] [1,0] [0,1] [1,2] [2,0] [3,4] Transfer Graph: 0 1 2 3 4 ALGORITHM STEPS 1 Bitmask Enumeration Try all 2^m subsets of requests (m=6, 64 subsets) 2 Track Net Change For each building, compute in - out transfers 3 Validate Subset Check if ALL buildings have net change = 0 4 Maximize Count Track max valid subset size found Valid Subset Example: Mask: 111110 (5 requests) Building 0: -2+1+1 = 0 OK Building 1: +2-2 = 0 OK Building 2: +1-1 = 0 OK Buildings 3,4: +1-1= 0 OK FINAL RESULT Achievable Transfers: 5 Requests Selected: [0,1] [1,0] [0,1] [1,2] [2,0] Excluded (breaks balance): [3,4] Output: 5 Maximum achievable transfer requests All Buildings Balanced! Net change = 0 for each Key Insight: This is an NP-hard problem requiring exponential enumeration. With m requests, we check all 2^m subsets using bitmask. A subset is valid only when every building's net change (employees in - out) equals zero. The [3,4] request is excluded because including it creates an imbalance in buildings 3 and 4. TutorialsPoint - Maximum Number of Achievable Transfer Requests | Optimal Solution (Bitmask Enumeration) Time: O(2^m * n) | Space: O(n) where m = requests, n = buildings
Asked in
Google 12 Amazon 8 Microsoft 6
89.2K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen