# Building Problem – Dynamic Programming Solutions

«
»

This is a C++ Program that Solves Building Problem using Dynamic Programming technique.

Problem Description

There are n plots in a row. Buildings are to be constructed on these plots in such a way that there is space between every two buildings. Find the number of ways in which buildings can be constructed in the plots.

Problem Solution

This is a very simple problem if you observe it carefully. The solution to this problem is simply (n+2)th fibonacci number.

Expected Input and Output

Case-1:

```n=1
ways=2 either build here or not```

Case-2:

Note: Join free Sanfoundry classes at Telegram or Youtube
```n=2
ways=3 (both empty, only first empty, only second empty)```
Program/Source Code

Here is source code of the C++ Program to Solve Building Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `#include<iostream>`
2. `using namespace std;`
3. ` `
4. `//this function returns the (n+2)th fibonacci number`
5. `int buildingProblem(int n)`
6. `{`
7. `    int first=1, second=1;`
8. `    int j;`
9. ` `
10. `    for(int i=1;i<=n;i++)`
11. `    {`
12. `        j=first;`
13. `        first=second;`
14. `        second+=j;`
15. `    }`
16. ` `
17. `    return second;`
18. `}`
19. ` `
20. `int main()`
21. `{`
22. `    int n;`
23. `    cout<<"Enter the number of plots ";`
24. `    cin>>n;`
25. ` `
26. `    cout<<"The number of ways in which buildings can be constructed with the given constraints is "<<endl;`
27. `    cout<<buildingProblem(n);`
28. ` `
29. `    cout<<endl;`
30. `    return 0;`
31. `}`
Program Explanation

In the main function, we ask the user to input the value for number of buildings. We pass this value to the function buildingProblem as parameter. This function will calculate the expected result and return it. The returned value will be displayed.

Runtime Test Cases
```
Case-1:
\$ g++ building_problem.cpp
\$ ./a.outEnter the number of plots 3
The number of ways in which buildings can be constructed with the given constraints is
5```

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions. 