- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find maximum possible stolen value from houses in C++

In this problem, we are given n houses with some values in them. Our task is to Find the maximum possible stolen value from houses.

**Problem Description** − We have an array houses[] that consist of the values that are in each house. A thief robs the houses but he cannot steal from two adjacent houses as neighbours know about the theft. We need to find the maximum possible value that can be stolen by the thief from the house without getting caught.

**Let’s take an example to understand the problem,**

## Input

houses[] = {5, 2, 1, 6, 7, 9, 4, 3}

## Output

23

## Explanation

The max values can be stolen as : 5, 6, 9, 3.

## Solution Approach

The solution of the problem can be found using dynamic programming. As we need to find the maximum stolen value by the thief such that if he steals from a house at index i, then he cannot steal from the house at index (i+1). Also, we would not have stolen from the house at index (i-1). To solve the problem, we will create an DP array of size n. And for base case we will initialize the DP[0] with houses[0] and DP[1] with houses[1]. Then, we will find all the values of DP from index 2 to n-1. The value of DP[i] will be the maximum value out of DP[i-2] + houses[i] or DP[i-1]. And at the end the last value of the DP array is the maximum value that can be stolen.

**Program to illustrate the working of our solution,**

## Example

#include <iostream> using namespace std; int calMax(int a, int b){ if(a > b) return a; return b; } int findMaxValuesStolen(int houses[], int n) { if (n == 0) return 0; int DP[n]; DP[0] = houses[0]; DP[1] = calMax(houses[0], houses[1]); for (int i = 2; i<n; i++) DP[i] = calMax( (houses[i] + DP[i-2]), DP[i-1]); return DP[n-1]; } int main() { int houses[] = {5, 2, 1, 6, 7, 9, 4, 3}; int n = sizeof(houses)/sizeof(houses[0]); cout<<"The maximum possible values stolen from the houses is "<<findMaxValuesStolen(houses, n); return 0; }

## Output

The maximum possible values stolen from the houses is 23

This solution is good but it can be made more efficient using the fact that the maximum values stolen can be found using only two values. As in the DP, we have used only two values for each index. So, we can use only two variable to find the solution which is decrease the space complexity to the problem,

**Program to illustrate the working of our solution,**

## Example

#include <iostream> using namespace std; int calMax(int a, int b){ if(a > b) return a; return b; } int findMaxValuesStolen(int houses[], int n) { if (n == 0) return 0; int maxValStolen; int val1 = houses[0]; int val2 = calMax(houses[0], houses[1]); for (int i = 2; i<n; i++) { maxValStolen = calMax( (houses[i]+val1) , val2); val1 = val2; val2 = maxValStolen; } return maxValStolen; } int main() { int houses[] = {5, 2, 1, 6, 7, 9, 4, 3}; int n = sizeof(houses)/sizeof(houses[0]); cout<<"The maximum possible values stolen from the houses is "<<findMaxValuesStolen(houses, n); return 0; }

## Output

The maximum possible values stolen from the houses is 23

- Related Questions & Answers
- C++ program to find minimum possible number of keyboards are stolen
- C++ program to find maximum possible value of XORed sum
- C++ program to find maximum possible value for which XORed sum is maximum
- Program to find maximum possible value of smallest group in Python
- Find the maximum possible value of the minimum value of modified array in C++
- Program to find minimum possible maximum value after k operations in python
- Find maximum value from a VARCHAR column in MySQL
- C++ program to find out the maximum possible tally from given integers
- Program to find the maximum score from all possible valid paths in Python
- Program to find maximum possible value of an expression using given set of numbers in Python
- What is the maximum possible value of an integer in C# ?
- What is the maximum possible value of an integer in Python?
- What is the maximum possible value of an integer in Java ?
- Program to find minimum cost for painting houses in Python
- Return maximum value from records in MySQL