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”


  • Ashish

    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))


  • Arjun

    /*
    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);
    }


  • Vivek Singh

    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))


  • nandish

    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])


  • RISHAV

    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.


  • RISHAV

    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.


  • Mohit

    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))


  • Sukesh

    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))


  • Mitanshu

    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))


  • kritiy

    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))


  • ROHIT

    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)


      • ravi

        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)


        • Vivek Singh

          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))