Painting the Fence | Practice | GeeksforGeeks

Given a fence with n posts and k colors, find out the number of ways of painting the fence so that not more than two consecutive fences have the same colors. Since the answer can be large return it modulo 10^9 + 7.

Example 1:
Input:
N=3,  K=2 
Output: 6
Explanation: 
We have following possible combinations:

 
Example 2:
Input:
N=2,  K=4
Output: 16


Your Task:
Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countWays() that takes n and k as parameters and returns the number of ways in which the fence can be painted.(modulo 109 + 7)
 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
 
Constraints:
1 ≤ N ≤ 5000
1 ≤ K ≤ 100
 
0
lindan1231 week ago
int mod = 1e9 + 7;
    long long countWays(int n, int k){
        long long int dp[n+1];
        //because when 1 fence only
        if(n==1)
        {
            return k;
        }
        
        else if(n==2)
        {
            return k*k;
        }
        dp[1] = k;
        dp[2] = k*k;
        for(long long int i=3;i<=n;i++)
        {
            dp[i] = ((k-1)*((dp[i-1]%mod)+ (dp[i-2])%mod)%mod)%mod;
        }
        return dp[n]%mod;
    }
Time Taken : 0.02
Cpp
0
mukulbansal4213 weeks ago
0
aryanrot2343 weeks ago
Idk why my code is not working for other test cases , i'm getting TLE 
 
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at Array.main(File.java:53)
 
#My code
 long countWays(int n,int k)
    {
        if(n==1) return k;
        if(n==0) return 0;
        if(k==0) return 0;
        
        long same = k*1;
        long diff = k*(k-1);
        long total = same+diff;
        
        for(int i = 3;i<=n;i++){
            same = diff*1;
            diff = total*(k-1);
            total = same+diff;
        }
        return total%1000000007;
    }
+1
jainmuskan5653 weeks ago
 long long countWays(int n, int k){       /*  we will think of this as taking count of 2 types of ways           same= in which last 2 fences are of same color           diff= last 2 are of different color           total= same+diff           we iterate over the number of fences and change           the value of same, total and diff accordingly                      for each next iteration the value of           same = diff*1 (all the different values and their last element is decided)           diff = total *(k-1) (all the values then choose the color not taken already)           total= same+diff       */       // only 1 fence number of ways would be k       if(n==1){           return k;       }       long long same,diff,total;       long long mod= 1e9+7;       same= k%mod;       diff= (k*(k-1))%mod;       total= (same+diff)%mod;       for(int i=3;i<=n;i++){           same= (diff)%mod;           diff= ((total)%mod *(k-1))%mod;           total= ((diff)%mod+(same)%mod)%mod;       }       return (total)%mod;   }
0
khushiaggarwal09021 month ago
long long countWays(int n, int k)   {     long long mod=1e9+7;          if(n==0)     return 0;          if(n==1)     return k%mod;               long long same=k%mod;     long long diff= (k*(k-1))%mod;          for(long long  i=3;i<=n;i++)     {         long long prev=diff%mod;        diff=((same+prev)*(k-1))%mod;          same=prev%mod;     }          return (same+diff)%mod;        }
0
ankitghanghas10721 month ago
long long countWays(int n, int k){       // code here       int inf = 1e9 + 7;       long long dp[n+1];       dp[1] = k;       dp[2] = k*k;       for(int i = 3; i<=n; i++){           dp[i] = ((k-1)*((dp[i-1] + dp[i-2])%inf))%inf;       }       return dp[n];   }
+4
aloksinghbais022 months ago
C++ solution having time complexity as O(N) and space complexity as O(1) is as follows :- 
 
Execution Time :- 0.0 / 1.1 sec
 
long long countWays(int n, int k){       if(n == 1) return (k);       int mod = (int)1e9 + 7;       long long int same = k * 1;       long long int diff = (k * (k-1)) % mod;       long long int total = (same + diff) % mod;              for(int i = 3; i <= n; i++){           same = diff * 1;           diff = (total * (k-1)) % mod;           total = (same + diff) % mod;       }              return total;   }
0
bhargav_412 months ago
Java Solution | TC - O(N) | SC - O(1)
 
class Solution
{
    long countWays(int n,int k)
    {
        if(n==1) return k;
        long last_two_same;
        long last_two_different;
        long total;
        
        last_two_same = (long)k;
        last_two_different = (long)k*((long)(k-1));
        total = last_two_same + last_two_different;
        
        int mod = 1000000007;
        
        for(int i=3;i<=n;i++){
            last_two_same = last_two_different%mod;
            last_two_different = ((total%mod)*(long)(k-1))%mod;
            total = (last_two_different%mod + last_two_same%mod)%mod;
        }
        
        return total%mod;
    }
}
0
gauravkmr01102 months ago
long long countWays(int n, int k){              long long dp[n+1];       dp[0]=0;       dp[1]=k;       dp[2]=k*k;              int m = 1e9+7;         for(int i =3 ; i<=n ; i++){           dp[i]=((dp[i-2]+dp[i-1])*(k-1))%m;       }              return dp[n];          }
0
uttarandas5012 months ago
long long countWays(int n, int k){
        // code here
        
        int mod = 1e9+7;
        long long a = 1, b = k, c;
        if(n==1){return k;}
        if(n==2){return b*(k-1);}
        else{
            for(int i=3; i<=n; i++){
                c = ((a+b)*(k-1))%mod;
                a = b;
                b = c;
            }
        }
        return (b*k)%mod;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.