Talentd
FleetCode
Make and follow sheets.Sign in

Maximum Number of Achievable Transfer Requests solution and explanation

HardArrayBacktrackingBit ManipulationEnumeration7 min read64.5% acceptanceUpdated 9 Apr 2026
Maximum Number of Achievable Transfer Requests | Khandani Backtracking Template | Leetcode-1601codestorywithMIK · 19:44 · 11K viewsWatch on YouTube
9 more videos on this problem
  1. Maximum Number of Achievable Transfer Requests | Khandani Backtracking Template | Leetcode-1601codestorywithMIK · 19:44 · 11K views
  2. Leetcode 1601. Maximum Number of Achievable Transfer RequestsFraz · 12:42 · 4K views
  3. 1601. Maximum Number of Achievable Transfer Requests | Bit ManipulationDeep Tech · 19:35 · 1K views
  4. LeetCode 1601 Maximum Number of Achievable Transfer RequestsJeevan Kumar - Cracckify · 12:44 · 1K views
  5. Maximum Number of Achievable Transfer Requests - Leetcode 1601 - PythonTechError · 9:24 · 1K views
  6. LeetCode 1601. Maximum Number of Achievable Transfer RequestsHappy Coding · 20:50 · 956 views
  7. 1601. Maximum Number of Achievable Transfer Requests (Leetcode Hard)Programming Live with Larry · 7:09 · 644 views
  8. 1601 || Maximum Number of Achievable Transfer Requests || C++ || Leetcode Daily ChallengeCodeFreaks · 21:43 · 368 views
  9. Daily Leetcode #1188: Jul 02, 2023 - 1601. Maximum Number of Achievable Transfer RequestsProgramming Live with Larry · 7:25 · 336 views
  10. 1601 Maximum Number of Achievable Transfer Requests javaSloppy Guy · 18:32 · 236 views

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

def backtrack(indegree, start, n, count, requests):
    global max_requests
    if start == len(requests):
        if all(x == 0 for x in indegree):
            max_requests = max(max_requests, count)
        return

    # Choose the current request
    indegree[requests[start][0]] -= 1
    indegree[requests[start][1]] += 1
    backtrack(indegree, start + 1, n, count + 1, requests)

    # Backtrack
    indegree[requests[start][0]] += 1
    indegree[requests[start][1]] -= 1

    # Skip the current request
    backtrack(indegree, start + 1, n, count, requests)


def maximumRequests(n, requests):
    global max_requests
    max_requests = 0
    indegree = [0] * n
    backtrack(indegree, 0, n, 0, requests)
    return max_requests

# Testing the implementation
print(maximumRequests(5, [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]))
print(maximumRequests(3, [[0,0],[1,2],[2,1]]))
print(maximumRequests(4, [[0,3],[3,1],[1,2],[2,0]]))
public class MaxAchievableRequests {
    int max_requests = 0;

    public void backtrack(int[] indegree, int start, int n, int count, int[][] requests) {
        if (start == requests.length) {
            for (int i = 0; i < n; i++) {
                if (indegree[i] != 0) return;
            }
            max_requests = Math.max(max_requests, count);
            return;
        }

        indegree[requests[start][0]]--;
        indegree[requests[start][1]]++;
        backtrack(indegree, start + 1, n, count + 1, requests);
        indegree[requests[start][0]]++;
        indegree[requests[start][1]]--;

        backtrack(indegree, start + 1, n, count, requests);
    }

    public int maximumRequests(int n, int[][] requests) {
        int[] indegree = new int[n];
        backtrack(indegree, 0, n, 0, requests);
        return max_requests;
    }

    public static void main(String[] args) {
        MaxAchievableRequests mar = new MaxAchievableRequests();
        int[][] requests1 = {{0, 1}, {1, 0}, {0, 1}, {1, 2}, {2, 0}, {3, 4}};
        System.out.println(mar.maximumRequests(5, requests1));

        int[][] requests2 = {{0, 0}, {1, 2}, {2, 1}};
        System.out.println(mar.maximumRequests(3, requests2));

        int[][] requests3 = {{0, 3}, {3, 1}, {1, 2}, {2, 0}};
        System.out.println(mar.maximumRequests(4, requests3));
    }
}
#include <iostream>
#include <vector>

