Python Program for Houses Problem (TCS Codevita) | PrepInsta

Python Program for Houses Problem

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:

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.

C++

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

C++

25 comments on “Python Program for Houses Problem (TCS Codevita) | PrepInsta”


  • ashu.835

    v=input()
    v.split()
    sum=0
    sum1=0
    for i in range(0, len(v)):
    if i%2==0:
    sum=sum+int(v[i]
    else:
    sum1 = sum1+int(v[i])
    print(max(sum1, sum))


  • Shaswata

    val = list(map(int,input().split()))
    total = sum(val)
    even = 0
    for i in range(0,len(val),2):
    even += val[i]
    print(i)
    print(max(even,total-even))