# JAVA 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

## JAVA Code

```import java.util.Scanner;
public class Main {

static int prep(int hval[],int n) {
if(n==0)
return 0;
if(n==1)
return hval[0];
if(n==2)
return Math.max(hval[0],hval[1]);

int[] dp = new int[n];
dp[0]=hval[0];
dp[1]=Math.max(hval[0],hval[1]);

for (int i = 2; i < n; i++) {
dp[i]=Math.max(hval[i]+dp[i-2],dp[i-1]);
}
return dp[n-1];
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int hval[] = {3,2,4,6,7,1};
int n=hval.length;
System.out.println("Max loot value :"+prep(hval,n));
}

}```
```Output
6 7 1 3 8 2 5
20```

## Python

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

Python

## C

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

## C++

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

### 3 comments on “JAVA Program for Houses Problem (TCS Codevita) | PrepInsta”

• Dilip

here is the code for HOUSE PROBLEM in C language
#include
#include
#include
int max(int x,int y)
{
if(x>y)
{
return x;
}
else
return y;
}
int main()
{
int n,i,k,sum=0,a[100],x=0,y=0;
printf(“enter the value for n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
if(n<=2)
{
if(n==0)
{
printf(" no elemnts to do thiefing %d",x);
}
if(n==1)
{
printf("only one element had been stolen %d",a[0]);

}
if(n==2)
{
y=max(a[0],a[1]);
printf("max of two is stolen : %d ",y);
}
}
else
{

//this follwing code is for more than two elements

i=0;
while(ia[i+1])
{
sum=sum+a[i];
k=i;
k=k+2;
}
else
{
sum=sum+a[i+1];
k=i+1;
k=k+2;
}
i=k;
}
}

if(i==(n-1))
{
sum=sum+a[i];
}
printf(” the maximal value theft by thief is %d”,sum);
}
}

• Dilip

#include
#include
#include
int max(int x,int y)
{
if(x>y)
{
return x;
}
else
return y;
}
int main()
{
int n,i,k,sum=0,a[100],x=0,y=0;
printf(“enter the value for n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
if(n<=2)
{
if(n==0)
{
printf(" no elemnts to do thiefing %d",x);
}
if(n==1)
{
printf("only one element had been stolen %d",a[0]);

}
if(n==2)
{
y=max(a[0],a[1]);
printf("max of two is stolen : %d ",y);
}
}
else
{

//this follwing code is for more than two elements

i=0;
while(ia[i+1])
{
sum=sum+a[i];
k=i;
k=k+2;
}
else
{
sum=sum+a[i+1];
k=i+1;
k=k+2;
}
i=k;
}
}

if(i==(n-1))
{
sum=sum+a[i];
}
printf(” the maximal value theft by thief is %d”,sum);
}
}

• Arun

import java.util.*;

public class CVHouses {
public static void main(String[] args){
Scanner scan = new Scanner (System.in);
int s = scan.nextInt();
int[] arr = new int[s];
for(int i =0; i < s; i++){
arr[i] = scan.nextInt();
}
int max = 0, pos = 0, val = 0;
boolean flag = false;
while(true){
for(int i = 0; i < s; i++){
if(max < arr[i]){
max = arr[i];
pos = i;
flag = true;
}
}

val += arr[pos];
arr[pos] = 0;
if(pos-1 != -1){
arr[pos-1] = 0;
}
if(pos+1 != s){
arr[pos+1] = 0;
}

if(!flag){
break;
}
max = 0;
flag = false;
}

System.out.println(val);
}
}