- Related Questions & Answers
- Finding all possible ways of integer partitioning in JavaScript
- Shortest Distance from All Buildings in C++
- 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
- Count number of ways to jump to reach end in C++
- Print all possible ways to convert one string into another string in C++
- Count of suffix increment/decrement operations to construct a given array in C++
- 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 number of ways to reach a given score in a game
- Count number of ways to divide a number in parts in C++
- Count ways to divide circle using N non-intersecting chords in C++
- Count ways to express a number as sum of powers in C++

- 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