Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

House Robber

Moderate
0/80
Average time to solve is 26m
profile
Contributed by
66 upvotes
Asked in companies
AmazonQuikrOYO

Problem statement

A thief wants to loot houses. He knows the amount of money in each house. He cannot loot two consecutive houses. Find the maximum amount of money he can loot.

Detailed explanation ( Input/output format, Notes, Images )
Constraints :
0 <= 'N' <= 10^5
0 <= 'A[i]' <= 10^4

Where 'A[i]' represents the money present in the 'i-th' house.

Time limit: 1 sec
Sample Input 1:
4
10 2 3 11
Sample Output 1:
21
Explanation of Sample Input 1:
Since the thief cant loot two consecutive houses, the ways in which he may loot are:

1. [10, 3]: a total loot of 13
2. [10, 11]: a total loot of 21
3. [2, 11]: a total loot of 13
4. [10]: a total loot of 10
5. [2]: a total loot of 2
6. [3]: a total loot of 3
7. [11]: a total loot of 11

We can't neglect the option to loot just either of the houses if it yields the maximum loot.

From all the possible seven ways, the second option yields the maximum loot amount and hence the answer.
Sample Input 2:
6
5 5 10 100 10 5
Sample Output 2 :
110
Full screen
Console