C++ Program for Houses Problem (TCS Codevita) | PrepInsta

C++ Program for Houses Problem

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.

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

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:

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.

Python

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

 Python

5 comments on “C++ Program for Houses Problem (TCS Codevita) | PrepInsta”


  • Briram

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


    • Briram

      #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);
      }


      • Briram

        #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);
        }


        • Briram

          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.

          0

        • Care

          What is the exact issue Briam, that you are facing while posting your answer. Can you please elaborate it.

          0