using namespace std;

int max_requests = 0;

void backtrack(vector<int>& indegree, int start, int n, int requestsSize, int count, vector<vector<int>>& requests) {
    if (start == requestsSize) {
        for (int i = 0; i < n; ++i) {
            if (indegree[i] != 0) return;
        }
        max_requests = max(max_requests, count);
        return;
    }

    indegree[requests[start][0]]--;
    indegree[requests[start][1]]++;
    backtrack(indegree, start + 1, n, requestsSize, count + 1, requests);
    indegree[requests[start][0]]++;
    indegree[requests[start][1]]--;

    backtrack(indegree, start + 1, n, requestsSize, count, requests);
}

int maximumRequests(int n, vector<vector<int>>& requests) {
    vector<int> indegree(n, 0);
    backtrack(indegree, 0, n, requests.size(), 0, requests);
    return max_requests;
}

int main() {
    vector<vector<int>> requests1 = {{0, 1}, {1, 0}, {0, 1}, {1, 2}, {2, 0}, {3, 4}};
    cout << maximumRequests(5, requests1) << endl;

    vector<vector<int>> requests2 = {{0, 0}, {1, 2}, {2, 1}};
    cout << maximumRequests(3, requests2) << endl;

    vector<vector<int>> requests3 = {{0, 3}, {3, 1}, {1, 2}, {2, 0}};
    cout << maximumRequests(4, requests3) << endl;
    
    return 0;
}
function backtrack(indegree, start, n, count, requests) {
    if (start === requests.length) {
        if (indegree.every(x => x === 0)) {
            maxRequests = Math.max(maxRequests, count);
        }
        return;
    }

    indegree[requests[start][0]]--;
    indegree[requests[start][1]]++;
    backtrack(indegree, start + 1, n, count + 1, requests);

    indegree[requests[start][0]]++;
    indegree[requests[start][1]]--;
    backtrack(indegree, start + 1, n, count, requests);
}

let maxRequests;

function maximumRequests(n, requests) {
    maxRequests = 0;
    const indegree = Array(n).fill(0);
    backtrack(indegree, 0, n, 0, requests);
    return maxRequests;
}

// Testing the function
console.log(maximumRequests(5, [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]));
console.log(maximumRequests(3, [[0,0],[1,2],[2,1]]));
console.log(maximumRequests(4, [[0,3],[3,1],[1,2],[2,0]]));
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int max_requests;

void backtrack(int* indegree, int start, int n, int total_requests, int count, int (*requests)[2], int requestsSize) {
    if (start == requestsSize) {
        // Check balance for all buildings
        for (int i = 0; i < n; i++) {
            if (indegree[i] != 0) {
                return;
            }
        }
        if (count > max_requests) {
            max_requests = count;
        }
        return;
    }

    // Choose the current request
    indegree[requests[start][0]]--;
    indegree[requests[start][1]]++;
    backtrack(indegree, start + 1, n, total_requests, count + 1, requests, requestsSize);
    
    // Backtrack
    indegree[requests[start][0]]++;
    indegree[requests[start][1]]--;

    // Skip the current request
    backtrack(indegree, start + 1, n, total_requests, count, requests, requestsSize);
}

int maximumRequests(int n, int** requests, int requestsSize, int* requestsColSize) {
    int indegree[n];
    memset(indegree, 0, sizeof(indegree));
    max_requests = 0;
    backtrack(indegree, 0, n, requestsSize, 0, (int (*)[2])requests, requestsSize);
    return max_requests;
}

int main() {
    int requests1[][2] = {{0, 1}, {1, 0}, {0, 1}, {1, 2}, {2, 0}, {3, 4}};
    int requestsSize1 = 6;
    int requests1ColumnSize = 2;
    printf("%d\n", maximumRequests(5, (int **)requests1, requestsSize1, &requests1ColumnSize));

    int requests2[][2] = {{0, 0}, {1, 2}, {2, 1}};
    int requestsSize2 = 3;
    int requests2ColumnSize = 2;
    printf("%d\n", maximumRequests(3, (int **)requests2, requestsSize2, &requests2ColumnSize));

    int requests3[][2] = {{0, 3}, {3, 1}, {1, 2}, {2, 0}};
    int requestsSize3 = 4;
    int requests3ColumnSize = 2;
    printf("%d\n", maximumRequests(4, (int **)requests3, requestsSize3, &requests3ColumnSize));

    return 0;
}
using System;
using System.Collections.Generic;

