Open In App

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:

  1. Red, Yellow, Red
  2. Yellow, Red, Yellow
  3. Green, Red, Yellow
  4. Red, Yellow, Green
  5. Yellow, Red, Green
  6. Green, Red, Green
  7. Red, Green, Red
  8. Yellow, Green, Red
  9. Green, Yellow, Red
  10. Red, Green, Yellow
  11. Yellow, Green, Yellow
  12. Green, Yellow, Green

Input: N = 2
Output: 102



Approach: Follow the steps below to solve the problem:

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++ 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 conditions
void 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 Code
int main()
{
    int N = 2;
    waysToPaint(N);
}
 
// This code is contributed by ukasp.




// Java program for the above approach
import 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 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 = 2
waysToPaint(N)




// C# program for the above approach
using 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




<script>
 
// Javascript program for the above approach
 
// Function to count the number
// of ways to paint  N * 3 grid
// based on given conditions
function 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 code
var N = 2;
 
// Function call
waysToPaint(N);
         
// This code is contributed by Khushboogoyal499
 
</script>

Output: 
12

 

Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :