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 the answer can be large return it modulo 10^9 + 7.
Examples:
Input : n = 2 k = 4
Output : 16
Explanation: 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
The 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 the 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 possibility 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 + same
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 = (previous total ways) * (k - 1)
(k + k * (k - 1) * (k - 1)
same = previous diff ways
k * (k-1)
Hence we deduce that,
total[i] = same[i] + diff[i]
same[i] = diff[i-1]
diff[i] = total[i-1] * (k-1)
Below is the implementation of the problem:
C++
#include <bits/stdc++.h>
using namespace std;
long countWays( int n, int k)
{
long dp[n + 1];
memset (dp, 0, sizeof (dp));
long long mod = 1000000007;
dp[1] = k;
dp[2] = k * k;
for ( int i = 3; i <= n; i++) {
dp[i] = ((k - 1) * (dp[i - 1] + dp[i - 2])) % mod;
}
return dp[n];
}
int main()
{
int n = 3, k = 2;
cout << countWays(n, k) << endl;
return 0;
}
|
Java
import java.util.*;
class GfG {
static long countWays( int n, int k)
{
long dp[] = new long [n + 1 ];
Arrays.fill(dp, 0 );
int mod = 1000000007 ;
dp[ 1 ] = k;
int same = 0 , diff = k;
for ( int i = 2 ; i <= n; i++) {
same = diff;
diff = ( int )(dp[i - 1 ] * (k - 1 ));
diff = diff % mod;
dp[i] = (same + diff) % mod;
}
return dp[n];
}
public static void main(String[] args)
{
int n = 3 , k = 2 ;
System.out.println(countWays(n, k));
}
}
|
Python3
def countWays(n, k):
dp = [ 0 ] * (n + 1 )
total = k
mod = 1000000007
dp[ 1 ] = k
dp[ 2 ] = k * k
for i in range ( 3 ,n + 1 ):
dp[i] = ((k - 1 ) * (dp[i - 1 ] + dp[i - 2 ])) % mod
return dp[n]
n = 3
k = 2
print (countWays(n, k))
|
C#
using System;
public class GFG
{
static long countWays( int n, int k)
{
long [] dp = new long [n + 1];
Array.Fill(dp, 0);
int mod = 1000000007;
dp[1] = k;
int same = 0, diff = k;
for ( int i = 2; i <= n; i++)
{
same = diff;
diff = ( int )(dp[i - 1] * (k - 1));
diff = diff % mod;
dp[i] = (same + diff) % mod;
}
return dp[n];
}
static public void Main ()
{
int n = 3, k = 2;
Console.WriteLine(countWays(n, k));
}
}
|
Javascript
<script>
function countWays(n, k)
{
let dp = new Array(n + 1);
dp.fill(0);
let mod = 1000000007;
dp[1] = k;
let same = 0, diff = k;
for (let i = 2; i <= n; i++)
{
same = diff;
diff = (dp[i - 1] * (k - 1));
diff = diff % mod;
dp[i] = (same + diff) % mod;
}
return dp[n];
}
let n = 3, k = 2;
document.write(countWays(n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Space optimization :
We can optimize the above solution to use one variable instead of a table.
Below is the implementation of the problem:
C++
#include <bits/stdc++.h>
using namespace std;
long countWays( int n, int k)
{
long total = k;
int mod = 1000000007;
int same = 0, diff = k;
for ( int i = 2; i <= n; i++) {
same = diff;
diff = total * (k - 1);
diff = diff % mod;
total = (same + diff) % mod;
}
return total;
}
int main()
{
int n = 3, k = 2;
cout << countWays(n, k) << endl;
return 0;
}
|
Java
class GFG {
static long countWays( int n, int k)
{
long total = k;
int mod = 1000000007 ;
int same = 0 , diff = k;
for ( int i = 2 ; i <= n; i++) {
same = diff;
diff = ( int )total * (k - 1 );
diff = diff % mod;
total = (same + diff) % mod;
}
return total;
}
public static void main(String[] args)
{
int n = 3 , k = 2 ;
System.out.println(countWays(n, k));
}
}
|
Python3
def countWays(n, k) :
total = k
mod = 1000000007
same, diff = 0 , k
for i in range ( 2 , n + 1 ) :
same = diff
diff = total * (k - 1 )
diff = diff % mod
total = (same + diff) % mod
return total
if __name__ = = "__main__" :
n, k = 3 , 2
print (countWays(n, k))
|
C#
using System;
class GFG {
static long countWays( int n, int k)
{
long total = k;
int mod = 1000000007;
long same = 0, diff = k;
for ( int i = 2; i <= n; i++) {
same = diff;
diff = total * (k - 1);
diff = diff % mod;
total = (same + diff) % mod;
}
return total;
}
static void Main()
{
int n = 3, k = 2;
Console.Write(countWays(n, k));
}
}
|
PHP
<?php
function countWays( $n , $k )
{
$total = $k ;
$mod = 1000000007;
$same = 0;
$diff = $k ;
for ( $i = 2; $i <= $n ; $i ++)
{
$same = $diff ;
$diff = $total * ( $k - 1);
$diff = $diff % $mod ;
$total = ( $same + $diff ) % $mod ;
}
return $total ;
}
$n = 3;
$k = 2;
echo countWays( $n , $k ) . "\n" ;
?>
|
Javascript
<script>
function countWays(n, k)
{
let total = k;
let mod = 1000000007;
let same = 0, diff = k;
for (let i = 2; i <= n; i++) {
same = diff;
diff = total * (k - 1);
diff = diff % mod;
total = (same + diff) % mod;
}
return total;
}
let n = 3, k = 2;
document.write(countWays(n, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 May, 2023
Like Article
Save Article