public class MaxAchievableRequests {
    private int max_requests = 0;

    public void Backtrack(int[] indegree, int start, int n, int count, int[][] requests) {
        if (start == requests.Length) {
            for (int i = 0; i < n; i++) {
                if (indegree[i] != 0) return;
            }
            max_requests = Math.Max(max_requests, count);
            return;
        }

        indegree[requests[start][0]]--;
        indegree[requests[start][1]]++;
        Backtrack(indegree, start + 1, n, count + 1, requests);
        indegree[requests[start][0]]++;
        indegree[requests[start][1]]--;

        Backtrack(indegree, start + 1, n, count, requests);
    }

    public int MaximumRequests(int n, int[][] requests) {
        int[] indegree = new int[n];
        Backtrack(indegree, 0, n, 0, requests);
        return max_requests;
    }

    public static void Main(string[] args) {
        MaxAchievableRequests mar = new MaxAchievableRequests();
        int[][] requests1 = new int[][] { new int[] {0, 1}, new int[] {1, 0}, new int[] {0, 1}, new int[] {1, 2}, new int[] {2, 0}, new int[] {3, 4} };
        Console.WriteLine(mar.MaximumRequests(5, requests1));

        int[][] requests2 = new int[][] { new int[] {0, 0}, new int[] {1, 2}, new int[] {2, 1} };
        Console.WriteLine(mar.MaximumRequests(3, requests2));

        int[][] requests3 = new int[][] { new int[] {0, 3}, new int[] {3, 1}, new int[] {1, 2}, new int[] {2, 0} };
        Console.WriteLine(mar.MaximumRequests(4, requests3));
    }
}

The Python implementation uses recursion to explore each subset of requests. It updates a global maximum count when all building transfers result in zero net transfer.

Complexity

Time Complexity: O(2^R * N) due to exploring all subsets for tracking request approaches.

Space Complexity: O(N) for the indegree array.

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.

def maximumRequests(n, requests):
    m = len(requests)
    max_res = 0
    for mask in range(1 << m):
        indegree = [0] * n
        num_requests = 0
        for i in range(m):
            if mask & (1 << i):
                indegree[requests[i][0]] -= 1
                indegree[requests[i][1]] += 1
                num_requests += 1
        if all(x == 0 for x in indegree):
            max_res = max(max_res, num_requests)
    return max_res

# Testing the function
print(maximumRequests(5, [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]))
print(maximumRequests(3, [[0,0],[1,2],[2,1]]))
print(maximumRequests(4, [[0,3],[3,1],[1,2],[2,0]]))
import java.util.Arrays;

public class MaxAchievableRequestsBitmask {

    public int maximumRequests(int n, int[][] requests) {
        int m = requests.length;
        int[] dp = new int[1 << m];
        int maxRes = 0;

        for (int mask = 0; mask < (1 << m); mask++) {
            int[] indegree = new int[n];
            int cnt = 0;

            for (int i = 0; i < m; i++) {
                if ((mask & (1 << i)) != 0) {
                    cnt++;
                    indegree[requests[i][0]]--;
                    indegree[requests[i][1]]++;
                }
            }

            boolean valid = true;
            for (int num : indegree) {
                if (num != 0) {
                    valid = false;
                    break;
                }
            }

            if (valid) {
                dp[mask] = cnt;
                maxRes = Math.max(maxRes, cnt);
            }
        }

        return maxRes;
    }

