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 RequestsInput: n=3, requests=[[0,1],[1,0],[0,1]]012Building 0Building 1Building 2[0,1][1,0][0,1]Check All Possible SubsetsValid Subset: {[0,1], [1,0]}Building 0: -1+1=0 ✓Building 1: +1-1=0 ✓Output: Maximum 2 requests can be achieved
Understanding the Visualization
1
Input
Buildings and transfer requests between them
2
Process
Find subset where each building has zero net change
3
Output
Maximum number of achievable requests
Key Takeaway
🎯 Key Insight: Valid transfers require each building to have equal employees leaving and entering
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