Maximum Number of Achievable Transfer Requests - Problem

Imagine a large corporate campus with n buildings numbered from 0 to n-1. It's transfer season, and employees want to move between buildings!

You're given an array of requests where each requests[i] = [from_i, to_i] represents an employee's request to transfer from building from_i to building to_i.

Here's the catch: all buildings are at full capacity. This means a set of transfer requests can only be approved if the net change for each building is zero - the number of employees leaving must equal the number moving in.

Goal: Find the maximum number of transfer requests that can be simultaneously approved while maintaining this balance constraint.

Example: If 2 employees leave building 0 and 1 leaves building 1, then exactly 2 must move to building 0 and 1 must move to building 1 for the transfers to be achievable.

Input & Output

example_1.py โ€” Basic Cycle
$ Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
โ€บ Output: 5
๐Ÿ’ก Note: We can approve requests [0,1], [1,0], [0,1], [1,2], [2,0]. Building 0: +1-1+1-1 = 0, Building 1: -1+1-1+1 = 0, Building 2: -1+1 = 0. All buildings balanced.
example_2.py โ€” Simple Swap
$ Input: n = 3, requests = [[0,0],[1,2],[2,1]]
โ€บ Output: 3
๐Ÿ’ก Note: All three requests can be approved. Building 0: 0, Building 1: -1+1 = 0, Building 2: +1-1 = 0. Perfect balance achieved.
example_3.py โ€” No Valid Combination
$ Input: n = 4, requests = [[0,3],[3,1],[1,2],[2,0]]
โ€บ Output: 4
๐Ÿ’ก Note: This forms a complete cycle: 0โ†’3โ†’1โ†’2โ†’0. All four requests can be approved simultaneously as they create a balanced cycle.

Constraints

  • 1 โ‰ค n โ‰ค 20
  • 1 โ‰ค requests.length โ‰ค 16
  • requests[i].length == 2
  • 0 โ‰ค fromi, toi < n
  • Small input size allows exponential solutions

Visualization

Tap to expand
Building AFull CapacityBuilding BFull CapacityBuilding CFull CapacityTransfer RequestReturn Transfer-1+1Net: 0 โœ“+1-1Net: 0 โœ“No transfersNet: 0 โœ“๐ŸŽฏ Goal: Maximum Balanced Transfersโ€ข Each building must maintain net change = 0โ€ข Find largest subset of requests satisfying this constraintโ€ข Use backtracking to explore all valid combinations
Understanding the Visualization
1
Setup
Buildings are at full capacity, employees want to transfer
2
Constraint
For every employee leaving, someone must take their place
3
Search
Find the largest set of transfers that maintains balance
4
Solution
Return the maximum number of achievable requests
Key Takeaway
๐ŸŽฏ Key Insight: This is a constraint satisfaction problem requiring us to find the maximum subset of requests where building capacity changes balance out to zero.
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
18.5K Views
Medium Frequency
~25 min Avg. Time
842 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