Given n stairs and we have 2 colour yellow and green the task is that we have to paint given stairs by given colour with condition is that we cannot paints two yellow steps directly after each other.

**Examples :**

Input : n = 1 Output : 2 A single stair can be colored either as green or yellow. Input : n = 3 Output : 5

Case 1: When we have 1 stair, we can paint either yellow or green.

Case 2: When we have 2 stairs, we can paint first stair by either yellow or green but for next stair we can only paint by green because we cannot paint two yellow steps directly after each other. So total cases are three YG, GG, GY.

Case 3: When we have 3 stairs then we can paint it by in 5 ways.

If we take a closer look, we can notice that it follows Fibonacci Series.

## C++

`// C++ Program to find the number of ways to paint stairs ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the number of ways ` `int` `ways(` `int` `n) ` `{ ` ` ` `int` `W[n + 1]; ` ` ` ` ` `// take base case for 1 and 2 ` ` ` `W[1] = 2; ` ` ` `W[2] = 3; ` ` ` ` ` `for` `(` `int` `i = 3; i <= n; i++) ` ` ` `W[i] = W[i - 1] + W[i - 2]; ` ` ` ` ` `return` `W[n]; ` `} ` ` ` `// Driven code ` `int` `main() ` `{ ` ` ` `int` `n = 3; ` ` ` `printf` `(` `"%d"` `, ways(n)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// java Program to find the number of ` `// ways to paint stairs ` `import` `java.io.*; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to find the number of ways ` ` ` `static` `int` `ways(` `int` `n) ` ` ` `{ ` ` ` `int` `[]W = ` `new` `int` `[n+` `1` `]; ` ` ` ` ` `// take base case for 1 and 2 ` ` ` `W[` `1` `] = ` `2` `; ` ` ` `W[` `2` `] = ` `3` `; ` ` ` ` ` `for` `(` `int` `i = ` `3` `; i <= n; i++) ` ` ` `W[i] = W[i - ` `1` `] + W[i - ` `2` `]; ` ` ` ` ` `return` `W[n]; ` ` ` `} ` ` ` ` ` `// Driven code ` ` ` `static` `public` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `System.out.println(ways(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to find the number ` `# of ways to paint stairs ` ` ` `# Function to find the number of ways ` `def` `ways( n ): ` ` ` `W ` `=` `list` `() ` ` ` ` ` `# take base case for 1 and 2 ` ` ` `W.append(` `0` `) ` ` ` `W.append(` `2` `) ` ` ` `W.append(` `3` `) ` ` ` ` ` `i ` `=` `3` ` ` `while` `i <` `=` `n: ` ` ` `W.append(W[i ` `-` `1` `] ` `+` `W[i ` `-` `2` `]) ` ` ` `i ` `=` `i ` `+` `1` ` ` ` ` `return` `W[n] ` ` ` `# Driver code ` `n ` `=` `3` `print` `(ways(n)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the number of ` `// ways to paint stairs ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to find the number of ways ` ` ` `static` `int` `ways(` `int` `n) ` ` ` `{ ` ` ` `int` `[]W =` `new` `int` `[n+1]; ` ` ` ` ` `// take base case for 1 and 2 ` ` ` `W[1] = 2; ` ` ` `W[2] = 3; ` ` ` ` ` `for` `(` `int` `i = 3; i <= n; i++) ` ` ` `W[i] = W[i - 1] + W[i - 2]; ` ` ` ` ` `return` `W[n]; ` ` ` `} ` ` ` ` ` `// Driven code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 3; ` ` ` ` ` `Console.WriteLine(ways(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find the ` `// number of ways to paint stairs ` ` ` `// Function to find the ` `// number of ways ` `function` `ways(` `$n` `) ` `{ ` ` ` ` ` `// take base case ` ` ` `// for 1 and 2 ` ` ` `$W` `[1] = 2; ` ` ` `$W` `[2] = 3; ` ` ` ` ` `for` `(` `$i` `= 3; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `$W` `[` `$i` `] = ` `$W` `[` `$i` `- 1] + ` ` ` `$W` `[` `$i` `- 2]; ` ` ` ` ` `return` `$W` `[` `$n` `]; ` `} ` ` ` `// Driven code ` `$n` `= 3; ` `echo` `ways(` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

5

**Time Complexity :** O(n)

**Extra Space :** O(n)

We can solve this problem in O(Log n) time also using matrix exponentiation solution for n-th Fibonacci Number.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Ways to paint N paintings such that adjacent paintings don't have same colors
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Number of ways to paint K cells in 3 x N grid such that no P continuous columns are left unpainted
- Ways to color a 3*N board using 4 colors
- Permutation of Array such that sum of adjacent elements are not divisible by 3
- Ways of dividing a group into two halves such that two elements are in different groups
- Number of ways to form an array with distinct adjacent elements
- Maximum sum such that no two elements are adjacent
- Rearrange characters in a string such that no two adjacent are same
- Permutations of string such that no two vowels are adjacent
- Number of ways to merge two arrays such retaining order
- Total number of ways to place X and Y at n places such that no two X are together
- Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Longest subsequence such that adjacent elements have at least one common digit
- Partition first N natural number into two sets such that their sum is not coprime
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Number of ways to arrange a word such that all vowels occur together

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.