Asked inUber,SDE-2

House Robber Problem Statement

Mr. X is a professional robber with a plan to rob houses arranged in a circular street. Each house has a certain amount of money hidden, separated by a security system that alerts the police if two adjacent houses are robbed on the same night. Your task is to determine the maximum amount of money Mr. X can rob without triggering the alarm.

Explanation:

You are given an array/list of non-negative integers, ARR, representing the amount of money in each house. Find the maximum amount Mr. X can rob without alarming the police, considering the circular arrangement of houses.

Input:

The first line contains an integer 'T', representing the number of test cases. Each test case begins with an integer 'N', the size of ARR, followed by N space-separated integers indicating the amounts of money in respective houses.

Output:

For each test case, print a single integer representing the maximum money that can be robbed, each result on a new line.

Example:

(i) For arr[] = {2, 3, 2}, the output is 3. Mr. X cannot rob houses 1 and 3 because they are adjacent, so he robs house 2.
(ii) For arr[] = {1, 2, 3, 1}, the output is 4. Mr. X robs house 1 and 3.
(iii) For arr[] = {0}, the output is 0. There is nothing to rob.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 5 x 10^3
  • 1 <= ARR[i] <= 10^9
  • Time limit: 1 sec
Note:
If multiple sets of houses yield the same maximum rob amount, any set is valid. You do not need to handle printing; just implement the function.
Be the first one to answer
Add answer anonymously...
Uber SDE-2 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