There is a street of length n and as we know it has two sides. Therefore a total of 2 * n spots are available. In each of these spots either a house or an office can be built with following 2 restrictions:

1. No two offices on the same side of the street can be adjacent.

2. No two offices on different sides of the street can be exactly opposite to each other i.e. they can’t overlook each other.

There are no restrictions on building houses and each spot must either have a house or office.

Given length of the street n, find total number of ways to build the street.

**Examples:**

Input : 2 Output : 7 Please see below diagram for explanation. Input : 3 Output : 17

Following image depicts the 7 possible ways for building the street with N = 2

Ways for building street with length 1 with 2 houses: (H H) = {1} with 1 office and 1 house: (O H) (H O) = {2} (O O) is not allowed according to the problem statement. Total = 1 + 2 = 3

For length = 2, with 2 houses: (H H) can be added to all the cases of length 1: (H H) (H H) (H H) (H H) (O H) (H O) = {3} with 1 office and 1 house: (O H) and (H O) can both be added to all the cases of length 1 with last row (H H) (O H) (H O) (H H) (H H) = {2} when last row of a case of length 1 contains 1 office, it can only be extended in one way to build an office in row 2. (H O) (O H) (O H) (H O) = {2} (O H) (H O) (O O) (O H) (H O) (O H) etc are not allowed. Total = 3 + 2 + 2 = 7

Since the problem can be solved by finding solution for smaller subproblems and then extending the same logic, it can be solved using dynamic programming. We move in steps of one unit length. For each row we have two options:

Build houses in both the spots

Build one house and one office

The first one can be done without any constraints. There is one way of building houses in both the spots at length i. So total ways using this choice = total ways for length i – 1.

For the second choice, if row (i-1) had houses in both spots we have two ways of building a office i.e. (H O) and (O H)

if row(i-1) had an office in one of its two spots we only have one way to build an office in row i.If prev row had (O H) curr row would have (H O) and similarly for prev row = (H O) curr row = (O H).

From the above logic, total ways with this choice = 2 * (choice1(i-1)) + choice2(i-1)

We will build a 2D dp for this.

dp[0][i] indicates choice1 and dp[1][i] indicates choice2 for row i.

Below is the implementation of above idea :

## C++

`// C++ program to count ways to build street ` `// under given constraints ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to count ways of building ` `// a street of n rows ` `long` `countWays(` `int` `n) ` `{ ` ` ` `long` `dp[2][n + 1]; ` ` ` ` ` `// base case ` ` ` `dp[0][1] = 1; ` ` ` `dp[1][1] = 2; ` ` ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` ` ` `// ways of building houses in both ` ` ` `// the spots of ith row ` ` ` `dp[0][i] = dp[0][i - 1] + dp[1][i - 1]; ` ` ` ` ` `// ways of building an office in one of ` ` ` `// the two spots of ith row ` ` ` `dp[1][i] = dp[0][i - 1] * 2 + dp[1][i - 1]; ` ` ` `} ` ` ` ` ` `// total ways for n rows ` ` ` `return` `dp[0][n] + dp[1][n]; ` `} ` ` ` `// driver program for checking above function ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 5; ` ` ` `cout << ` `"Total no of ways with n = "` `<< n ` ` ` `<< ` `" are: "` `<< countWays(n) << endl; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count ways to build street ` `// under given constraints ` `public` `class` `GFG { ` ` ` `// function to count ways of building ` `// a street of n rows ` ` ` `static` `long` `countWays(` `int` `n) { ` ` ` `long` `dp[][] = ` `new` `long` `[` `2` `][n + ` `1` `]; ` ` ` ` ` `// base case ` ` ` `dp[` `0` `][` `1` `] = ` `1` `; ` ` ` `dp[` `1` `][` `1` `] = ` `2` `; ` ` ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) { ` ` ` ` ` `// ways of building houses in both ` ` ` `// the spots of ith row ` ` ` `dp[` `0` `][i] = dp[` `0` `][i - ` `1` `] + dp[` `1` `][i - ` `1` `]; ` ` ` ` ` `// ways of building an office in one of ` ` ` `// the two spots of ith row ` ` ` `dp[` `1` `][i] = dp[` `0` `][i - ` `1` `] * ` `2` `+ dp[` `1` `][i - ` `1` `]; ` ` ` `} ` ` ` ` ` `// total ways for n rows ` ` ` `return` `dp[` `0` `][n] + dp[` `1` `][n]; ` ` ` `} ` ` ` `// driver program for checking above function ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.print(` `"Total no of ways with n = "` `+ n ` ` ` `+ ` `" are: "` `+ countWays(n)); ` ` ` `} ` ` ` `} ` ` ` `/*This code is contributed by PrinciRaj1992*/` |

*chevron_right*

*filter_none*

## Python3

