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)
Last Updated :
23 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...