Given a number n, count number of ways to fill a n x 4 grid using 1 x 4 tiles.
Examples: 
 
Input : n = 1
Output : 1
Input : n = 2
Output : 1
We can only place both tiles horizontally
Input : n = 3
Output : 1
We can only place all tiles horizontally.
Input : n = 4
Output : 2
The two ways are :
1) Place all tiles horizontally
2) Place all tiles vertically.
Input : n = 5
Output : 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 vertically.
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++ program to count of ways to place 1 x 4 tiles // on n x 4 grid. #include<iostream> usingnamespacestd; // Returns count of count of ways to place 1 x 4 tiles // on n x 4 grid. intcount(intn) {     // Create a table to store results of subproblems     // dp[i] stores count of ways for i x 4 grid.     intdp[n+1];     dp[0] = 0;     // Fill the table from d[1] to dp[n]     for(inti=1; i<=n; i++)     {         // Base cases         if(i >= 1 && i <= 3)             dp[i] = 1;         elseif(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];     }     returndp[n]; } // Driver program to test above intmain() {     intn = 5;     cout << "Count of ways is "<< count(n);     return0; }  | 
Java
| // Java program to count of ways to place 1 x 4 tiles// on n x 4 gridimportjava.io.*;classGrid {     // Function that count the number of ways to place 1 x 4 tiles    // on n x 4 grid.    staticintcount(intn)    {        // Create a table to store results of sub-problems        // dp[i] stores count of ways for i x 4 grid.        int[] dp = newint[n+1];        dp[0] = 0;        // Fill the table from d[1] to dp[n]        for(inti=1;i<=n;i++)        {            // Base cases            if(i >= 1&& i <= 3)                dp[i] = 1;            elseif(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];            }        }        returndp[n];    }        // Driver program    publicstaticvoidmain (String[] args)    {        intn = 5;        System.out.println("Count of ways is: "+ count(n));    }}// Contributed by Pramod Kumar | 
Python3
| # 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.defcount(n):    # Create a table to store results of subproblems    # dp[i] stores count of ways for i x 4 grid.    dp =[0for_ inrange(n+1)]    # Fill the table from d[1] to dp[n]    fori inrange(1,n+1):        # Base cases        ifi <=3:            dp[i] =1        elifi ==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]    returndp[n]# Driver code to test aboven =5print("Count of ways is"),print(count(n)) | 
C#
| // C# program to count of ways // to place 1 x 4 tiles on// n x 4 gridusingSystem;classGFG{        // Function that count the number     // of ways to place 1 x 4 tiles    // on n x 4 grid.    staticintcount(intn)    {                // Create a table to store results        // of sub-problems dp[i] stores        // count of ways for i x 4 grid.        int[] dp = newint[n + 1];        dp[0] = 0;                // Fill the table from d[1]        // to dp[n]        for(inti = 1; i <= n; i++)        {                        // Base cases            if(i >= 1 && i <= 3)                dp[i] = 1;            elseif(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];            }        }        returndp[n];    }        // Driver Code    publicstaticvoidMain ()    {        intn = 5;        Console.WriteLine("Count of ways is: "                           + count(n));    }}// This code is contributed by Sam007 | 
Javascript
| <script>// JavaScript program to count of ways to place 1 x 4 tiles// on n x 4 grid    // Function that count the number of ways to place 1 x 4 tiles    // on n x 4 grid.    functioncount(n)    {            // Create a table to store results of sub-problems        // dp[i] stores count of ways for i x 4 grid.        let dp = [];        dp[0] = 0;                // Fill the table from d[1] to dp[n]        for(let i = 1; i <= n; i++)        {                    // Base cases            if(i >= 1 && i <= 3)                dp[i] = 1;            elseif(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];            }        }        returndp[n];    }// Driver Code        let n = 5;        document.write("Count of ways is: "+ count(n));        // This code is contributed by target_2.</script> | 
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.functioncountt($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;        elseif($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.?> | 
Output :
Count of ways is 3
Time Complexity : O(n) 
Auxiliary Space : O(n)
Efficient approach : Space Optimization
In previous approach we the current value dp[i] is only depend upon the previous 2 values i.e. dp[i-1] and dp[i-4]. So to optimize the space we can keep track of previous 4 values and current values which will reduce the space complexity from O(N) to O(1).
Implementation Steps:
- Handle the base cases:
- If n is less than or equal to 0, return 0.
- If n is less than or equal to 3, return 1.
 
- Initialize variables dp1, dp2, dp3, and dp4 with values 1, 1, 1, and 2 respectively.
- Initialize dp to 0.
- Iterate from 5 to n:
- Calculate dp as the sum of dp4 and dp1.
- Update the variables: shift dp1 to dp2, dp2 to dp3, dp3 to dp4, and dp4 to dp.
- Return dp as the count of ways.
Implementation:
C++
| // C++ code for the above approach approach #include<iostream>usingnamespacestd;// Returns count of count of ways to place 1 x 4 tiles// on n x 4 grid.intcount(intn){       // Base Case    if(n <= 0)        return0;    if(n <= 3)        return1;        // create the dp previous instances    intdp1 = 1;    intdp2 = 1;    intdp3 = 1;    intdp4 = 2;    intdp = 0; // to store current value        // iterate to get the current value from previous computations    for(inti = 5; i <= n; i++)    {        dp = dp4 + dp1;        dp1 = dp2;        dp2 = dp3;        dp3 = dp4;        dp4 = dp;    }    // return final answer    returndp;}// Driver codeintmain(){    intn = 5;    cout << "Count of ways is "<< count(n) << endl;    return0;}// -- by bhardwajji | 
Java
| /*package whatever //do not write package name here */importjava.io.*;classGfg {    // Returns count of ways to place 1 x 4 tiles on n x 4 grid.    staticintcount(intn) {        // Base Case        if(n <= 0)            return0;        if(n <= 3)            return1;        // Create the dp previous instances        intdp1 = 1;        intdp2 = 1;        intdp3 = 1;        intdp4 = 2;        intdp = 0; // To store the current value        // Iterate to get the current value from previous computations        for(inti = 5; i <= n; i++) {            dp = dp4 + dp1;            dp1 = dp2;            dp2 = dp3;            dp3 = dp4;            dp4 = dp;        }        // Return the final answer        returndp;    }    // Driver code    publicstaticvoidmain(String[] args) {        intn = 5;        System.out.println("Count of ways is "+ count(n));    }}// code is contributed by shinjanpatra | 
Output :
Count of ways is 3
Time Complexity : O(n) 
Auxiliary Space : O(1)
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.
 
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


 
                                    








