

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
<p style="">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.</p><p>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.</p><p>So, if the input is like 1, then the output will be 12</p><p style=""><img src="https://www.tutorialspoint.com/assets/questions/media/40380/number_of_ways.jpg" class="fr-fic fr-dib" style="width: 418px; height: 160.233px;" width="418" height="160.233"></p><p>To solve this, we will follow these steps −</p><ul class="list"><li><p>m = 10^9 + 7</p></li><li><p>Define a function add(), this will take a, b,</p></li><li><p>return ((a mod m) + (b mod m)) mod m</p></li><li><p>From the main method do the following −</p></li><li><p>a123 := 6, a121 = 6</p></li><li><p>for initialize i := 2, when i −= n, update (increase i by 1), do −</p><ul class="list"><li><p>b121 := add(3 * a121, 2 * a123)</p></li><li><p>b123 := add(2 * a121, 2 * a123)</p></li><li><p>a121 := b121</p></li><li><p>a123 := b123</p></li></ul></li><li><p>return add(a123, a121)</p></li></ul><h2>Example </h2><p>Let us see the following implementation to get better understanding −</p><p><a class="demo" href="http://tpcg.io/QE02KbYK" rel="nofollow" target="_blank"> Live Demo</a></p><pre class="prettyprint notranslate" style="">#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)); }</pre><h2>Input</h2><pre class="result notranslate">3</pre><h2>Output</h2><pre class="result notranslate">246</pre>
- Related Questions & Answers
- Number of Ways to Paint N × 3 Grid in C++
- Program to count number of ways we can fill 3 x n box with 2 x 1 dominos in Python
- 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 find sum of series 1 + 2 + 2 + 3 + 3 + 3 + .. + n 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++
- C++ program to count in how many ways we can paint blocks with two conditions
- Ways to paint N paintings such that adjacent paintings don’t have same colors in C programming
- Count of different ways to express N as the sum of 1, 3 and 4 in C++
- Program to find number of ways we can get n R.s using Indian denominations in Python
- Find the Number of Ways to go From One Point to Another in a Grid using C++
- C++ program to find the sum of the series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- 3 ways to initialize an object in Java
- Find length of a string in python (3 ways)
Advertisements