Count number of unique ways to paint a N x 3 grid

Given an integer N, the task is to paint a grid of size N x 3 using colors Red, Yellow, or Green while making such that no pair of adjacent cells has the same color. Print the number of distinct ways in which it is possible
Examples:
Input: N = 1
Output: 12
Explanation:Â
Following 12 possible ways to paint the grid exists:
- Red, Yellow, Red
- Yellow, Red, Yellow
- Green, Red, Yellow
- Red, Yellow, Green
- Yellow, Red, Green
- Green, Red, Green
- Red, Green, Red
- Yellow, Green, Red
- Green, Yellow, Red
- Red, Green, Yellow
- Yellow, Green, Yellow
- Green, Yellow, Green
Input: N = 2
Output: 102
Approach: Follow the steps below to solve the problem:
- Ways to color a row can be split into the following two categories:
- The leftmost and rightmost cells are of the same color.
- The leftmost and rightmost cells are of different colors.
- Considering the first case:
- Six possible ways exist to paint the row such that the leftmost and rightmost colors are of the same.
- For every color occupying both the leftmost and rightmost cell, there exists two different colors with which the middle row can be colored.
- Considering the second case:
- Six possible ways exist to paint the leftmost and rightmost colors are different.
- Three choices for the left cell, two choices for the middle, and fill the rightmost cell with the only remaining color. Therefore, the total number of possibilities is 3*2*1 = 6.
- Now, for the subsequent cells, look at the following example:
- If the previous row is painted as Red Green Red, then there are the following five valid ways to color the current row:
- {Green Red Green}
- {Green Red  Yellow}
- {Green Yellow Green}
- {Yellow Red Green}
- {Yellow Red Yellow}
- From the above observation, it is clear that three possibilities have ends with the same color, and two possibilities have ends with different colors.
- If the previous row is colored Red Green Yellow, the following four possibilities of coloring the current row exists:
- {Green Red Green}
- {Green Yellow Red}
- {Green Yellow Green}
- {Yellow Red Green}
- From the above observation, it is clear that possibilities have ends the same color, and two possibilities have ends with different colors.
- If the previous row is painted as Red Green Red, then there are the following five valid ways to color the current row:
- Therefore, based on the above observations, the following recurrence relation can be defined for the number of ways to paint the N rows:
Count of ways to color current row having ends of same color SN+1 = 3 * SN + 2DN
Count of ways to color current row having ends of different colors DN+1 = 2 * SN + 2DN
Total number of ways to paint all N rows is equal to the sum of SN and DN.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to count the number// of ways to paint N * 3 grid// based on given conditionsvoid waysToPaint(int n){Â
    // Count of ways to pain a    // row with same colored ends    int same = 6;Â
    // Count of ways to pain a row    // with different colored ends    int diff = 6;Â
    // Traverse up to (N - 1)th row    for (int i = 0; i < n - 1; i++) {Â
        // Calculate the count of ways        // to paint the current rowÂ
        // For same colored ends        long sameTmp = 3 * same + 2 * diff;Â
        // For different colored ends        long diffTmp = 2 * same + 2 * diff;Â
        same = sameTmp;        diff = diffTmp;    }Â
    // Print the total number of ways    cout << (same + diff);}Â
// Driver Codeint main(){Â Â Â Â int N = 2;Â Â Â Â waysToPaint(N);}Â
// This code is contributed by ukasp. |
Java
// Java program for the above approachimport java.io.*;import java.lang.*;import java.util.*;Â
class GFG {Â
    // Function to count the number    // of ways to paint N * 3 grid    // based on given conditions    static void waysToPaint(int n)    {Â
        // Count of ways to pain a        // row with same colored ends        long same = 6;Â
        // Count of ways to pain a row        // with different colored ends        long diff = 6;Â
        // Traverse up to (N - 1)th row        for (int i = 0; i < n - 1; i++) {Â
            // Calculate the count of ways            // to paint the current rowÂ
            // For same colored ends            long sameTmp = 3 * same + 2 * diff;Â
            // For different colored ends            long diffTmp = 2 * same + 2 * diff;Â
            same = sameTmp;            diff = diffTmp;        }Â
        // Print the total number of ways        System.out.println(same + diff);    }Â
    // Driver code    public static void main(String[] args)    {Â
        int N = 2;Â
        // Function call        waysToPaint(N);    }} |
Python3
# Python3 program for the above approachÂ
# Function to count the number# of ways to paint N * 3 grid# based on given conditionsÂ
Â
def waysToPaint(n):Â
    # Count of ways to pain a    # row with same colored ends    same = 6Â
    # Count of ways to pain a row    # with different colored ends    diff = 6Â
    # Traverse up to (N - 1)th row    for _ in range(n - 1):Â
        # Calculate the count of ways        # to paint the current rowÂ
        # For same colored ends        sameTmp = 3 * same + 2 * diffÂ
        # For different colored ends        diffTmp = 2 * same + 2 * diffÂ
        same = sameTmp        diff = diffTmpÂ
    # Print the total number of ways    print(same + diff)Â
Â
# Driver CodeÂ
N = 2waysToPaint(N) |
C#
// C# program for the above approachusing System;Â
class GFG {Â
    // Function to count the number    // of ways to paint N * 3 grid    // based on given conditions    static void waysToPaint(int n)    {Â
        // Count of ways to pain a        // row with same colored ends        long same = 6;Â
        // Count of ways to pain a row        // with different colored ends        long diff = 6;Â
        // Traverse up to (N - 1)th row        for (int i = 0; i < n - 1; i++) {Â
            // Calculate the count of ways            // to paint the current rowÂ
            // For same colored ends            long sameTmp = 3 * same + 2 * diff;Â
            // For different colored ends            long diffTmp = 2 * same + 2 * diff;Â
            same = sameTmp;            diff = diffTmp;        }Â
        // Print the total number of ways        Console.WriteLine(same + diff);    }Â
    // Driver code    static public void Main()    {        int N = 2;Â
        waysToPaint(N);    }}Â
// This code is contributed by offbeat |
Javascript
<script>Â
// Javascript program for the above approachÂ
// Function to count the number// of ways to paint N * 3 grid// based on given conditionsfunction waysToPaint(n){         // Count of ways to pain a    // row with same colored ends    var same = 6;Â
    // Count of ways to pain a row    // with different colored ends    var diff = 6;Â
    // Traverse up to (N - 1)th row    for(var i = 0; i < n - 1; i++)    {                 // Calculate the count of ways        // to paint the current rowÂ
        // For same colored ends        var sameTmp = 3 * same + 2 * diff;Â
        // For different colored ends        var diffTmp = 2 * same + 2 * diff;                 same = sameTmp;          diff = diffTmp;    }Â
    // Print the total number of ways    document.write(same + diff);}Â
// Driver codevar N = 2;Â
// Function callwaysToPaint(N);Â Â Â Â Â Â Â Â Â // This code is contributed by Khushboogoyal499Â
</script> |
12
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



