Maximize absolute displacement from origin by moving on X | Recursive function to find the maximum absolute displacement from origin by performing the given set of moves ; If i is equal to N ; If S [ i ] is equal to ' L ' ; If S [ i ] is equal to ' R ' ; If S [ i ] is equal to ' ? ' ; Function to find the maximum absolute displacement from the origin ; Return the maximum absolute displacement ; Input ; Function call
function DistRecursion ( S , i , dist ) { if ( i == S . length ) return Math . abs ( dist ) ; if ( S [ i ] == ' ' ) return DistRecursion ( S , i + 1 , dist - 1 ) ; if ( S [ i ] == ' ' ) return DistRecursion ( S , i + 1 , dist + 1 ) ; return Math . max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) ; } function maxDistance ( S ) { return DistRecursion ( S , 0 , 0 ) ; } let S = " " ; document . write ( maxDistance ( S ) ) ;