- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Number of Ways to Paint N × 3 Grid in C++

Suppose we have grid of size n x 3 and we want to paint each cell of the grid with exactly one of the three colors. The colors are Red, Yellow or Green. Now there is a constraint that is no two adjacent cells have the same color. We have n the number of rows of the grid. We have to find the number of ways we can paint this grid. The answer may be very large so return it modulo 10^9 + 7.

So, if the input is like 1, then the output will be 12

To solve this, we will follow these steps −

m = 1^9 + 7

Define a function add(), this will take a, b,

return ((a mod m) + (b mod m)) mod m

From the main method do the following −

a123 := 6, a121 = 6

for initialize i := 2, when i <= n, update (increase i by 1), do −

b121 := add(3 * a121, 2 * a123)

b123 := add(2 * a121, 2 * a123)

a121 := b121

a123 := b123

return add(a123, a121)

Let us see the following implementation to get better understanding −

## Example

#include <bits/stdc++.h> using namespace std; typedef long long int lli; const lli mod = 1e9 + 7; class Solution { public: lli add(lli a, lli b){ return ((a % mod) + (b % mod)) % mod; } int numOfWays(int n){ lli a123 = 6, a121 = 6; lli b123, b121; for (int i = 2; i <= n; i++) { b121 = add(3 * a121, 2 * a123); b123 = add(2 * a121, 2 * a123); a121 = b121; a123 = b123; } return add(a123, a121); } }; main(){ Solution ob; cout << (ob.numOfWays(3)); }

## Input

3

## Output

246

- Related Articles
- Number of Ways to Paint N × 3 Grid in C++ program
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C++
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C programming
- Find the Number of Ways to go From One Point to Another in a Grid using C++
- C++ program to find out the number of ways a grid with boards can be colored
- C++ program to count in how many ways we can paint blocks with two conditions
- Find the Number of Ways to Traverse an N-ary Tree using C++
- Cost of painting n * m grid in C++
- Ways to paint stairs with two colors such that two adjacent are not yellow in C++
- Count of different ways to express N as the sum of 1, 3 and 4 in C++
- Program to count the number of ways to distribute n number of candies in k number of bags in Python
- Count number of ways to divide a number in parts in C++
- Program to count number of ways we can throw n dices in Python
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
- Count number of ways to jump to reach end in C++