Given a fence with n posts and k colors, find out the number of ways of painting the fence such that at most 2 adjacent posts have the same color. Since answer can be large return it modulo 10^9 + 7.
Input : n = 2 k = 4 Output : 16 We have 4 colors and 2 posts. Ways when both posts have same color : 4 Ways when both posts have diff color : 4*(choices for 1st post) * 3(choices for 2nd post) = 12 Input : n = 3 k = 2 Output : 6
Following image depicts the 6 possible ways of painting 3 posts with 2 colors:
Consider the following image in which c, c’ and c” are respective colors of posts i, i-1 and i -2.
According to the constraint of the problem, c = c’ = c” is not possible simultaneously, so either c’ != c or c” != c or both. There are k – 1 possibilities for c’ != c and k – 1 for c” != c.
diff = no of ways when color of last two posts is different same = no of ways when color of last two posts is same total ways = diff + sum for n = 1 diff = k, same = 0 total = k for n = 2 diff = k * (k-1) //k choices for first post, k-1 for next same = k //k choices for common color of two posts total = k + k * (k-1) for n = 3 diff = [k + k * (k-1)] * (k-1) (k-1) choices for 3rd post to not have color of 2nd post. same = k * (k-1) c'' != c, (k-1) choices for it Hence we deduce that, total[i] = same[i] + diff[i] same[i] = diff[i-1] diff[i] = (diff[i-1] + diff[i-2]) * (k-1) = total[i-1] * (k-1)
Below is the C++ implementation of the problem:
Space optimization :
We can optimize above solution to use one variable instead of a table.
Below is the the implementation of the problem:
This article is contributed by Aditi Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- In-Place Algorithm
- Jump Pointer Algorithm
- Floyd Warshall Algorithm | DP-16
- Bellman–Ford Algorithm | DP-23
- Reversal algorithm for right rotation of an array
- Distinct elements in subarray using Mo's Algorithm
- Reversal algorithm for array rotation
- C Program for Reversal algorithm for array rotation
- Block swap algorithm for array rotation
- Maximum Subarray Sum using Divide and Conquer algorithm
- Java Program for Reversal algorithm for array rotation
- Manacher's Algorithm - Linear Time Longest Palindromic Substring - Part 1
- Longest sub-sequence of array containing Lucas numbers
- Count array elements that divide the sum of all other elements