Program to find out how many transfer requests can be satisfied in Python

Suppose there are n hostel rooms numbered from 0 to n-1. Students want to transfer to different rooms and place transfer requests. Since no room can remain vacant, a transfer is only satisfied if another student takes the transferring student's place. We need to find the maximum number of requests that can be satisfied simultaneously.

Problem Understanding

For a set of transfer requests to be valid, the number of students leaving each room must equal the number of students entering that room. This creates a balanced system where no room becomes empty or overcrowded.

Example

If we have n = 3 rooms and requests = [[0,2],[1,0],[2,1]], all 3 requests can be satisfied ?

  • Student in room 0 transfers to room 2

  • Student in room 1 transfers to room 0

  • Student in room 2 transfers to room 1

This forms a perfect cycle where each room loses one student and gains one student.

Algorithm

We use a greedy approach with backtracking ?

  • Try all possible combinations starting from the maximum number of requests

  • For each combination, track the net change for each room (outgoing - incoming)

  • If all rooms have zero net change, the combination is valid

  • Return the size of the first valid combination found

Implementation

from itertools import combinations

def solve(n, requests):
    for k in range(len(requests), 0, -1):
        for c in combinations(range(len(requests)), k):
            d = [0] * n
            for i in c:
                d[requests[i][0]] -= 1  # Student leaves this room
                d[requests[i][1]] += 1  # Student enters this room
            if not any(d):  # All rooms have net change of 0
                return k
    return 0

# Test the function
n = 3
requests = [[0,2],[1,0],[2,1]]
result = solve(n, requests)
print(f"Maximum requests that can be satisfied: {result}")
Maximum requests that can be satisfied: 3

How It Works

The algorithm creates a balance array d where each index represents a room. For each request [from, to], we decrement d[from] and increment d[to]. If all values in d are zero, it means every room has equal incoming and outgoing transfers.

Additional Example

# Example with partial satisfaction
n = 4
requests = [[0,1],[1,2],[2,0],[3,1]]
result = solve(n, requests)
print(f"Maximum requests: {result}")

# Let's trace through the solution
print("\nTracing the solution:")
for k in range(len(requests), 0, -1):
    print(f"Trying {k} requests...")
    for c in combinations(range(len(requests)), k):
        d = [0] * n
        for i in c:
            d[requests[i][0]] -= 1
            d[requests[i][1]] += 1
        print(f"  Combination {c}: balance = {d}")
        if not any(d):
            print(f"  Valid! Can satisfy {k} requests.")
            break
    else:
        continue
    break
Maximum requests: 3
  
Tracing the solution:
Trying 4 requests...
  Combination (0, 1, 2, 3): balance = [-1, 1, -1, 1]
Trying 3 requests...
  Combination (0, 1, 2): balance = [0, 0, 0, 0]
  Valid! Can satisfy 3 requests.

Conclusion

The solution uses combinations to find the maximum set of transfer requests that maintain room balance. The key insight is that valid transfers must form cycles or chains where every room's outgoing students equal incoming students.

---
Updated on: 2026-03-26T14:27:57+05:30

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements