# C++ Program for Houses Problem (TCS Codevita) | PrepInsta ## Problem Description

Question:- There are n houses build in a line, each of which contains some value in it.

A thief is going to steal the maximal value of these houses, but he can’t steal in two adjacent houses because the owner of the stolen houses will tell his two neighbours left and right side.

What is the maximum stolen value?

Sample Input: val[] = {6, 7, 1, 3, 8, 2, 5}

Sample Output: 20

## C++ Code

```#include  <iostream>
using namespace std;
int maxLoot(int *hval, int n) ;
int main()
{
int hval[]= {6, 7, 1, 3, 8, 2, 4, 12};
int n = sizeof(hval)/sizeof(hval);
cout << "Maximum loot possible : "
<< maxLoot(hval, n);
return 0;
}
// calculate the maximum stolen value
int maxLoot(int *hval, int n)
{
if (n == 0)
return 0;
if (n == 1)
return hval;
if (n == 2)
return max(hval, hval);

// dp[i] represent the maximum value stolen
// so far after reaching house i.
int dp[n];

// Initialize the dp and dp
dp = hval;
dp = max(hval, hval);

// Fill remaining positions
for (int i = 2; i<n; i++)
dp[i] = max(hval[i]+dp[i-2], dp[i-1]);

return dp[n-1];
}```
```Output
6 7 1 3 8 2 5
20```

## JAVA

To find the solution of To find the solution of Houses problem in JAVA Programming language click on the button below:

JAVA

## C

Sorry, we don’t the solution in C You can provide the solution for this problem in C, below in the comments sections.

## Python

To find the solution of Houses problem in Python Programming language click on the button below:

### 6 comments on “C++ Program for Houses Problem (TCS Codevita) | PrepInsta”

• sahit

#include
using namespace std;
#define fo(n) for(int i=0;i<n;i++)
#define Fo(k,n) for(int i=k;k<n?in;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
typedef pair pii;
typedef pair pl;
typedef vector vi;
typedef vector vl;
typedef vector vpii;
typedef vector vpl;
typedef vector vvi;
typedef vector vvl;
const int mod = 1’000’000’007;
const int N = 3e5, M = N;
//=======================

void solve() {
int n;
si(n);
vi a(n);
fo(n)si(a[i]);
int incl , excl ,pincl;
incl = a;
pincl=incl;
excl=0;
Fo(1,n){
incl= excl+a[i];
excl= max(excl,pincl);
pincl=incl;
}
cout<> t;
while(t–) {
solve();
}

return 0;
} 0
• Briram

I have a solution of this problem in C.
#include
#include

int main()
{
int s[]= {6, 7, 1, 3, 8, 2, 5};
int sum1=0,sum2=0;
for(int i=0;i<7;i=i+2)
{
sum1=sum1+s[i];
}
for(int j=1;jsum2)
{
printf(“%d”,sum1);
}
else
printf(“%d”,sum2);
} 2
• Briram

#include
#include

int main()
{
int s[]= {6, 7, 1, 3, 8, 2, 5};
int sum1=0,sum2=0;
for(int i=0;i<7;i=i+2)
{
sum1=sum1+s[i];
}
for(int j=1;jsum2)
{
printf(“%d”,sum1);
}
else
printf(“%d”,sum2);
} 0
• Briram

#include
#include

int main()
{
int s[]= {6, 7, 1, 3, 8, 2, 5};
int sum1=0,sum2=0;
for(int i=0;i<7;i=i+2)
{
sum1=sum1+s[i];
}
for(int j=1;jsum2)
{
printf(“%d”,sum1);
}
else
printf(“%d”,sum2);
} 0
• Briram

what i have written and what is posted is different. I am copying from codeblock and pasting here but after pasting the code in comment box, it became different. 0
• Care

What is the exact issue Briam, that you are facing while posting your answer. Can you please elaborate it. 0