- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

# Count possible ways to construct buildings

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 and Output

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.

## Algorithm

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

## Example

#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." ; }

## Output

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

- Related Questions & Answers
- C++ code to count ways to form reconnaissance units
- Finding all possible ways of integer partitioning in JavaScript
- 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 number of ways to jump to reach end in C++
- Print all possible ways to convert one string into another string in C++
- Program to count number of possible humble matrices in Python
- Count ways to spell a number with repeated digits in C++
- Count the number of ways to traverse a Matrix in C++
- Count the number of possible triangles in C++
- Count of suffix increment/decrement operations to construct a given array in C++
- Count number of ways to reach a given score in a game
- Count number of ways to divide a number in parts in C++

Advertisements