Find the probability of reaching all points after N moves from point N
Last Updated :
29 Oct, 2023
Given N which denotes the initial position of the person on the number line. Also given L which is the probability of the person of going left. Find the probability of reaching all points on the number line after N moves from point N. Each move can be either to the left or to the right.
Examples:
Input: n = 2, l = 0.5
Output: 0.2500 0.0000 0.5000 0.0000 0.2500
The person can’t reach n-1th position and n+1th position in 2 passes, hence the probability is 0. The person can reach 0th position by only moving 2 steps left from index 2, hence the probability of reaching 0th index is 05*0.5=0.25. Similarly for 2n index, the probability is 0.25.
Input: n = 3, l = 0.1
Output: 0.0010 0.0000 0.0270 0.0000 0.2430 0.0000 0.7290
The person can reach n-1th in three ways, i.e., (llr, lrl, rll) where l denotes left and r denotes right. Hence the probability of n-1th index is 0.027. Similarly probabilities for all other points are also calculated.
Approach: Construct an array arr[n+1][2n+1] where each row represents a pass and the columns represent the points on the line. The maximum a person can move from index N is to 0th index at left or to 2nth index at right. Initially the probabilities after one pass will be left for arr[1][n-1] and right for arr[1][n+1]. The n-1 moves which are left will be done, hence the two possible moves will either be n steps to the right or n steps to the left. So the recurrence relations for right and left moves for all will be:
arr[i][j] += (arr[i – 1][j – 1] * right)
arr[i][j] += (arr[i – 1][j + 1] * left)
The summation of probabilities for all possible moves for any index will be stored in arr[n][i].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void printProbabilities( int n, double left)
{
double right = 1 - left;
double arr[n + 1][2 * n + 1] = {{0}};
arr[1][n + 1] = right;
arr[1][n - 1] = left;
for ( int i = 2; i <= n; i++)
{
for ( int j = 1; j <= 2 * n; j++)
arr[i][j] += (arr[i - 1][j - 1] * right);
for ( int j = 2 * n - 1; j >= 0; j--)
arr[i][j] += (arr[i - 1][j + 1] * left);
}
for ( int i = 0; i < 2*n+1; i++)
printf ( "%5.4f " , arr[n][i]);
}
int main()
{
int n = 2;
double left = 0.5;
printProbabilities(n, left);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void printProbabilities( int n, double left)
{
double right = 1 - left;
double [][] arr = new double [n + 1 ][ 2 * n + 1 ];
arr[ 1 ][n + 1 ] = right;
arr[ 1 ][n - 1 ] = left;
for ( int i = 2 ; i <= n; i++) {
for ( int j = 1 ; j <= 2 * n; j++) {
arr[i][j] += (arr[i - 1 ][j - 1 ] * right);
}
for ( int j = 2 * n - 1 ; j >= 0 ; j--) {
arr[i][j] += (arr[i - 1 ][j + 1 ] * left);
}
}
printArray(arr, n);
}
static void printArray( double [][] arr, int n)
{
for ( int i = 0 ; i < arr[ 0 ].length; i++) {
System.out.printf( "%5.4f " , arr[n][i]);
}
}
public static void main(String[] args)
{
int n = 2 ;
double left = 0.5 ;
printProbabilities(n, left);
}
}
|
Python3
def printProbabilities(n, left):
right = 1 - left;
arr = [[ 0 for j in range ( 2 * n + 1 )]
for i in range (n + 1 )]
arr[ 1 ][n + 1 ] = right;
arr[ 1 ][n - 1 ] = left;
for i in range ( 2 , n + 1 ):
for j in range ( 1 , 2 * n + 1 ):
arr[i][j] + = (arr[i - 1 ][j - 1 ] * right);
for j in range ( 2 * n - 1 , - 1 , - 1 ):
arr[i][j] + = (arr[i - 1 ][j + 1 ] * left);
for i in range ( 2 * n + 1 ):
print ( "{:5.4f} " . format (arr[n][i]), end = ' ' );
if __name__ = = "__main__" :
n = 2 ;
left = 0.5 ;
printProbabilities(n, left);
|
C#
using System;
class GFG
{
static void printProbabilities( int n, double left)
{
double right = 1 - left;
double [,] arr = new double [n + 1,2 * n + 1];
arr[1,n + 1] = right;
arr[1,n - 1] = left;
for ( int i = 2; i <= n; i++)
{
for ( int j = 1; j <= 2 * n; j++)
{
arr[i, j] += (arr[i - 1, j - 1] * right);
}
for ( int j = 2 * n - 1; j >= 0; j--)
{
arr[i, j] += (arr[i - 1, j + 1] * left);
}
}
printArray(arr, n);
}
static void printArray( double [,] arr, int n)
{
for ( int i = 0; i < GetRow(arr,0).GetLength(0); i++)
{
Console.Write( "{0:F4} " , arr[n,i]);
}
}
public static double [] GetRow( double [,] matrix, int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new double [rowLength];
for ( var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
public static void Main(String[] args)
{
int n = 2;
double left = 0.5;
printProbabilities(n, left);
}
}
|
Javascript
function printProbabilities(n, left) {
let right = 1 - left;
let arr = new Array(n + 1);
for (let i = 0; i < n + 1; i++) {
arr[i] = new Array(2 * n + 1).fill(0);
}
arr[1][n + 1] = right;
arr[1][n - 1] = left;
for (let i = 2; i <= n; i++)
{
for (let j = 1; j <= 2 * n; j++) {
arr[i][j] += (arr[i - 1][j - 1] * right);
}
for (let j = 2 * n - 1; j >= 0; j--) {
arr[i][j] += (arr[i - 1][j + 1] * left);
}
}
for (let i = 0; i < 2*n+1; i++) {
console.log(`${arr[n][i].toFixed(4)} `);
}
}
let n = 2;
let left = 0.5;
printProbabilities(n, left);
|
Output
0.2500 0.0000 0.5000 0.0000 0.2500
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Efficient approach : Space optimization
In previous approach the arr[i][j] is depend upon the current and previous row of 2D matrix. So to optimize space we use two vectors curr and dp that keep track of current and previous row of arr.
Implementation Steps:
- Initialize a vectors arr of size 2*N+1 to keep track of only previous row of arr matrix with 0.
- Initialize base case for the condition when person can reach left or right in one move.
- Now iterative over subproblems and get the current computation.
- While Initialize a vectors temp of size 2*N+1 to keep track of only current row of arr matrix with 0.
- At last traverse and print all values of arr.
Implementations Steps:
C++
#include <bits/stdc++.h>
using namespace std;
void printProbabilities( int n, double left)
{
double right = 1 - left;
vector< double > arr(2 * n + 1, 0);
arr[n + 1] = right;
arr[n - 1] = left;
for ( int i = 2; i <= n; i++)
{
vector< double > temp(2 * n + 1, 0);
for ( int j = 1; j <= 2 * n; j++)
temp[j] += (arr[j - 1] * right);
for ( int j = 2 * n - 1; j >= 0; j--)
temp[j] += (arr[j + 1] * left);
arr = temp;
}
for ( int i = 0; i < 2 * n + 1; i++)
printf ( "%5.4f" , arr[i]);
}
int main()
{
int n = 2;
double left = 0.5;
printProbabilities(n, left);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
public class GFG {
static void printProbabilities( int n, double left) {
double right = 1 - left;
double [] arr = new double [ 2 * n + 1 ];
arr[n + 1 ] = right;
arr[n - 1 ] = left;
for ( int i = 2 ; i <= n; i++) {
double [] temp = new double [ 2 * n + 1 ];
for ( int j = 1 ; j <= 2 * n; j++)
temp[j] += (arr[j - 1 ] * right);
for ( int j = 2 * n - 1 ; j >= 0 ; j--)
temp[j] += (arr[j + 1 ] * left);
arr = Arrays.copyOf(temp, temp.length);
}
for ( int i = 0 ; i < 2 * n + 1 ; i++)
System.out.printf( "%5.4f " , arr[i]);
}
public static void main(String[] args) {
int n = 2 ;
double left = 0.5 ;
printProbabilities(n, left);
}
}
|
Python3
def print_probabilities(n, left):
right = 1 - left
arr = [ 0 ] * ( 2 * n + 1 )
arr[n + 1 ] = right
arr[n - 1 ] = left
for i in range ( 2 , n + 1 ):
temp = [ 0 ] * ( 2 * n + 1 )
for j in range ( 1 , 2 * n + 1 ):
temp[j] + = (arr[j - 1 ] * right)
for j in range ( 2 * n - 1 , - 1 , - 1 ):
temp[j] + = (arr[j + 1 ] * left)
arr = temp
for i in range ( 2 * n + 1 ):
print (f "{arr[i]:.4f}" , end = " " )
if __name__ = = "__main__" :
n = 2
left = 0.5
print_probabilities(n, left)
|
C#
using System;
class GFG
{
static void PrintProbabilities( int n, double left)
{
double right = 1 - left;
double [] arr = new double [2 * n + 1];
arr[n + 1] = right;
arr[n - 1] = left;
for ( int i = 2; i <= n; i++)
{
double [] temp = new double [2 * n + 1];
for ( int j = 1; j <= 2 * n; j++)
temp[j] += (arr[j - 1] * right);
for ( int j = 2 * n - 1; j >= 0; j--)
temp[j] += (arr[j + 1] * left);
arr = temp;
}
for ( int i = 0; i < 2 * n + 1; i++)
Console.Write(String.Format( "{0:F4} " , arr[i]));
}
static void Main( string [] args)
{
int n = 2;
double left = 0.5;
PrintProbabilities(n, left);
}
}
|
Javascript
function printProbabilities(n, left) {
let right = 1 - left;
let arr = new Array(2 * n + 1).fill(0);
arr[n + 1] = right;
arr[n - 1] = left;
for (let i = 2; i <= n; i++) {
let temp = new Array(2 * n + 1).fill(0);
for (let j = 1; j <= 2 * n; j++)
temp[j] += arr[j - 1] * right;
for (let j = 2 * n - 1; j >= 0; j--)
temp[j] += arr[j + 1] * left;
arr = [...temp];
}
for (let i = 0; i < 2 * n + 1; i++)
console.log(arr[i].toFixed(4));
}
let n = 2;
let left = 0.5;
printProbabilities(n, left);
|
Output:
0.2500 0.0000 0.5000 0.0000 0.2500
Time Complexity: O(N^2)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...