    public static void main(String[] args) {
        MaxAchievableRequestsBitmask marb = new MaxAchievableRequestsBitmask();
        int[][] requests1 = {{0, 1}, {1, 0}, {0, 1}, {1, 2}, {2, 0}, {3, 4}};
        System.out.println(marb.maximumRequests(5, requests1));

        int[][] requests2 = {{0, 0}, {1, 2}, {2, 1}};
        System.out.println(marb.maximumRequests(3, requests2));

        int[][] requests3 = {{0, 3}, {3, 1}, {1, 2}, {2, 0}};
        System.out.println(marb.maximumRequests(4, requests3));
    }
}
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int maximumRequests(int n, vector<vector<int>>& requests) {
    int m = requests.size();
    vector<int> dp(1 << m, 0);
    int maxRes = 0;

    for (int mask = 0; mask < (1 << m); mask++) {
        vector<int> indegree(n, 0);
        int cnt = 0;
        for (int i = 0; i < m; i++) {
            if (mask & (1 << i)) {
                cnt++;
                indegree[requests[i][0]]--;
                indegree[requests[i][1]]++;
            }
        }

        bool valid = true;
        for (int j = 0; j < n; j++) {
            if (indegree[j] != 0) {
                valid = false;
                break;
            }
        }
        if (valid) {
            dp[mask] = cnt;
            maxRes = max(maxRes, cnt);
        }
    }
    return maxRes;
}

int main() {
    vector<vector<int>> requests1 = {{0, 1}, {1, 0}, {0, 1}, {1, 2}, {2, 0}, {3, 4}};
    cout << maximumRequests(5, requests1) << endl;

    vector<vector<int>> requests2 = {{0, 0}, {1, 2}, {2, 1}};
    cout << maximumRequests(3, requests2) << endl;

    vector<vector<int>> requests3 = {{0, 3}, {3, 1}, {1, 2}, {2, 0}};
    cout << maximumRequests(4, requests3) << endl;
    
    return 0;
}
function maximumRequests(n, requests) {
    const m = requests.length;
    let maxRes = 0;

    for (let mask = 0; mask < (1 << m); mask++) {
        const indegree = Array(n).fill(0);
        let num_requests = 0;

        for (let i = 0; i < m; i++) {
            if (mask & (1 << i)) {
                indegree[requests[i][0]]--;
                indegree[requests[i][1]]++;
                num_requests++;
            }
        }

        if (indegree.every(x => x === 0)) {
            maxRes = Math.max(maxRes, num_requests);
        }
    }
    return maxRes;
}

// Testing the function
console.log(maximumRequests(5, [[0,1],[1,0],[0,1],[1,2],[2,0],[3,4]]));
console.log(maximumRequests(3, [[0,0],[1,2],[2,1]]));
console.log(maximumRequests(4, [[0,3],[3,1],[1,2],[2,0]]));
#include <stdio.h>
#include <string.h>

int dp[1<<16];

int max(int a, int b) {
    return a > b ? a : b;
}

int maximumRequests(int n, int** requests, int requestsSize, int* requestsColSize) {
    memset(dp, -1, sizeof(dp));
    dp[0] = 0;
    int maxRes = 0;

    for (int mask = 0; mask < (1 << requestsSize); mask++) {
        if (dp[mask] == -1) continue;

        int indegree[n];
        memset(indegree, 0, sizeof(indegree));

        for (int i = 0; i < requestsSize; i++) {
            if (mask & (1 << i)) {
                indegree[requests[i][0]]--;
                indegree[requests[i][1]]++;
            }
        }

        int next_mask = (~mask) & ((1 << requestsSize) - 1);
        for (int j = 0; j < requestsSize; j++) {
            if (next_mask & (1 << j)) {
                dp[mask | (1 << j)] = max(dp[mask | (1 << j)], dp[mask] + 1);
            }
        }

        int valid = 1;
        for (int k = 0; k < n; k++) {
            if (indegree[k] != 0) {
                valid = 0;
                break;
            }
        }
        if (valid) maxRes = max(maxRes, dp[mask]);
    }

    return maxRes;
}

int main() {
    int requests1[][2] = {{0, 1}, {1, 0}, {0, 1}, {1, 2}, {2, 0}, {3, 4}};
    int requestsSize1 = 6;
    int requests1ColumnSize = 2;
    printf("%d\n", maximumRequests(5, (int **)requests1, requestsSize1, &requests1ColumnSize));

    int requests2[][2] = {{0, 0}, {1, 2}, {2, 1}};
    int requestsSize2 = 3;
    int requests2ColumnSize = 2;
    printf("%d\n", maximumRequests(3, (int **)requests2, requestsSize2, &requests2ColumnSize));

    int requests3[][2] = {{0, 3}, {3, 1}, {1, 2}, {2, 0}};
    int requestsSize3 = 4;
    int requests3ColumnSize = 2;
    printf("%d\n", maximumRequests(4, (int **)requests3, requestsSize3, &requests3ColumnSize));

    return 0;
}
using System;