`# Python program to count ways to build ` `# street under given constraints ` ` ` `# function to count ways of building ` `# a street of n rows ` `def` `countWays(n) : ` ` ` ` ` ` ` `dp ` `=` `[[` `0` `] ` `*` `(n ` `+` `1` `) ` `for` `i ` `in` `range` `(` `2` `)] ` ` ` ` ` `# base case ` ` ` `dp[` `0` `][` `1` `] ` `=` `1` ` ` `dp[` `1` `][` `1` `] ` `=` `2` ` ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `) : ` ` ` ` ` `# ways of building houses in both ` ` ` `# the spots of ith row ` ` ` `dp[` `0` `][i] ` `=` `dp[` `0` `][i ` `-` `1` `] ` `+` `dp[` `1` `][i ` `-` `1` `] ` ` ` ` ` `# ways of building an office in one of ` ` ` `# the two spots of ith row ` ` ` `dp[` `1` `][i] ` `=` `(dp[` `0` `][i ` `-` `1` `] ` `*` `2` `+` ` ` `dp[` `1` `][i ` `-` `1` `]) ` ` ` ` ` `# total ways for n rows ` ` ` `return` `dp[` `0` `][n] ` `+` `dp[` `1` `][n] ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `5` ` ` `print` `(` `"Total no of ways with n ="` `, ` ` ` `n, ` `"are:"` `, countWays(n)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count ways to build street ` `// under given constraints ` ` ` `using` `System; ` `public` `class` `GFG{ ` ` ` ` ` `// function to count ways of building ` `// a street of n rows ` ` ` `static` `long` `countWays(` `int` `n) { ` ` ` `long` `[,]dp = ` `new` `long` `[2 , n + 1]; ` ` ` ` ` `// base case ` ` ` `dp[0,1] = 1; ` ` ` `dp[1,1] = 2; ` ` ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` ` ` `// ways of building houses in both ` ` ` `// the spots of ith row ` ` ` `dp[0 , i] = dp[0 , i - 1] + dp[1 , i - 1]; ` ` ` ` ` `// ways of building an office in one of ` ` ` `// the two spots of ith row ` ` ` `dp[1 , i] = dp[0 , i - 1] * 2 + dp[1 , i - 1]; ` ` ` `} ` ` ` ` ` `// total ways for n rows ` ` ` `return` `dp[0 , n] + dp[1 , n]; ` ` ` `} ` ` ` `// driver program for checking above function ` ` ` `public` `static` `void` `Main() { ` ` ` ` ` `int` `n = 5; ` ` ` `Console.Write(` `"Total no of ways with n = "` `+ n ` ` ` `+ ` `" are: "` `+ countWays(n)); ` ` ` `} ` ` ` `} ` ` ` `/*This code is contributed by PrinciRaj1992*/` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count ways to build street ` `// under given constraints ` ` ` `// function to count ways of building ` `// a street of n rows ` `function` `countWays(` `$n` `) ` `{ ` ` ` ` ` `// base case ` ` ` `$dp` `[0][1] = 1; ` ` ` `$dp` `[1][1] = 2; ` ` ` ` ` `for` `(` `$i` `= 2; ` `$i` `<= ` `$n` `; ` `$i` `++) { ` ` ` ` ` `// ways of building houses in both ` ` ` `// the spots of ith row ` ` ` `$dp` `[0][` `$i` `] = ` `$dp` `[0][` `$i` `- 1] + ` ` ` `$dp` `[1][` `$i` `- 1]; ` ` ` ` ` `// ways of building an office in one of ` ` ` `// the two spots of ith row ` ` ` `$dp` `[1][` `$i` `] = ` `$dp` `[0][` `$i` `- 1] * ` ` ` `2 + ` `$dp` `[1][` `$i` `- 1]; ` ` ` `} ` ` ` ` ` `// total ways for n rows ` ` ` `return` `$dp` `[0][` `$n` `] + ` `$dp` `[1][` `$n` `]; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 5; ` ` ` `echo` `"Total no of ways with n = "` `,` `$n` `, ` ` ` `" are: "` `,countWays(` `$n` `),` `"\n"` `; ` ` ` `// This code is contributed by jit_t ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Total no of ways with n = 5 are: 99

**Time Complexity:** O(N)

**Auxiliary Space :** O(N)

This article is contributed by **Aditi Sharma**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of ways to arrange N items under given constraints
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Count of strings that can be formed using a, b and c under given constraints
- Ways to form a group from three groups with given constraints
- Count digit groupings of a number with given constraints
- Find the longest path in a matrix with given constraints
- Count of Ways to obtain given Sum from the given Array elements
- Minimum cost to build N blocks from one block
- Maximum path sum for each position with jumps under divisibility condition
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count number of ways to reach a given score in a game
- Count number of ways to reach a given score in a Matrix
- Count of ways to split a given number into prime segments
- Count ways to obtain given sum by repeated throws of a dice
- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Count ways to reach the n'th stair
- Count possible ways to construct buildings
- Bitmasking and Dynamic Programming | Set 1 (Count ways to assign unique cap to every person)
- Count number of ways to cover a distance
- Count number of ways to partition a set into k subsets