# Find the probability of reaching all points after N moves from point N

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-1^{th}position and n+1^{th}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-1^{th}in three ways, i.e., (llr, lrl, rll) where l denotes left and r denotes right. Hence the probability of n-1^{th}index is 0.027. Similarly probabilties 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 0^{th} index at left or to 2n^{th} 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++

`// CPP program to calculate the ` `// probability of reaching all points ` `// after N moves from point N ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate the probabilities ` `void` `printProbabilities(` `int` `n, ` `double` `left) ` `{ ` ` ` `double` `right = 1 - left; ` ` ` ` ` `// Array where row represent the pass and the ` ` ` `// coloumn represents the points on the line ` ` ` `double` `arr[n + 1][2 * n + 1] = {{0}}; ` ` ` ` ` `// Initially the person can reach left ` ` ` `// or right with one move ` ` ` `arr[1][n + 1] = right; ` ` ` `arr[1][n - 1] = left; ` ` ` ` ` `// Calculate probabilities for N-1 moves ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `{ ` ` ` `// when the person moves from ith index in ` ` ` `// right direction when i moves has been done ` ` ` `for` `(` `int` `j = 1; j <= 2 * n; j++) ` ` ` `arr[i][j] += (arr[i - 1][j - 1] * right); ` ` ` ` ` `// when the person moves from ith index in ` ` ` `// left direction when i moves has been done ` ` ` `for` `(` `int` `j = 2 * n - 1; j >= 0; j--) ` ` ` `arr[i][j] += (arr[i - 1][j + 1] * left); ` ` ` `} ` ` ` ` ` `// Print the arr ` ` ` `for` `(` `int` `i = 0; i < 2*n+1; i++) ` ` ` `printf` `(` `"%5.4f "` `, arr[n][i]); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 2; ` ` ` `double` `left = 0.5; ` ` ` `printProbabilities(n, left); ` ` ` `return` `0; ` `} ` ` ` `/* This code is contributed by SujanDutta */` |

*chevron_right*

*filter_none*

## Java

`// Java program to calculate the ` `// probability of reaching all points ` `// after N moves from point N ` `import` `java.util.*; ` `class` `GFG { ` ` ` ` ` `// Function to calculate the probabilities ` ` ` `static` `void` `printProbabilities(` `int` `n, ` `double` `left) ` ` ` `{ ` ` ` `double` `right = ` `1` `- left; ` ` ` ` ` `// Array where row represent the pass and the ` ` ` `// coloumn represents the points on the line ` ` ` `double` `[][] arr = ` `new` `double` `[n + ` `1` `][` `2` `* n + ` `1` `]; ` ` ` ` ` `// Initially the person can reach left ` ` ` `// or right with one move ` ` ` `arr[` `1` `][n + ` `1` `] = right; ` ` ` `arr[` `1` `][n - ` `1` `] = left; ` ` ` ` ` `// Calculate probabilities for N-1 moves ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) { ` ` ` ` ` `// when the person moves from ith index in ` ` ` `// right direction when i moves has been done ` ` ` `for` `(` `int` `j = ` `1` `; j <= ` `2` `* n; j++) { ` ` ` `arr[i][j] += (arr[i - ` `1` `][j - ` `1` `] * right); ` ` ` `} ` ` ` ` ` `// when the person moves from ith index in ` ` ` `// left direction when i moves has been done ` ` ` `for` `(` `int` `j = ` `2` `* n - ` `1` `; j >= ` `0` `; j--) { ` ` ` `arr[i][j] += (arr[i - ` `1` `][j + ` `1` `] * left); ` ` ` `} ` ` ` `} ` ` ` `// Calling function to print the array with probabilities ` ` ` `printArray(arr, n); ` ` ` `} ` ` ` ` ` `// Function that prints the array ` ` ` `static` `void` `printArray(` `double` `[][] arr, ` `int` `n) ` ` ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < arr[` `0` `].length; i++) { ` ` ` `System.out.printf(` `"%5.4f "` `, arr[n][i]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `2` `; ` ` ` `double` `left = ` `0.5` `; ` ` ` `printProbabilities(n, left); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## C#

`// C# program to calculate the ` `// probability of reaching all points ` `// after N moves from point N ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to calculate the probabilities ` ` ` `static` `void` `printProbabilities(` `int` `n, ` `double` `left) ` ` ` `{ ` ` ` `double` `right = 1 - left; ` ` ` ` ` `// Array where row represent the pass and the ` ` ` `// coloumn represents the points on the line ` ` ` `double` `[,] arr = ` `new` `double` `[n + 1,2 * n + 1]; ` ` ` ` ` `// Initially the person can reach left ` ` ` `// or right with one move ` ` ` `arr[1,n + 1] = right; ` ` ` `arr[1,n - 1] = left; ` ` ` ` ` `// Calculate probabilities for N-1 moves ` ` ` `for` `(` `int` `i = 2; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// when the person moves from ith index in ` ` ` `// right direction when i moves has been done ` ` ` `for` `(` `int` `j = 1; j <= 2 * n; j++) ` ` ` `{ ` ` ` `arr[i, j] += (arr[i - 1, j - 1] * right); ` ` ` `} ` ` ` ` ` `// when the person moves from ith index in ` ` ` `// left direction when i moves has been done ` ` ` `for` `(` `int` `j = 2 * n - 1; j >= 0; j--) ` ` ` `{ ` ` ` `arr[i, j] += (arr[i - 1, j + 1] * left); ` ` ` `} ` ` ` `} ` ` ` `// Calling function to print the array with probabilities ` ` ` `printArray(arr, n); ` ` ` `} ` ` ` ` ` `// Function that prints the array ` ` ` `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; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `n = 2; ` ` ` `double` `left = 0.5; ` ` ` `printProbabilities(n, left); ` ` ` `} ` `} ` ` ` `/* This code contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

**Output:**

0.2500 0.0000 0.5000 0.0000 0.2500

**Time Complexity**: O(N^{2})

**Auxiliary Space**: O(N^{2})

## Recommended Posts:

- Probability of reaching a point with 2 or 3 steps at a time
- Find maximum path sum in a 2D matrix when exactly two left moves are allowed
- Puzzle | Find The Probability of Distance in a Square
- Find the probability of a state at a given time in a Markov chain | Set 1
- Find probability of selecting element from kth column after N iterations
- Find probability that a player wins when probabilities of hitting the target are given
- Find number of endless points
- Find maximum points which can be obtained by deleting elements from array
- Minimum Cost Path with Left, Right, Bottom and Up moves allowed
- Probability of rain on N+1th day
- Aptitude | Probability | Question 5
- Aptitude | Probability | Question 6
- Aptitude | Probability | Question 7
- Aptitude | Probability | Question 8
- Probability that two persons will meet

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.