Given a number n, count number of ways to fill a n x 4 grid using 1 x 4 tiles.

Examples:

Input :n = 1Output : 1 Input :n = 2Output : 1 We can only place both tiles horizontally Input :n = 3Output : 1 We can only place all tiles horizontally. Input :n = 4Output : 2 The two ways are : 1) Place all tiles horizontally 2) Place all tiles vertically. Input :n = 5Output : 3 We can fill a 5 x 4 grid in following ways : 1) Place all 5 tiles horizontally 2) Place first 4 vertically and 1 horizontally. 3) Place first 1 horizontally and 4 horizontally.

## We strongly recommend that you click here and practice it, before moving on to the solution.

This problem is mainly an extension of this tiling problem

Let **“count(n)” be the count of ways to place tiles on a “n x 4” grid**, following two cases arise when we place the first tile.

**Place the first tile horizontally**: If we place first tile horizontally, the problem reduces to “count(n-1)”**Place the first tile vertically**: If we place first tile vertically, then we must place 3 more tiles vertically. So the problem reduces to “count(n-4)”

Therefore, count(n) can be written as below.

count(n) = 1 if n = 1 or n = 2 or n = 3 count(n) = 2 if n = 4 count(n) = count(n-1) + count(n-4)

This recurrence is similar to Fibonacci Numbers and can be solved using Dynamic programming.

## C/C++

`// C++ program to count of ways to place 1 x 4 tiles ` `// on n x 4 grid. ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `// Returns count of count of ways to place 1 x 4 tiles ` `// on n x 4 grid. ` `int` `count(` `int` `n) ` `{ ` ` ` `// Create a table to store results of subproblems ` ` ` `// dp[i] stores count of ways for i x 4 grid. ` ` ` `int` `dp[n+1]; ` ` ` `dp[0] = 0; ` ` ` ` ` `// Fill the table from d[1] to dp[n] ` ` ` `for` `(` `int` `i=1; i<=n; i++) ` ` ` `{ ` ` ` `// Base cases ` ` ` `if` `(i >= 1 && i <= 3) ` ` ` `dp[i] = 1; ` ` ` `else` `if` `(i==4) ` ` ` `dp[i] = 2 ; ` ` ` ` ` `else` ` ` `// dp(i-1) : Place first tile horizontally ` ` ` `// dp(n-4) : Place first tile vertically ` ` ` `// which means 3 more tiles have ` ` ` `// to be placed vertically. ` ` ` `dp[i] = dp[i-1] + dp[i-4]; ` ` ` `} ` ` ` ` ` `return` `dp[n]; ` `} ` ` ` `// Driver program to test above ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` `cout << ` `"Count of ways is "` `<< count(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count of ways to place 1 x 4 tiles ` `// on n x 4 grid ` `import` `java.io.*; ` ` ` `class` `Grid ` `{ ` ` ` `// Function that count the number of ways to place 1 x 4 tiles ` ` ` `// on n x 4 grid. ` ` ` `static` `int` `count(` `int` `n) ` ` ` `{ ` ` ` `// Create a table to store results of sub-problems ` ` ` `// dp[i] stores count of ways for i x 4 grid. ` ` ` `int` `[] dp = ` `new` `int` `[n+` `1` `]; ` ` ` `dp[` `0` `] = ` `0` `; ` ` ` `// Fill the table from d[1] to dp[n] ` ` ` `for` `(` `int` `i=` `1` `;i<=n;i++) ` ` ` `{ ` ` ` `// Base cases ` ` ` `if` `(i >= ` `1` `&& i <= ` `3` `) ` ` ` `dp[i] = ` `1` `; ` ` ` `else` `if` `(i==` `4` `) ` ` ` `dp[i] = ` `2` `; ` ` ` ` ` `else` ` ` `{ ` ` ` `// dp(i-1) : Place first tile horizontally ` ` ` `// dp(i-4) : Place first tile vertically ` ` ` `// which means 3 more tiles have ` ` ` `// to be placed vertically. ` ` ` `dp[i] = dp[i-` `1` `] + dp[i-` `4` `]; ` ` ` `} ` ` ` `} ` ` ` `return` `dp[n]; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` `System.out.println(` `"Count of ways is: "` `+ count(n)); ` ` ` `} ` `} ` ` ` `// Contributed by Pramod Kumar ` |

*chevron_right*

*filter_none*

## Python

