- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose, there are n number of hostel rooms numbered from 0 to n-1. The students in the hostel rooms want to transfer to another room, and they place several requests to do that. No hostel seat remains vacant, a transfer request is only taken care of if another student takes the place of the student willing to transfer. So, given the requests, we have to find out how many requests can be satisfied.

So, if the input is like n = 3, requests = [[0,2],[1,0],[2,1]], then the output will be 3.

The student in room 0 transfers to room 2.

The student in room 1 transfers to room 0.

The student in room 2 transfers to room 1.

To solve this, we will follow these steps −

for k in range size of requests to -1, decrease by 1, do

for c in all combinations of (0 to size of requests and k), do

d := a new array of size n containing value 0

for each i in c, do

d[requests[i, 0]] := d[requests[i, 0]] - 1

d[requests[i, 1]] := d[requests[i, 1]] + 1

if none of the items in d are true, then

return k

return 0

Let us see the following implementation to get better understanding

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 d[requests[i][1]] += 1 if not any(d): return k return 0 print(solve(3, [[0,2],[1,0],[2,1]]))

3, [[0,2],[1,0],[2,1]]

3

- Related Questions & Answers
- Program to find out how many boxes can be put into the godown in Python
- Python Program to find out how many cubes are cut
- Program to find out number of blocks that can be covered in Python
- How can transfer learning be implemented in Python using Keras?
- Program to find how many ways we can climb stairs in Python
- Program to find maximum how many water bottles we can drink in Python
- Python Program to find out how many times the balls will collide in a circular tube
- Program to find how many lines intersect in Python
- Python Program to find out the number of rooms in which a prize can be hidden
- C++ Program to find out how many movies an attendee can watch entirely at a Film festival
- How can you have a satisfied life?
- Program to find how many total amount of rain we can catch in Python
- Can I find out the next auto_increment to be used?
- Problem to Find Out the Maximum Number of Coins that Can be Collected in Python
- Program to Find Out Currency Arbitrage in Python

Advertisements