Skip to main content

Maximum Number of Achievable Transfer Requests - Solution & Explanation

HardArrayBacktrackingBit ManipulationEnumeration26 min readAsked at: Amazon
Practice this problem

Problem Statement

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. 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.

 

Example 1:

Input: n = 5, requests = [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]
Output: 5
Explantion: 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.

Example 2:

Input: n = 3, requests = [[0,0],[1,2],[2,1]]
Output: 3
Explantion: 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. 

Example 3:

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

 

Constraints:

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

Approach Overview

Problem Overview: You are given n buildings and a list of employee transfer requests where each request moves one employee from building from to building to. The goal is to accept the maximum number of requests such that the net change of employees in every building remains zero.

Approach 1: Backtracking with Balance Tracking (Time: O(2^m * n), Space: O(n))

Use backtracking to explore whether each request should be accepted or skipped. Maintain an array balance[n] where accepting a request decrements balance[from] and increments balance[to]. Recursively process all requests and track how many are chosen. When all requests are considered, verify that every building has zero balance; if so, update the maximum count. This approach prunes the search naturally and works well because the number of requests is small (≤20). It is intuitive for interviews and clearly demonstrates the constraint that every building must end balanced.

Approach 2: Bitmask Enumeration (Time: O(2^m * m), Space: O(n))

Enumerate all subsets of requests using bit manipulation. A bitmask from 0 to (1 << m) - 1 represents which requests are selected. For each mask, count the number of set bits and compute the building balance by iterating through the selected requests. If every building's balance is zero, the subset is valid and you update the answer with the maximum size. Iterating through subsets makes this a classic enumeration technique. Although it checks every subset, the constraint m ≤ 20 keeps the total manageable.

Recommended for interviews: The backtracking solution is usually expected because it models the decision process directly and allows pruning when the remaining requests cannot beat the current best. Bitmask enumeration is also acceptable and sometimes faster to implement, especially if you are comfortable with subset iteration using bit operations.

Approach 1: Backtracking Approach

This approach involves generating all subsets of requests and checking if they result in a balanced transfer of employees among all buildings. We maximize the number of requests in the subset where the number of inputs equals the number of outputs for every building.

This implementation uses backtracking. We use an indegree array to track the net transfer requests at each building. We consider each request to either choose or not choose a request while backtracking. We update maximum requests when all buildings are balanced at zero net requests.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(2^R * N), where R is the number of requests and N is the number of buildings.

Space Complexity: O(N), mainly because of the indegree array used for tracking the net inflow for each building.

Try this approach in the editor →

Approach 2: Bitmask DP Approach

This approach uses dynamic programming with bitmasking, reducing redundant calculations by storing results of previously computed states. It leverages the idea of subsets, where we test combinations represented as binary masks and check balance conditions for employee transfers.

The C solution uses a bitmask dynamic programming approach. It stores intermediate results of various combinations of requests in a dp array. The mask iterates over possible subsets of requests, updating indegree array and maximizing valid request count.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(2^R * R * N) where R is the number of requests and N is the number of buildings.

Space Complexity: O(2^R) for dp array holding states by subsets of requests.

Try this approach in the editor →

Approach 3: Binary Enumeration

We note that the length of the room change request list does not exceed 16. Therefore, we can use the method of binary enumeration to enumerate all room change request lists. Specifically, we can use a binary number of length 16 to represent a room change request list, where the i-th bit being 1 means the i-th room change request is selected, and 0 means the i-th room change request is not selected.

We enumerate all binary numbers in the range of [1, 2^{m}), for each binary number mask, we first calculate how many 1s are in its binary representation, denoted as cnt. If cnt is larger than the current answer ans, then we judge whether mask is a feasible room change request list. If it is, then we update the answer ans with cnt. To judge whether mask is a feasible room change request list, we only need to check whether the net inflow of each room is 0.

The time complexity is O(2^m times (m + n)), and the space complexity is O(n), where m and n are the lengths of the room change request list and the number of rooms, respectively.

Code

Python

Java

C++

Go

TypeScript

JavaScript

C#

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Backtracking Approach

Time Complexity: O(2^R * N), where R is the number of requests and N is the number of buildings.

Space Complexity: O(N), mainly because of the indegree array used for tracking the net inflow for each building.

Bitmask DP Approach

Time Complexity: O(2^R * R * N) where R is the number of requests and N is the number of buildings.

Space Complexity: O(2^R) for dp array holding states by subsets of requests.

Binary Enumeration—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Backtracking with Balance ArrayO(2^m * n)O(n)Best for interviews; easy to reason about and supports pruning while exploring request decisions
Bitmask EnumerationO(2^m * m)O(n)Good when comfortable with subset iteration using bit operations; concise implementation

Video Solution

Maximum Number of Achievable Transfer Requests | Khandani Backtracking Template | Leetcode-1601 • codestorywithMIK • 11,140 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Maximum Number of Achievable Transfer Requests easy or hard?
The problem is labeled Hard because it requires exploring exponential combinations while maintaining a constraint across multiple nodes. The logic is manageable once you recognize it as a subset enumeration or backtracking problem.
Maximum Number of Achievable Transfer Requests Python/Java solution
Both Python and Java implementations typically use backtracking with a balance array or iterate through subsets using bitmasks. Each approach checks whether the final balance of every building is zero before updating the maximum request count.
How to solve Maximum Number of Achievable Transfer Requests in O(n)?
An O(n) solution is not possible because the problem requires checking combinations of requests. The constraints allow up to 20 requests, so the accepted approach is exploring subsets using backtracking or bitmask enumeration with O(2^m) complexity.
What is the best approach for Maximum Number of Achievable Transfer Requests?
Backtracking with a building balance array is the most common approach. You recursively decide whether to include each request while tracking the net employee change for every building. If all balances become zero after processing a subset, the subset is valid. The approach runs in O(2^m * n) time and O(n) space.
Is Maximum Number of Achievable Transfer Requests asked at Google/Amazon/Meta?
Backtracking and subset enumeration problems like this commonly appear in interviews at companies such as Google, Amazon, and Meta. They test your ability to explore combinations while maintaining constraints using arrays and recursion.
What data structure is used in Maximum Number of Achievable Transfer Requests?
The core data structure is an array that tracks the net employee balance for each building. The algorithms also rely on recursion for backtracking or integer bitmasks for subset enumeration.
What is the time complexity of Maximum Number of Achievable Transfer Requests?
The typical complexity is O(2^m * n) for backtracking or O(2^m * m) for bitmask enumeration, where m is the number of transfer requests. Each subset of requests is evaluated to check if all building balances return to zero.

Ready to solve this problem?

Practice Maximum Number of Achievable Transfer Requests with our built-in code editor and test cases.

Practice on FleetCode