- 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++ program
Suppose we have a grid whose size is n x 3 and we want to paint every cell of the grid with exactly one of the three colors. Here the colors that will be used are Red, Yellow and Green.
Now there is a constraint, that is no two adjacent cells have the same color. We have n number of rows of the grid. Finally, 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 = 10^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)
Example
Let us see the following implementation to get better understanding −
#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++
- C++ program to find out the number of ways a grid with boards can be colored
- 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
- C++ program to count in how many ways we can paint blocks with two conditions
- Find the Number of Ways to go From One Point to Another in a Grid using C++
- Program to count number of ways we can throw n dices in Python
- Program to count the number of ways to distribute n number of candies in k number of bags in Python
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
- C++ Program to find out the number of illuminated cells in a grid
- C++ Program to find out the number of border cells in a grid
- Program to find number of ways we can get n R.s using Indian denominations in Python
- 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++