`# Python program to count of ways to place 1 x 4 tiles ` `# on n x 4 grid. ` ` ` `# Returns count of count of ways to place 1 x 4 tiles ` `# on n x 4 grid. ` `def` `count(n): ` ` ` ` ` `# Create a table to store results of subproblems ` ` ` `# dp[i] stores count of ways for i x 4 grid. ` ` ` `dp ` `=` `[` `0` `for` `_ ` `in` `range` `(n` `+` `1` `)] ` ` ` ` ` `# Fill the table from d[1] to dp[n] ` ` ` `for` `i ` `in` `range` `(` `1` `,n` `+` `1` `): ` ` ` ` ` `# Base cases ` ` ` `if` `i <` `=` `3` `: ` ` ` `dp[i] ` `=` `1` ` ` `elif` `i ` `=` `=` `4` `: ` ` ` `dp[i] ` `=` `2` ` ` `else` `: ` ` ` `# dp(i-1) : Place first tile horizontally ` ` ` `# dp(n-4) : Place first tile vertically ` ` ` `# which means 3 more tiles have ` ` ` `# to be placed vertically. ` ` ` `dp[i] ` `=` `dp[i` `-` `1` `] ` `+` `dp[i` `-` `4` `] ` ` ` ` ` `return` `dp[n] ` ` ` `# Driver code to test above ` `n ` `=` `5` `print` `(` `"Count of ways is"` `), ` `print` `(count(n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count of ways ` `// to place 1 x 4 tiles on ` `// n x 4 grid ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function that count the number ` ` ` `// of ways to place 1 x 4 tiles ` ` ` `// on n x 4 grid. ` ` ` `static` `int` `count(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Create a table to store results ` ` ` `// of sub-problems dp[i] stores ` ` ` `// count of ways for i x 4 grid. ` ` ` `int` `[] dp = ` `new` `int` `[n + 1]; ` ` ` `dp[0] = 0; ` ` ` ` ` `// Fill the table from d[1] ` ` ` `// to dp[n] ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Base cases ` ` ` `if` `(i >= 1 && i <= 3) ` ` ` `dp[i] = 1; ` ` ` `else` `if` `(i == 4) ` ` ` `dp[i] = 2 ; ` ` ` ` ` `else` ` ` `{ ` ` ` ` ` `// dp(i-1) : Place first tile ` ` ` `// horizontally dp(i-4) : ` ` ` `// Place first tile vertically ` ` ` `// which means 3 more tiles have ` ` ` `// to be placed vertically. ` ` ` `dp[i] = dp[i - 1] + ` ` ` `dp[i - 4]; ` ` ` `} ` ` ` `} ` ` ` `return` `dp[n]; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` `Console.WriteLine(` `"Count of ways is: "` ` ` `+ count(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count of ways to ` `// place 1 x 4 tiles on n x 4 grid. ` ` ` `// Returns count of count of ways ` `// to place 1 x 4 tiles ` `// on n x 4 grid. ` `function` `countt(` `$n` `) ` `{ ` ` ` ` ` `// Create a table to store ` ` ` `// results of subproblems ` ` ` `// dp[i] stores count of ` ` ` `// ways for i x 4 grid. ` ` ` `$dp` `[` `$n` `+ 1] = 0; ` ` ` `$dp` `[0] = 0; ` ` ` ` ` `// Fill the table ` ` ` `// from d[1] to dp[n] ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` ` ` `// Base cases ` ` ` `if` `(` `$i` `>= 1 && ` `$i` `<= 3) ` ` ` `$dp` `[` `$i` `] = 1; ` ` ` `else` `if` `(` `$i` `== 4) ` ` ` `$dp` `[` `$i` `] = 2 ; ` ` ` ` ` `else` ` ` `// dp(i-1) : Place first tile horizontally ` ` ` `// dp(n-4) : Place first tile vertically ` ` ` `// which means 3 more tiles have ` ` ` `// to be placed vertically. ` ` ` `$dp` `[` `$i` `] = ` `$dp` `[` `$i` `- 1] + ` `$dp` `[` `$i` `- 4]; ` ` ` `} ` ` ` ` ` `return` `$dp` `[` `$n` `]; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 5; ` ` ` `echo` `"Count of ways is "` `, countt(` `$n` `); ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

Output :

Count of ways is 3

Time Complexity : O(n)

Auxiliary Space : O(n)

This article is contributed by **Rajat Jha**. 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:

- Count the number of ways to tile the floor of size n x m using 1 x m size tiles
- Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
- Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted
- Fill array with 1's using minimum iterations of filling neighbors
- Collect maximum points in a grid using two traversals
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Minimum cost to fill given weight in a bag
- 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 ways to divide circle using N non-intersecting chords
- Count ways to reach a score using 1 and 2 with no consecutive 2s
- Count ways to reach the nth stair using step 1, 2 or 3
- Count number of ways to reach a given score in a game
- Count number of ways to cover a distance
- Count number of ways to partition a set into k subsets
- Count number of ways to reach destination in a Maze
- Count number of ways to jump to reach end
- Count the number of ways to traverse a Matrix
- Count number of ways to reach a given score in a Matrix
- Count number of ways to get Odd Sum
- Count number of ways to arrange first N numbers