- Related Questions & Answers
- 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
- Maximum sum such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent - Set 2 in C++
- Maximum sum in circular array such that no two elements are adjacent in C++
- Maximum sum such that no two elements are adjacent Alternate Method in C++ program
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Maximum sum in a 2 x n grid such that no two elements are adjacent in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Python - Make pair from two list such that elements are not same in pairs
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- Find area of triangle if two vectors of two adjacent sides are given using C++
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in C++
- Find a triplet such that sum of two equals to third element in C++
- Program to find minimum cost to paint fences with k different colors in Python

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

We are given n stairs and 2 colors (red and yellow) with which these stairs are to be painted. Our task is to count the number of ways in which we can paint the stairs such that no two consecutive steps will be yellow-colored.

Let’s take an example to understand the problem,

3

5

The ways in which stairs can be painted are YRY, RYR, YRR, RRY, RRR. here R denotes red color, Y denotes yellow color.

To solve this problem, let’s see the number of ways the stairs can be painted.

N = 1, ways(1) = 2 : R, Y

N = 2, ways(2) = 3 : RY, YR, RR

N = 3, ways(3) = 5 : RYR, YRY, RRY, YRR, RRR

N = 4, ways(4) = 8 : YRYR, RYRY, RYRR, YRRY, YRRR, RRYR, RRRR, RRRY.

So from these cases, we can derivate that this is a Fibonacci Series starting with 2 as first element and 3 as second.

Program to illustrate the working of our logic,

#include <iostream> using namespace std; int colorSteps(int n) { int first = 2; int next = 3; for (int i = 3; i <= n; i++) { next = first + next; first = next - first; } return next; } int main(){ int n = 6; cout<<"Number of ways to color "<<n<<" steps is "<<colorSteps(n); return 0; }

Number of ways to color 6 steps is 21

Advertisements