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.
Input: n = 2, l = 0.5
Output: 0.2500 0.0000 0.5000 0.0000 0.2500
The person can 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 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 0th index at left or to 2nth index at right. Initially the probabilities after one pass will be right for arr[n-1] and left for arr[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:
0.2500 0.0000 0.5000 0.0000 0.2500
Time Complexity: O(N2)
Auxiliary Space: O(N2)
- Probability of reaching a point with 2 or 3 steps at a time
- Reaching a point using clockwise or anticlockwise movements
- Collect maximum points in an array with k moves
- Minimum time to reach a point with +t and -t moves at time t
- Find minimum moves to reach target on an infinite line
- Find the minimum number of moves needed to move from one cell of matrix to another
- 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 an index of maximum occurring element with equal probability
- Find Corners of Rectangle using mid points
- Find number of endless points
- Steps required to visit M points in order on a circular ring of N points
- Minimum number of points to be removed to get remaining points on one side of axis
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Find the other end point of a line with given one end and mid
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.