House Robber – Maximum possible stolen value
Last Updated :
01 Oct, 2024
There are n houses built in a line, each of which contains some money in it. A robber wants to steal money from these houses, but he can’t steal from two adjacent houses. The task is to find the maximum amount of money which can be stolen.
Examples:
Input: hval[] = {6, 7, 1, 3, 8, 2, 4}
Output: 19
Explanation: The thief will steal from house 1, 3, 5 and 7, total money = 6 + 1 + 8 + 4 = 19.
Input: hval[] = {5, 3, 4, 11, 2}
Output: 16
Explanation: Thief will steal from house 1 and 4, total money = 5 + 11 = 16.
Given an array, the solution is to find the maximum sum subsequence where no two selected elements are adjacent.
[Naive Approach] Using Recursion- O(2n) Time and O(n) Space
The idea is to explore all the possibilities for each house using Recursion. We can start from the last house and for each house, we have two choices:
- Rob the current house and skip the house just before it.
- Skip the current house and move to the next house.
So, the recurrence relation will be:
maxLootRec(n) = max(hval[n – 1] + maxLootRec(n – 2), maxLootRec(n – 1)),
where maxLootRec(n) returns the maximum amount of money which can be stolen if n houses are left.
C++
// C++ Program to solve House Robber Problem using Recursion
#include <iostream>
#include <vector>
using namespace std;
// Calculate the maximum stolen value recursively
int maxLootRec(vector<int> &hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + maxLootRec(hval, n - 2);
int notPick = maxLootRec(hval, n - 1);
// Return the max of two choices
return max(pick, notPick);
}
// Function to calculate the maximum stolen value
int maxLoot(vector<int>& hval) {
int n = hval.size();
// Call the recursive function for n houses
return maxLootRec(hval, n);
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval);
return 0;
}
C
// C Program to solve House Robber Problem using Recursion
#include <stdio.h>
// Function to calculate the maximum stolen value
int maxLoot(int *hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + maxLoot(hval, n - 2);
int notPick = maxLoot(hval, n - 1);
// Return the max of two choices
return (pick > notPick) ? pick : notPick;
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using Recursion
class GfG {
// Calculate the maximum stolen value recursively
static int maxLootRec(int[] hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + maxLootRec(hval, n - 2);
int notPick = maxLootRec(hval, n - 1);
// Return the max of two choices
return Math.max(pick, notPick);
}
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.length;
// Call the recursive function for n houses
return maxLootRec(hval, n);
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using Recursion
# Calculate the maximum stolen value recursively
def maxLootRec(hval, n):
# If no houses are left, return 0.
if n <= 0:
return 0
# If only 1 house is left, rob it.
if n == 1:
return hval[0]
# Two Choices: Rob the nth house and do not rob the nth house
pick = hval[n - 1] + maxLootRec(hval, n - 2)
notPick = maxLootRec(hval, n - 1)
# Return the max of two choices
return max(pick, notPick)
# Function to calculate the maximum stolen value
def maxLoot(hval):
n = len(hval)
# Call the recursive function for n houses
return maxLootRec(hval, n)
if __name__ == "__main__":
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using Recursion
using System;
class GfG {
// Calculate the maximum stolen value recursively
static int MaxLootRec(int[] hval, int n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n == 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
int pick = hval[n - 1] + MaxLootRec(hval, n - 2);
int notPick = MaxLootRec(hval, n - 1);
// Return the max of two choices
return Math.Max(pick, notPick);
}
// Function to calculate the maximum stolen value
static int MaxLoot(int[] hval) {
int n = hval.Length;
// Call the recursive function for n houses
return MaxLootRec(hval, n);
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(MaxLoot(hval));
}
}
JavaScript
// JavaScript Program to solve House Robber Problem using Recursion
// Calculate the maximum stolen value recursively
function maxLootRec(hval, n) {
// If no houses are left, return 0.
if (n <= 0) return 0;
// If only 1 house is left, rob it.
if (n === 1) return hval[0];
// Two Choices: Rob the nth house and do not rob the nth house
let pick = hval[n - 1] + maxLootRec(hval, n - 2);
let notPick = maxLootRec(hval, n - 1);
// Return the max of two choices
return Math.max(pick, notPick);
}
// Function to calculate the maximum stolen value
function maxLoot(hval) {
let n = hval.length;
// Call the recursive function for n houses
return maxLootRec(hval, n);
}
let hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(2n). Every house has 2 choices to pick and not pick.
Auxiliary Space: O(n). For recursion stack space
[Better Approach] Using Memoization – O(n) Time and O(n) Space
The above solution has optimal substructure and overlapping subproblems. See the below recursion tree, maxLootRec(2) is being evaluated twice.

Recursion Tree for House Robber
We can optimize this solution using a memo array of size (n + 1), such that memo[i] represents the maximum value that can be collected from first i houses. Please note that there is only one parameter that changes in recursion and the range of this parameter is from 0 to n.
C++
// C++ Program to solve House Robber Problem using Memoization
#include <iostream>
#include <vector>
using namespace std;
int maxLootRec(const vector<int>& hval, int n, vector<int>& memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
int notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = max(pick, notPick);
return memo[n];
}
int maxLoot(vector<int>& hval) {
int n = hval.size();
// Initialize memo array with -1
vector<int> memo(n + 1, -1);
return maxLootRec(hval, n, memo);
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval);
return 0;
}
C
// C Program to solve House Robber Problem using Memoization
#include <stdio.h>
#include <stdlib.h>
int maxLootRec(const int* hval, int n, int* memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
int notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = (pick > notPick) ? pick : notPick;
return memo[n];
}
int maxLoot(int* hval, int n) {
// Initialize memo array with -1
int memo[n + 1];
for (int i = 0; i <= n; ++i) {
memo[i] = -1;
}
int result = maxLootRec(hval, n, memo);
return result;
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using Memoization
import java.util.Arrays;
class GfG {
static int maxLootRec(int[] hval, int n, int[] memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
int notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = Math.max(pick, notPick);
return memo[n];
}
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.length;
// Initialize memo array with -1
int[] memo = new int[n + 1];
Arrays.fill(memo, -1);
return maxLootRec(hval, n, memo);
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using Memoization
def maxLootRec(hval, n, memo):
if n <= 0:
return 0
if n == 1:
return hval[0]
# Check if the result is already computed
if memo[n] != -1:
return memo[n]
pick = hval[n - 1] + maxLootRec(hval, n - 2, memo)
notPick = maxLootRec(hval, n - 1, memo)
# Store the max of two choices in the memo array and return it
memo[n] = max(pick, notPick)
return memo[n]
def maxLoot(hval):
n = len(hval)
# Initialize memo array with -1
memo = [-1] * (n + 1)
return maxLootRec(hval, n, memo)
if __name__ == "__main__":
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using Memoization
using System;
class GfG {
static int MaxLootRec(int[] hval, int n, int[] memo) {
if (n <= 0) return 0;
if (n == 1) return hval[0];
// Check if the result is already computed
if (memo[n] != -1) return memo[n];
int pick = hval[n - 1] + MaxLootRec(hval, n - 2, memo);
int notPick = MaxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = Math.Max(pick, notPick);
return memo[n];
}
// Function to calculate the maximum stolen value
static int MaxLoot(int[] hval) {
int n = hval.Length;
// Initialize memo array with -1
int[] memo = new int[n + 1];
for (int i = 0; i <= n; i++) {
memo[i] = -1;
}
return MaxLootRec(hval, n, memo);
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(MaxLoot(hval));
}
}
JavaScript
// JavaScript Program to solve House Robber Problem using Memoization
function maxLootRec(hval, n, memo) {
if (n <= 0) return 0;
if (n === 1) return hval[0];
// Check if the result is already computed
if (memo[n] !== -1) return memo[n];
const pick = hval[n - 1] + maxLootRec(hval, n - 2, memo);
const notPick = maxLootRec(hval, n - 1, memo);
// Store the max of two choices in the memo array and return it
memo[n] = Math.max(pick, notPick);
return memo[n];
}
// Function to calculate the maximum stolen value
function maxLoot(hval) {
const n = hval.length;
// Initialize memo array with -1
const memo = new Array(n + 1).fill(-1);
return maxLootRec(hval, n, memo);
}
const hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(n). Every house is computed only once.
Auxiliary Space: O(n). For recursion stack space and memo array.
[Expected Approach 1] Using Tabulation – O(n) Time and O(n) Space
The idea is to build the solution in bottom-up manner. We create a dp[] array of size n+1 where dp[i] represents the maximum value that can be collected with first i houses. We first fill the known values, dp[0] and dp[1] and then fill the remaining values using the formula: dp[i] = max(hval[i] + dp[i – 2], dp[i – 1]). The final result will be stored at dp[n].
C++
// C++ Program to solve House Robber Problem using Tabulation
#include <iostream>
#include <vector>
using namespace std;
// Function to calculate the maximum stolen value using bottom-up DP
int maxLoot(vector<int>& hval) {
int n = hval.size();
// Create a dp array to store the maximum loot at each house
vector<int> dp(n+1, 0);
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (int i = 2; i <= n; i++)
dp[i] = max(hval[i - 1] + dp[i - 2], dp[i - 1]);
return dp[n];
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval) << endl;
return 0;
}
C
// C Program to solve House Robber Problem using Tabulation
#include <stdio.h>
int max(int a, int b) {return (a > b) ? a : b;}
int maxLoot(int* hval, int n) {
// Create a dp array to store the
// maximum loot at each house
int dp[n+1];
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the
// bottom-up approach
for (int i = 2; i <= n; i++)
dp[i] = max(hval[i - 1] + dp[i - 2], dp[i - 1]);
return dp[n];
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using Tabulation
class GfG {
// Function to calculate the maximum stolen value using bottom-up DP
static int maxLoot(int[] hval) {
int n = hval.length;
// Create a dp array to store the maximum loot at each house
int[] dp = new int[n + 1];
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (int i = 2; i <= n; i++) {
dp[i] = Math.max(hval[i - 1] + dp[i - 2], dp[i - 1]);
}
return dp[n];
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using Tabulation
def maxLoot(hval):
n = len(hval)
# Create a dp array to store the maximum loot at each house
dp = [0] * (n + 1)
# Base cases
dp[0] = 0
dp[1] = hval[0]
# Fill the dp array using the bottom-up approach
for i in range(2, n + 1):
dp[i] = max(hval[i - 1] + dp[i - 2], dp[i - 1])
return dp[n]
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using Tabulation
using System;
class GfG {
// Function to calculate the maximum stolen value using bottom-up DP
static int MaxLoot(int[] hval) {
int n = hval.Length;
// Create a dp array to store the maximum loot at each house
int[] dp = new int[n + 1];
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (int i = 2; i <= n; i++) {
dp[i] = Math.Max(hval[i - 1] + dp[i - 2], dp[i - 1]);
}
return dp[n];
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(MaxLoot(hval));
}
}
JavaScript
// JavaScript Program to solve House Robber Problem using Tabulation
function maxLoot(hval) {
const n = hval.length;
// Create a dp array to store the maximum loot at each house
const dp = new Array(n + 1).fill(0);
// Base cases
dp[0] = 0;
dp[1] = hval[0];
// Fill the dp array using the bottom-up approach
for (let i = 2; i <= n; i++)
dp[i] = Math.max(hval[i - 1] + dp[i - 2], dp[i - 1]);
return dp[n];
}
const hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(n), Every house is computed only once.
Auxiliary Space O(n), We are using a dp array of size n.
[Expected Approach 2] Space-Optimized DP – O(n) Time and O(1) Space
On observing the dp[] array in the previous approach, it can be seen that the answer at the current index depends only on the last two values. In other words, dp[i] depends only on dp[i – 1] and dp[i – 2]. So, instead of storing the result in an array, we can simply use two variables to store the last and second last result.
C++
// C++ Program to solve House Robber Problem using
// Space Optimized Tabulation
#include <iostream>
#include <vector>
using namespace std;
// Function to calculate the maximum stolen value
int maxLoot(vector<int> &hval) {
int n = hval.size();
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous two values
// The final current value would be our result
int res;
for (int i = 1; i < n; i++) {
res = max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
int main() {
vector<int> hval = {6, 7, 1, 3, 8, 2, 4};
cout << maxLoot(hval) << endl;
return 0;
}
C
// C Program to solve House Robber Problem using
// Space Optimized Tabulation
#include <stdio.h>
int max(int a, int b) { return (a > b) ? a : b; }
// Function to calculate the maximum stolen value
int maxLoot(int hval[], int n) {
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous
// two values. The final current value
// would be our result
int res;
for (int i = 1; i < n; i++) {
res = max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
int main() {
int hval[] = {6, 7, 1, 3, 8, 2, 4};
int n = sizeof(hval) / sizeof(hval[0]);
printf("%d\n", maxLoot(hval, n));
return 0;
}
Java
// Java Program to solve House Robber Problem using
// Space Optimized Tabulation
import java.util.Arrays;
class GfG {
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.length;
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous
// two values. The final current value
// would be our result
int res = 0;
for (int i = 1; i < n; i++) {
res = Math.max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
public static void main(String[] args) {
int[] hval = {6, 7, 1, 3, 8, 2, 4};
System.out.println(maxLoot(hval));
}
}
Python
# Python Program to solve House Robber Problem using
# Space Optimized Tabulation
# Function to calculate the maximum stolen value
def maxLoot(hval):
n = len(hval)
if n == 0:
return 0
if n == 1:
return hval[0]
# Set previous 2 values
secondLast = 0
last = hval[0]
# Compute current value using previous two values
# The final current value would be our result
res = 0
for i in range(1, n):
res = max(hval[i] + secondLast, last)
secondLast = last
last = res
return res
hval = [6, 7, 1, 3, 8, 2, 4]
print(maxLoot(hval))
C#
// C# Program to solve House Robber Problem using
// Space Optimized Tabulation
using System;
class GfG {
// Function to calculate the maximum stolen value
static int maxLoot(int[] hval) {
int n = hval.Length;
if (n == 0)
return 0;
if (n == 1)
return hval[0];
// Set previous 2 values
int secondLast = 0, last = hval[0];
// Compute current value using previous two values
// The final current value would be our result
int res = 0;
for (int i = 1; i < n; i++) {
res = Math.Max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
static void Main() {
int[] hval = { 6, 7, 1, 3, 8, 2, 4 };
Console.WriteLine(maxLoot(hval));
}
}
JavaScript
// Function to calculate the maximum stolen value
function maxLoot(hval) {
const n = hval.length;
if (n === 0)
return 0;
if (n === 1)
return hval[0];
// Set previous 2 values
let secondLast = 0, last = hval[0];
// Compute current value using previous two values
// The final current value would be our result
let res;
for (let i = 1; i < n; i++) {
res = Math.max(hval[i] + secondLast, last);
secondLast = last;
last = res;
}
return res;
}
const hval = [6, 7, 1, 3, 8, 2, 4];
console.log(maxLoot(hval));
Time Complexity: O(n), Every value is computed only once.
Auxiliary Space: O(1), as we are using only two variables.
Similar Reads
House Robber - Maximum possible stolen value
There are n houses built in a line, each of which contains some money in it. A robber wants to steal money from these houses, but he canât steal from two adjacent houses. The task is to find the maximum amount of money which can be stolen. Examples: Input: hval[] = {6, 7, 1, 3, 8, 2, 4}Output: 19Exp
15+ min read
Find the maximum possible value of f(B)
Given a tree rooted at node 1. The tree is given in the form of an array P where Pi denotes the parent of node i, also P1 = -1, as node 1 is the root node. Every node i has a value Ai associated with it. At first, you have to choose any node to start with, after that from a node you can go to any of
11 min read
Find the maximum possible value for the given periodic function
Given three numbers A, B, and N, the task is to find the maximum possible value of floor(A * x / B) - A * floor(x / b) where x is a non-negative integer less than or equal to N. Here floor(T) = denotes the greatest integer not greater than the real number T (G.I.F function). Constraints: 1 ? A ? 106
10 min read
Best order to maximise the value
Given, an array arr[] of N integers, the task is to select some integers from this array arr[] and arrange them in another array brr[], such that the sum of brr[i]*(i+1) for every index in array brr is maximum. Find out the maximum possible such value for a given array arr. Input: N = 5, arr[] = {-1
6 min read
Maximizing Robbery in Binary Tree Houses without Alerting Police
There are Houses arranged in the form of a Binary tree. There is only one entrance to the houses which is the root of a tree. It will automatically contact the police if two directly linked houses are broken into on the same night. Given the root of the binary tree, return the maximum amount of mone
11 min read
Maximum profit such that total stolen value is less than K to get bonus
Given an integer K and an array arr[] which denotes the amount that can be stolen, the task is to choose a subset of items such that their total value is less than K to get the bonus amount. Bonus Amount: The bonus amount will be the maximum value that can be stolen from the set of items for each it
7 min read
Maximum profit from sale of wines
Given n wines in a row, with integers denoting the cost of each wine respectively. Each year you can sale the first or the last wine in a row. However, the price of wines increases over time. Let the initial profits from the wines be P1, P2, P3...Pn. In the Yth year, the profit from the ith wine wil
15+ min read
Maximize the value of the given expression
Given three non-zero integers a, b and c. The task is to find the maximum value possible by putting addition and multiplication signs between them in any order. Note: Rearrangement of integers is allowed but addition and multiplication sign must be used once. Braces can also be placed between equati
8 min read
Minimum Amount to Paint the Walls
Given two 0-indexed integer arrays, cost[] and time[] of size n representing the costs and the time taken to paint n different walls respectively. There are two painters available: A paid painter that paints the ith wall in time[i] units of time and takes cost[i] units of money. A free painter that
7 min read
Maximum possible cost of buildings with pool view
Consider a series of buildings in a 2-D square plot with different heights and only those buildings are chargeable that have a clear and straight view of the pool from their building. The pool also spans a 2-D square plot with certain coordinates. Given the Coordinates of a square pool, the number o
10 min read