- 0
Notifications Mark All Read
- Login
- Get Prime
Python Program for Houses Problem (TCS Codevita) | PrepInsta
Houses Problem
Tata Consultancy Service in the search of best breed of programmers and developers organizes a Coding Competiton called TCS CodeVita. Houses is one of the sample problem of TCS CodeVita competition. The solution uses 1D array and some function from the MATH Class , here we have provided a solution in Python language for this problem.
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
Python Code
# This code is contributed by Mitanshu Mehta. num = list(map(int, input().split())) even, odd = 0, 0 for i in range(0, len(num), 2): odd+=num[i] for i in range(1, len(num), 2): even+=num[i] print(max(even,odd))
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:
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 To find the solution of Houses problem in C++ Programming language click on the button below:
Login/Signup to comment
Hope Someone Find this Helpful:
value = list(map(int, input(“Enter the Values of House: “).split(” “)))
house1 = sum(value[::2])
house2 = sum(value[1::2])
print(max(house1, house2))
num=list(map(int,input().split()))
a=sum(num[0::2])
b=sum(num[1::2])
print(max(a,b))
/*
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?
*/
// C code :-
#include
#define z 100
void main()
{
int n, temp, odd = 0, even = 0;
printf(“Enter number of houses\t”);
scanf(“%d”, &n);
for (int i = 0; i even ? printf(“%d”, odd) : printf(“%d”, even);
}
def maxtheftval(arr):
odd = even = 0
for i in range(0, len(arr)):
if i%2==0:
even += arr[i]
else:
odd += arr[i]
maxval = max(even, odd)
return maxval
arr = list(map(int, input().split()))
print(maxtheftval(arr))
Thanks for contributing the code Vivek
a=list(map(int,input().split()))
sum=0
b=a.index(max(a))
for i in range(b,-1,-2):
sum=sum+a[i]
for i in range(b, len(a),2):
sum=sum+a[i]
print(sum-a[b])
Thanks Nandish for contributing your course
n = list(map(int,input().split()))
L = [n[i] for i in range(len(n)) if i%2==0]
M = [n[j] for j in range(len(n)) if j%2!=0]
print(max(sum(L),sum(M)))
Try this buddy.
Thanks Rishav for contributing the code
n = list(map(int,input().split()))
L = [n[i] for i in range(len(n)) if i%2==0]
M = [n[j] for i in range(len(n)) if i%2!=0]
print(max(sum(L),sum(M)))
Try this buddy.
forgot j in the 3rd line😛. Just put j in place of i in line 3..
No problem Rishav, we’ll fix it up.
This is the simplest way to solve…..
n=list(map(int,input().split()))
sum1,sum2=0,0
for i in range(len(n)):
if i%2==0:
sum1=sum1+n[i]
else:
sum2=sum2+n[i]
print(max(sum1,sum2))
Thanks for contributing the code Mohit
Houses=list(map(int,input().split()))
even,odd=0,0
for i in range(len(Houses)):
if i%2==0:even+=Houses[i]
for j in range(len(Houses)):
if j%2!=0:odd+=Houses[j]
print(max(even,odd))
num = list(map(int, input().split()))
even, odd = 0, 0
for i in range(0, len(num), 2):
odd+=num[i]
for i in range(1, len(num), 2):
even+=num[i]
print(max(even,odd))
Thanks Mitanshu, we’ll update our code with this
arr = [2,500,700,4,6,8,200,1000]
odd=even=0
for i in range(0,len(arr),2):
even+=arr[i]
for i in range(1,len(arr),2):
odd+=arr[i]
print(max(even,odd))
Thanks for contributing this logic.
The above given code is wrong. Please refer to this code.
num = list(map(int, input().split()))
sum = 0
for i in range(0, len(num), 2):
sum = sum + int(num[i])
print(sum)
your code is also wrong
Review this
num = list(map(int, input().split()))
result=0
temp=0
tempindex=0
store=0
for i in range(0,len(num)):
temp=max(num)
for j in range (0,len(num)):
if(num[j]==temp):
tempindex=j
if(temp!=-1):
result=result+temp
num[tempindex]=-1
try:
num[tempindex-1]=-1
num[tempindex+1]=-1
except IndexError:
store=100
print(result)
def maxtheftval(arr):
odd = even = 0
for i in range(0, len(arr)):
if i%2 == 0:
even += arr[i]
else:
odd += arr[i]
maxval = max(even, odd)
return maxval
arr = list(map(int, input().split()))
print(maxtheftval(arr))