Find the probability of reaching all points after N moves from point N | Java program to calculate the probability of reaching all points after N moves from point N ; Function to calculate the probabilities ; Array where row represent the pass and the column represents the points on the line ; Initially the person can reach left or right with one move ; Calculate probabilities for N - 1 moves ; when the person moves from ith index in right direction when i moves has been done ; when the person moves from ith index in left direction when i moves has been done ; Calling function to print the array with probabilities ; Function that prints the array ; Driver Code
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 ) ; } }