public class MaxAchievableRequestsBitmask {
    public int MaximumRequests(int n, int[][] requests) {
        int m = requests.Length;
        int maxRes = 0;

        for (int mask = 0; mask < (1 << m); mask++) {
            int[] indegree = new int[n];
            int num_requests = 0;

            for (int i = 0; i < m; i++) {
                if ((mask & (1 << i)) != 0) {
                    indegree[requests[i][0]]--;
                    indegree[requests[i][1]]++;
                    num_requests++;
                }
            }

            bool valid = true;
            foreach (var deg in indegree) {
                if (deg != 0) {
                    valid = false;
                    break;
                }
            }

            if (valid) {
                maxRes = Math.Max(maxRes, num_requests);
            }
        }

        return maxRes;
    }

    public static void Main(string[] args) {
        MaxAchievableRequestsBitmask marb = new MaxAchievableRequestsBitmask();
        int[][] requests1 = new int[][] { new int[] {0, 1}, new int[] {1, 0}, new int[] {0, 1}, new int[] {1, 2}, new int[] {2, 0}, new int[] {3, 4} };
        Console.WriteLine(marb.MaximumRequests(5, requests1));

        int[][] requests2 = new int[][] { new int[] {0, 0}, new int[] {1, 2}, new int[] {2, 1} };
        Console.WriteLine(marb.MaximumRequests(3, requests2));

        int[][] requests3 = new int[][] { new int[] {0, 3}, new int[] {3, 1}, new int[] {1, 2}, new int[] {2, 0} };
        Console.WriteLine(marb.MaximumRequests(4, requests3));
    }
}

The Python solution explores each potential set subset using a bitmask. The indegree is summed for each subset, checking balance, and updating the maximum achievable number of requests accordingly.

Complexity

Time Complexity: O(2^R * R * N) where R is the number of requests; each mask is iterated for balance.

Space Complexity: O(1) - we do not maintain a space-consuming dp list outside boolean data.

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.

function maximumRequests(n: number, requests: number[][]): number {
    const m = requests.length;
    let ans = 0;
    const check = (mask: number): boolean => {
        const cnt = Array(n).fill(0);
        for (let i = 0; i < m; ++i) {
            if ((mask >> i) & 1) {
                const [f, t] = requests[i];
                --cnt[f];
                ++cnt[t];
            }
        }
        return cnt.every(v => v === 0);
    };
    for (let mask = 0; mask < 1 << m; ++mask) {
        const cnt = bitCount(mask);
        if (ans < cnt && check(mask)) {
            ans = cnt;
        }
    }
    return ans;
}

function bitCount(i: number): number {
    i = i - ((i >>> 1) & 0x55555555);
    i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
    i = (i + (i >>> 4)) & 0x0f0f0f0f;
    i = i + (i >>> 8);
    i = i + (i >>> 16);
    return i & 0x3f;
}
func maximumRequests(n int, requests [][]int) (ans int) {
	m := len(requests)
	check := func(mask int) bool {
		cnt := make([]int, n)
		for i, r := range requests {
			if mask>>i&1 == 1 {
				f, t := r[0], r[1]
				cnt[f]--
				cnt[t]++
			}
		}
		for _, v := range cnt {
			if v != 0 {
				return false
			}
		}
		return true
	}
	for mask := 0; mask < 1<<m; mask++ {
		cnt := bits.OnesCount(uint(mask))
		if ans < cnt && check(mask) {
			ans = cnt
		}
	}
	return
}
Try this approach in the editor

Complexity comparison

ApproachComplexity
Backtracking Approach

Time Complexity: O(2^R * N) due to exploring all subsets for tracking request approaches.

Space Complexity: O(N) for the indegree array.

Bitmask DP Approach

Time Complexity: O(2^R * R * N) where R is the number of requests; each mask is iterated for balance.

Space Complexity: O(1) - we do not maintain a space-consuming dp list outside boolean data.

Binary Enumeration–

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

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.

Comments

Now write it yourselfClose this page and solve Maximum Number of Achievable Transfer Requests in the editor, without looking.
Practise Maximum Number of Achievable Transfer Requests