Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief knows which house has what amount of money but is unable to come up with an optimal looting strategy. He asks for your help to find the maximum money he can get if he strictly follows the rule. ith house has hval[i] amount of money present in it.
Examples:
Input: hval[] = {5, 5, 10, 100, 10, 5}
Output: 110
Explanation: Pick the subsequence {5, 100, 5}.
The sum is 110 and no two elements are adjacent. This is the highest possible sum.
Input: hval[] = {3, 2, 7, 10}
Output: 13
Explanation: The subsequence is {3, 10}. This gives sum = 13.
This is the highest possible sum of a subsequence following the given criteria
Input: hval[] = {3, 2, 5, 10, 7}
Output: 15
Explanation: Pick the subsequence {3, 5, 7}. The sum is 15.
[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
Stickler Thief Editorial
Stickler the thief wants to loot money from a society having n houses in a single line. He is a weird person and follows a certain rule when looting the houses. According to the rule, he will never loot two consecutive houses. At the same time, he wants to maximize the amount he loots. The thief kno
15+ min read
Policemen catch thieves
Given an array of size n that has the following specifications: Each element in the array contains either a policeman or a thief.Each policeman can catch only one thief.A policeman cannot catch a thief who is more than K units away from the policeman. We need to find the maximum number of thieves th
15+ min read
GeeksforGeeks Problem Of The Day (POTD) Solutions | Answer Key, Videos & Editorials
Are you finding it difficult to develop a habit of coding? GeeksforGeeks is providing some extra incentive to keep your motivation levels always up - The POTD (Problem Of The Day). Become a more consistent coder by solving one question every day in the GeeksforGeeks Problem Of The Day (POTD) event a
3 min read
Design custom Twitter based on given operations
Design a simplified version of Twitter where users can post tweets, follow/unfollow another user, and see the 10 most recent tweets in their news feed. Our design should support the following methods: postTweet (userId, tweetId): Compose a new tweet.getNewsFeed (userId): Retrieve the 10 most recent
11 min read
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
Probability of collision between two trucks
Given two strings S and T, where S represents the first lane in which vehicles move from left to right and T represents the second lane in which vehicles move from right to left. Vehicles can be either B (bike), C (car), or T (truck). The task is to find the probability of collision between two truc
9 min read
Capgemini Tech Challenge 2019 | Round2 & Round 3 Question
Round 2: MISSION WITH MI6: Last year a hacking took place in the safest and crucial defense network of the US. Hackers downloaded crucial defense-related confidential files and documents from their network and after that, they flew to Dubai for delivery of the documents to some terror-related outfit
7 min read
Trap the King by one of the given Set of pieces
Given an 8 X 8 matrix of characters '*' and 'K' as a chessboard in which K represents the position of the King and the rest of the cell is represented by *. Also given the following three sets of pieces: Set 1: 1 QueenSet 2: 1 Queen, 1 RookSet 3: 1 Queen, 1 Rook, 1 Bishop Then you have to trap the K
15+ min read
Amazon Interview | Set 32
I would like to contribute for GeeksForGeeks by sharing my experience of Amazon Interview process. This was for a SDE position in Hyderabad. I have almost 2 years of work experience in Samsung. 1st Round: Written Test was on Interview Street Platform. Qs-1) A function printMostFrequentWords, which t
6 min read
POTD Solutions | 15 Novâ 23 | Better String
View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Dynamic Programming but will also help you build up pro
6 min read