


Consider If N = 2, then there can be 4 different ways to color fences such that at most 2 adjacent fences have the same color-:
[ [0, 1],
[1, 0],
[1, 1],
[0, 0] ]
Hence, the answer is 4.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case contains a single integer ‘N’, representing the number of fences.
For each test case, print a single integer denoting the number of ways to paint the fences modulo 10^9 + 7.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^6
Time Limit: 1 sec
The idea is to use a recursive approach to find the total number of ways to paint the fences. Let us consider the two colors are black and white.
We have to think about two possibilities:
In this approach, we are going to make a recursive function countWays(N). This function will return the total number of ways to paint N fences.
As discussed in the previous approach, we are using a recursive approach to find the total number of ways to paint the fences. In this approach, we will use memoization to reduce our time complexity by storing the solutions in the dynamic array. We have to maintain an array dpArray of size N+1. If we had already calculated the ways for a particular value of N then we can return dpArray[N] in that case instead of calculating the total number of ways again.
We will maintain two variables prev and curr, to find the total number of ways to paint the fences. We will initialize prev and curr as 2. We will traverse ind from 2 to N.
In the end, the variable curr stores the number of ways to paint N fences. We will return the variable curr.