Don’t worry, unlock all articles / blogs on PrepInsta by just simply logging in on our website
C++ Program for Houses Problem (TCS Codevita) | PrepInsta
July 2, 2020
Houses Problem
TCS, the largest IT Company of India, each year, organizes a hack-a-thon in which the participants can win prizes from the prize pool worth upto 20k $. The solution uses a user defined fuction called maxLoot which returns the max value, here we have provided a solution in C++ language for this problem.
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[0]);
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[0];
if (n == 2)
return max(hval[0], hval[1]); // dp[i] represent the maximum value stolen
// so far after reaching house i.
int dp[n];
// Initialize the dp[0] and dp[1]
dp[0] = hval[0];
dp[1] = max(hval[0], hval[1]);
// 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
Houses Problem in other Languages
JAVA
To find the solution of To find the solution of Houses problem in JAVA Programming language click on the button below:
I am confused with the question as well as the answer posted here…
#include
using namespace std;
int main()
{
int n,i,j;
cout<>n;
int house_no[n];
for(i=0;i<n;i++)
{cout<<"Enter the value of house no. "<<i+1<>house_no[i];
}
for(j=1;j<n;++j)
{
if(j%2==0)
house_no[0]+=house_no[j];
else
continue;
}
cout<<"The max. he can steal is "<<house_no[0];
}
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.
#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[0];
pincl=incl;
excl=0;
Fo(1,n){
incl= excl+a[i];
excl= max(excl,pincl);
pincl=incl;
}
cout<> t;
while(t–) {
solve();
}
return 0;
}
I am confused with the question as well as the answer posted here…
#include
using namespace std;
int main()
{
int n,i,j;
cout<>n;
int house_no[n];
for(i=0;i<n;i++)
{cout<<"Enter the value of house no. "<<i+1<>house_no[i];
}
for(j=1;j<n;++j)
{
if(j%2==0)
house_no[0]+=house_no[j];
else
continue;
}
cout<<"The max. he can steal is "<<house_no[0];
}
#include
using namespace std;
int main(){
int n,sum=0;
cin>>n;
int arr[n];
for(int i=0;i>arr[i];
}
while(1){
int max=arr[0],count=1,pos=0;
for(int i=1;i<n;i++){
if(max<arr[i]){
max=arr[i];
pos=i;
}
if(arr[i]==0){
++count;
}
}
arr[pos]=0;
arr[pos+1]=0;
if(pos !=0){
arr[pos-1]=0;
}
sum+=max;
if(count==n){
break;
}
}
cout<<sum;
}
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);
}
#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);
}
#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);
}
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.
What is the exact issue Briam, that you are facing while posting your answer. Can you please elaborate it.