Number of Ways to Stay in the Same Place After Some Steps in C++


Suppose there is an array of size arrLen, and we also have a pointer at index 0 in that array. At each step, we can move 1 position to the left, 1 position to the right in the array or stay in the same place.

Now suppose we have two integers steps and arrLen, we have to find the number of ways such that the pointer still at index 0 after exactly steps. If the answer is very large then return it modulo 10^9 + 7.

So, if the input is like steps = 3, arrLen = 2, then the output will be 4, as there are 4 different ways to stay at index 0 after 3 steps. These are [Right, Left, Stay], [Stay, Right, Left], [Right, Stay, Left], [Stay, Stay, Stay].

To solve this, we will follow these steps −

  • m := 1e9 + 7

  • Define a function add(), this will take a, b,

  • return (a mod m + b mod m) mod m

  • Define one 2D array dp

  • Define a function solve(), this will take n, x, pos initialize it with 0,

  • if x is same as 0, then −

    • return true when pos is same as 0

  • if dp[pos, n] is not equal to -1, then −

    • return dp[pos, n]

  • ans := 0

  • if pos > 0, then

    • ans := add(ans, solve(n, x - 1, pos - 1))

  • if pos < n - 1, then −

    • ans := add(ans, solve(n, x - 1, pos + 1))

  • ans := add(ans, solve(n, x - 1, pos))

  • dp[pos, n] := ans

  • return ans

  • From the main method do the following −

  • x := minimum of arrLen and steps / 2 + 1

  • dp := Define one 2D array of size 2 x (x + 1), fill this with 0

  • dp[0, 0] := 1

  • n := arrLen

  • for initialize i := 1, when i <= steps, update (increase i by 1), do −

    • for initialize j := 0, when j < minimum of arrLen and step/2 + 1, update (increase j by 1), do −

      • x := (i - 1) mod 2

      • y := i mod 2

      • dp[y, j] := dp[x, j]

      • if j - 1 >= 0, then −

        • dp[y, j] := add(dp[y, j], dp[x, j - 1])

      • if j + 1 < n, then −

        • dp[y, j] := add(dp[y, j], dp[x, j + 1])

  • return dp[steps mod 2, 0]

Let us see the following implementation to get better understanding −

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
const int MOD = 1e9 + 7;
lli add(lli a, lli b){
   return (a % MOD + b % MOD) % MOD;
}
class Solution {
   public:
   vector<vector<int> > dp;
   int solve(int n, int x, int pos = 0){
      if (x == 0) {
         return pos == 0;
      }
      if (dp[pos][n] != -1)
      return dp[pos][n];
      int ans = 0;
      if (pos > 0)
      ans = add(ans, solve(n, x - 1, pos - 1));
      if (pos < n - 1)
      ans = add(ans, solve(n, x - 1, pos + 1));
      ans = add(ans, solve(n, x - 1, pos));
      dp[pos][n] = ans;
      return ans;
   }
   int numWays(int steps, int arrLen){
      int x = min(arrLen, steps / 2 + 1);
      this->dp = vector<vector<int> >(2, vector<int>(x + 1, 0));
      dp[0][0] = 1;
      int n = arrLen;
      for (int i = 1; i <= steps; i++) {
         for (int j = 0; j < min(arrLen, steps / 2 + 1); j++) {
            int x = (i - 1) % 2;
            int y = i % 2;
            dp[y][j] = dp[x][j];
            if (j - 1 >= 0)
            dp[y][j] = add(dp[y][j], dp[x][j - 1]);
            if (j + 1 < n)
            dp[y][j] = add(dp[y][j], dp[x][j + 1]);
         }
      }
      return dp[steps % 2][0];
   }
};
main(){
   Solution ob;
   cout << (ob.numWays(3,2));
}

Input

3, 2

Output

4

Updated on: 08-Jun-2020

167 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements