Given a linear board of length N numbered from 1 to N, the task is to find the expected number of moves required to reach the Nth cell of the board, if we start at cell numbered 1 and at each step we roll a cubical dice to decide the next move. Also, we cannot go outside the bounds of the board. Note that the expected number of moves can be fractional.
Examples:
Input: N = 8
Output: 7
p1 = (1 / 6) | 1-step -> 6 moves expected to reach the end
p2 = (1 / 6) | 2-steps -> 6 moves expected to reach the end
p3 = (1 / 6) | 3-steps -> 6 moves expected to reach the end
p4 = (1 / 6) | 4-steps -> 6 moves expected to reach the end
p5 = (1 / 6) | 5-steps -> 6 moves expected to reach the end
p6 = (1 / 6) | 6-steps -> 6 moves expected to reach the end
If we are 7 steps away, then we can end up at 1, 2, 3, 4, 5, 6 steps
away with equal probability i.e. (1 / 6).
Look at the above simulation to understand better.
dp[N – 1] = dp[7]
= 1 + (dp[1] + dp[2] + dp[3] + dp[4] + dp[5] + dp[6]) / 6
= 1 + 6 = 7
Input: N = 10
Output: 7.36111
Approach: This problem can be solved using dynamic programming. To solve the problem, decide the states of the DP first. One way will be to use the distance between the current cell and the Nth cell to define the states of DP. Let’s call this distance X. Thus dp[X] can be defined as the expected number of steps required to reach the end of the board of length X + 1 starting from the 1st cell.
Thus, the recurrence relation becomes:
dp[X] = 1 + (dp[X – 1] + dp[X – 2] + dp[X – 3] + dp[X – 4] + dp[X – 5] + dp[X – 6]) / 6
Now, for the base-cases:
dp[0] = 0
Let’s try to calculate dp[1].
dp[1] = 1 + 5 * (dp[1]) / 6 + dp[0] (Why? its because (5 / 6) is the probability it stays stuck at 1.)
dp[1] / 6 = 1 (since dp[0] = 0)
dp[1] = 6
Similarly, dp[1] = dp[2] = dp[3] = dp[4] = dp[5] = 6
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define maxSize 50
using namespace std;
double dp[maxSize];
int v[maxSize];
double expectedSteps( int x)
{
if (x == 0)
return 0;
if (x <= 5)
return 6;
if (v[x])
return dp[x];
v[x] = 1;
dp[x] = 1 + (expectedSteps(x - 1) +
expectedSteps(x - 2) +
expectedSteps(x - 3) +
expectedSteps(x - 4) +
expectedSteps(x - 5) +
expectedSteps(x - 6)) / 6;
return dp[x];
}
int main()
{
int n = 10;
cout << expectedSteps(n - 1);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int maxSize = 50 ;
static double dp[] = new double [maxSize];
static int v[] = new int [maxSize];
static double expectedSteps( int x)
{
if (x == 0 )
return 0 ;
if (x <= 5 )
return 6 ;
if (v[x] == 1 )
return dp[x];
v[x] = 1 ;
dp[x] = 1 + (expectedSteps(x - 1 ) +
expectedSteps(x - 2 ) +
expectedSteps(x - 3 ) +
expectedSteps(x - 4 ) +
expectedSteps(x - 5 ) +
expectedSteps(x - 6 )) / 6 ;
return dp[x];
}
public static void main (String[] args)
{
int n = 10 ;
System.out.println(expectedSteps(n - 1 ));
}
}
|
Python3
maxSize = 50
dp = [ 0 ] * maxSize
v = [ 0 ] * maxSize
def expectedSteps(x):
if (x = = 0 ):
return 0
if (x < = 5 ):
return 6
if (v[x]):
return dp[x]
v[x] = 1
dp[x] = 1 + (expectedSteps(x - 1 ) +
expectedSteps(x - 2 ) +
expectedSteps(x - 3 ) +
expectedSteps(x - 4 ) +
expectedSteps(x - 5 ) +
expectedSteps(x - 6 )) / 6
return dp[x]
n = 10
print ( round (expectedSteps(n - 1 ), 5 ))
|
C#
using System;
class GFG
{
static int maxSize = 50;
static double []dp = new double [maxSize];
static int []v = new int [maxSize];
static double expectedSteps( int x)
{
if (x == 0)
return 0;
if (x <= 5)
return 6;
if (v[x] == 1)
return dp[x];
v[x] = 1;
dp[x] = 1 + (expectedSteps(x - 1) +
expectedSteps(x - 2) +
expectedSteps(x - 3) +
expectedSteps(x - 4) +
expectedSteps(x - 5) +
expectedSteps(x - 6)) / 6;
return dp[x];
}
public static void Main ()
{
int n = 10;
Console.WriteLine(expectedSteps(n - 1));
}
}
|
Javascript
<script>
var maxSize = 50;
var dp = Array(maxSize);
var v = Array(maxSize);
function expectedSteps(x)
{
if (x == 0)
return 0;
if (x <= 5)
return 6;
if (v[x])
return dp[x];
v[x] = 1;
dp[x] = 1 + (expectedSteps(x - 1) +
expectedSteps(x - 2) +
expectedSteps(x - 3) +
expectedSteps(x - 4) +
expectedSteps(x - 5) +
expectedSteps(x - 6)) / 6;
return dp[x];
}
var n = 10;
document.write( expectedSteps(n - 1).toFixed(5));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient approach: Space optimization O(1)
To optimize space complexity we can use variables instead of arrays to store the states of the DP and determine whether a state has been solved before because in previous approach the current value is dependent upon the previous values stored in array.
Implementation steps:
- Handle base cases: If bis 0, return 0. If x is less than or equal to 5, return 6.
- Initialize the previous values prev1, prev2, prev3, prev4, prev5, and prev6 to 6.
- Initialize the current value curr.
- Iterate from 6 to x (exclusive) to compute the current value based on the previous values.
- Calculate the current value as 1 plus the sum of the previous values divided by 6.
- Update the previous values by shifting their assignments.
- Return the final computed current value as the result.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
double expectedSteps( int x)
{
if (x == 0)
return 0;
if (x <= 5)
return 6;
double prev1 = 6;
double prev2 = 6;
double prev3 = 6;
double prev4 = 6;
double prev5 = 6;
double prev6 = 6;
double curr;
for ( int i = 6; i < x; i++) {
curr = 1 + (prev1 + prev2 + prev3 + prev4 + prev5 + prev6) / 6;
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = prev5;
prev5 = prev6;
prev6 = curr;
}
return curr;
}
int main()
{
int n = 10;
cout << expectedSteps(n - 1);
return 0;
}
|
Java
import java.util.Scanner;
public class GFG {
static double expectedSteps( int x)
{
if (x == 0 )
return 0 ;
if (x <= 5 )
return 6 ;
double prev1 = 6 ;
double prev2 = 6 ;
double prev3 = 6 ;
double prev4 = 6 ;
double prev5 = 6 ;
double prev6 = 6 ;
double curr = 0 ;
for ( int i = 6 ; i < x; i++) {
curr = 1
+ (prev1 + prev2 + prev3 + prev4 + prev5
+ prev6)
/ 6 ;
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = prev5;
prev5 = prev6;
prev6 = curr;
}
return curr;
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = 10 ;
System.out.println(expectedSteps(n - 1 ));
sc.close();
}
}
|
Python3
def expected_steps(x):
if x = = 0 :
return 0
if x < = 5 :
return 6
prev1 = 6
prev2 = 6
prev3 = 6
prev4 = 6
prev5 = 6
prev6 = 6
curr = 0
for i in range ( 6 , x):
curr = 1 + (prev1 + prev2 + prev3 + prev4 + prev5 + prev6) / 6
prev1 = prev2
prev2 = prev3
prev3 = prev4
prev4 = prev5
prev5 = prev6
prev6 = curr
return curr
def main():
n = 10
print (expected_steps(n - 1 ))
if __name__ = = "__main__" :
main()
|
C#
using System;
namespace ExpectedStepsExample
{
class GFG
{
static double ExpectedSteps( int x)
{
if (x == 0)
return 0;
if (x <= 5)
return 6;
double prev1 = 6;
double prev2 = 6;
double prev3 = 6;
double prev4 = 6;
double prev5 = 6;
double prev6 = 6;
double curr = 0;
for ( int i = 6; i < x; i++)
{
curr = 1 + (prev1 + prev2 + prev3 + prev4 + prev5 + prev6) / 6;
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = prev5;
prev5 = prev6;
prev6 = curr;
}
return curr;
}
static void Main( string [] args)
{
int n = 10;
Console.WriteLine(ExpectedSteps(n - 1));
}
}
}
|
Javascript
function expectedSteps(x) {
if (x === 0)
return 0;
if (x <= 5)
return 6;
let prev1 = 6;
let prev2 = 6;
let prev3 = 6;
let prev4 = 6;
let prev5 = 6;
let prev6 = 6;
let curr;
for (let i = 6; i < x; i++) {
curr = 1 + (prev1 + prev2 + prev3 + prev4 + prev5 + prev6) / 6;
prev1 = prev2;
prev2 = prev3;
prev3 = prev4;
prev4 = prev5;
prev5 = prev6;
prev6 = curr;
}
return curr;
}
const n = 10;
console.log(expectedSteps(n - 1));
|
Output:
7.36111
Time Complexity: O(N)
Auxiliary Space: O(1)
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 :
19 Sep, 2023
Like Article
Save Article