raghuvanshraj

Untitled

Sep 5th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. static int mod=1000000007;
  2.     // Complete the countArray function below.
  3.     static long countArray(int n, int k, int x) {
  4.         // Return the number of ways to fill in the array.
  5.           if(n==3)
  6.           {
  7.               if(x==1)
  8.               {
  9.                   return 2;
  10.               }
  11.               else
  12.               return 3;
  13.           }
  14.           long[][]dp=new long[n-2][2];
  15.  
  16.           for(int i=dp.length-1;i>=0;i--)
  17.           {
  18.               if(i==0)
  19.               {
  20.                    dp[i][0]=0;
  21.         dp[i][1]=(  
  22.             (((k-1)*(dp[i+1][0]%mod))%mod)%mod +
  23.                        (((dp[i+1][1]%mod)*(k-2))%mod)%mod     )%mod;
  24.               }
  25.               else if(i==dp.length-1)
  26.               {
  27.                   if(x!=1)
  28.                   {
  29.                       dp[i][0]=1;
  30.                       dp[i][1]=(k-2);
  31.                   }
  32.                   else
  33.                   {
  34.                       dp[i][0]=0;
  35.                       dp[i][1]=(k-1);
  36.                   }
  37.               }
  38.               else
  39.               {
  40.                    dp[i][0]=dp[i+1][1]%mod;
  41.         dp[i][1]=(  
  42.             (((k-1)*(dp[i+1][0]%mod))%mod)%mod +
  43.                        (((dp[i+1][1]%mod)*(k-2))%mod)%mod     )%mod;
  44.               }
  45.           }
  46.           return dp[0][1]%mod;    
  47.     }
Add Comment
Please, Sign In to add comment