- 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

# Program to check robbers can rob the vault or not in Python

Suppose there are N number of robbers are trying to rob a vault. There was a guard but he went out for G amount of time, after that he will come back. And each robber has specific time to rob the vault, but at most two of them can enter into the vault at the same time. Now the problem is we have to check whether they can rob the vault of getting caught by the guard? We have to keep in mind that −

If one robber goes inside the vault at a time t and at the same time another robber comes out, then it is like they were never in the vault at the same time.

If the guard gets inside vault at time G and a robber comes out exactly at time G, the guard will not notice the robber.

So, if the input is like N = 3 G = 5 time = [3,5,2], then the output will be True, because possible arrangement is there, that is −

- at time t=0, robber1 goes inside and comes out at t=3
- at time t=0, robber2 goes inside and comes out at t=5
- at time t=3, robber3 goes inside and comes out at t=5

To solve this, we will follow these steps −

- if sum of all elements in time > 2*G, then
- return False

- otherwise when sum of all elements in time <= G, then
- return True

- otherwise,
- valid := an array of size G + 1, and initially all values are False
- valid[0] := True
- for each x in time, do
- for i in range G to 0, decrease by 1, do
- if i-x >= 0 and valid[i-x], then
- valid[i] := True

- if i-x >= 0 and valid[i-x], then

- for i in range G to 0, decrease by 1, do
- if sum of all elements in time - maximum of i for all i in range 0 to size of valid when valid[i] <= G, then
- return True

- otherwise,
- return False

## Example

Let us see the following implementation to get better understanding −

def solve(N, G, time): if sum(time) > 2*G: return False elif sum(time) <= G: return True else: valid = [False]*(G+1) valid[0] = True for x in time: for i in range(G,-1,-1): if i-x >= 0 and valid[i-x]: valid[i] = True if sum(time) - max(i for i in range(len(valid)) if valid[i]) <= G: return True else: return False N = 3 G = 5 time = [3,5,2] print(solve(N, G, time))

## Input

3,5,[3,5,2]

## Output

True

- Related Questions & Answers
- Program to check we can reach leftmost or rightmost position or not in Python
- Program to check robot can reach target position or not in Python
- Python program to check whether we can pile up cubes or not
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check we can cross river by stones or not in Python
- Program to check we can form array from pieces or not in Python
- Program to check whether Amal can win stone game or not in Python
- Program to check whether all can get a seat or not in Python
- Python program to check if the string is empty or not
- Program to check whether we can get N queens solution or not in Python
- Program to check subarrays can be rearranged from arithmetic sequence or not in Python
- Program to check two rectangular overlaps or not in Python
- Program to check the string is repeating string or not in Python
- Program to check whether one point can be converted to another or not in Python