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.

Be the first one to answer
Add answer anonymously...
Lowe's Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter