- Related Questions & Answers
- Count ways to reach the n’th stair
- Count ways to form minimum product triplets in C++
- C++ Program to Perform Partition of an Integer in All Possible Ways
- Shortest Distance from All Buildings in C++
- Count the number of possible triangles in C++
- Count number of ways to reach a given score in a game
- Count all possible paths between two vertices in C++
- Count ways of choosing a pair with maximum difference in C++
- How to construct custom attributes in C#?
- Why did kings construct monuments?
- Construct the Rectangle in C++
- Construct string via recursion JavaScript
- Count number of right triangles possible with a given perimeter in C++
- Java Program to construct one String from another
- Python - Ways to Copy Dictionary

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Here n number of sections are given, in each section, there are two sides on the road to constructing buildings. If there is one empty space between two houses are needed, then how many possible ways to construct buildings in the plot.

There are four possibilities to construct buildings

- One side of the road
- Another side of the road
- No building can be constructed
- Both sides of the road

Input: It takes the number of sections to construct buildings. Say the input is 3. Output: Enter Number of sections: 3 Buildings can be constructed in 25 different ways.

constructionWays(n)

**Input:** There are n number of section.

**Output −** Number of possible ways.

Begin if n = 1, then return 4 countEnd := 1 countEndSpace := 1 for i := 2 to n, do prevCountEnd := countEnd prevCountEndSpace := countEndSpace countEndSpace := countEnd + prevCountEndSpace countEnd := prevCountEndSpace done answer := countEndSpace + countEnd return answer^2 End

#include<iostream> using namespace std; int constructionWays(int n) { if (n == 1) //if there is one section return 4; //4 possible ways to construct building in that section //set counting values for place at the end and end with space int countEnd=1, countEndSpace=1, prevCountEnd, prevCountEndSpace; for (int i=2; i<=n; i++) { //fot the second section to nth section prevCountEnd = countEnd; prevCountEndSpace = countEndSpace; countEndSpace = countEnd + prevCountEndSpace; countEnd = prevCountEndSpace; } //possible ways to end with space and building at the end int answer = countEndSpace + countEnd; return (answer*answer); //for two sides the answer will be squared } int main() { int n; cout << "Enter Number of sections: "; cin >> n; cout << "Buildings can be constructed in " << constructionWays(n) <<" different ways." ; }

Enter Number of sections: 3 Buildings can be constructed in 25 different ways.

Advertisements