Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximum Number of Achievable Transfer Requests

XPChallenge Points: 30
levelLevel: Hard

You are given n buildings labeled from 0 to n - 1. Each building initially has a fixed number of employees. During transfer season, employees may request to move from one building to another.

You are given an array requests, where each entry requests[i] = [from, to] indicates a single employee wants to move from building from to building to.

A group of transfer requests is considered valid only when, for every building, the total number of employees leaving equals the total number of employees arriving. In other words, each building must end with the same employee count it started with.

Your task is to return the maximum number of requests that can be fulfilled while still maintaining this balance.

Example 1:

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

Output: 4

Explanation:

A valid achievable set is: 0→1, 1→2, 2→3, 3→0. These form a full cycle. The remaining request (1→0) breaks balance. So the maximum achievable is 4.

Example 2:

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

Output: 3

Explanation:

Possible valid set includes: [0→1], [1→0], [2→1] or [1→2]. Choose any 3 forming a balanced flow.

Example 3:

Input: n = 2, requests = [[0,1],[1,0],[1,0],[0,1]]

Output: 4

Explanation:

Two employees swap buildings twice. All four requests can be satisfied.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result