House Robber Problem Statement

Consider Mr. X, a professional robber who is planning to rob houses along a street. These houses are arranged in a circle, which means the first house is a neighbor to the last. Each house contains a certain amount of money. Adjacent houses have a connected security system, so breaking into two consecutive houses in the same night is not possible.

Your task is to determine the maximum amount of money Mr. X can rob tonight without triggering the security system.

Input:

The first line of input contains an integer 'T' indicating the number of test cases. 
Each test case begins with an integer 'N', the count of houses.
The following line holds 'N' space-separated integers representing the money available in each house.

Output:

For each test case, output a single integer denoting the maximum money that Mr. X can rob, printed on a separate line for each test case.

Example:

(i) Input: arr[] = {2, 3, 2} | Output: 3 
Explanation: Mr. X robs house 2 (money = 3) because robbing house 1 (money = 2) and house 3 (money = 2) is not possible as they are adjacent.
(ii) Input: arr[] = {1, 2, 3, 1} | Output: 4
Explanation: Mr. X robs house 1 (money = 1) and house 3 (money = 3).
(iii) Input: arr[] = {0} | Output: 0
Explanation: Since there's no money in the only house, the maximum amount robbed is 0.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 5 x 10 ^ 3
  • 1 <= ARR[i] <= 10 ^ 9

Note: Your implementation should only calculate the maximum robable money; printing has been handled.

AnswerBot
4mo

House Robber problem where adjacent houses have connected security system, find maximum money Mr. X can rob without triggering security.

  • Use dynamic programming to keep track of maximum money robbed at...read more

Help your peers!
Select
Add answer anonymously...

Lowe's Software Developer interview questions & answers

A Software Developer was asked 6mo agoQ. Given a 1D array, remove the duplicate elements in-place.
A Software Developer was asked 8mo agoQ. How can we create a custom immutable class?
A Software Developer was asked 8mo agoQ. Why are strings immutable?

Popular interview questions of Software Developer

A Software Developer was asked 8mo agoQ1. How can we create a custom immutable class?
A Software Developer was asked 8mo agoQ2. Why are strings immutable?
A Software Developer was asked Q3. How does WebFlux work?
Lowe's Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits