Maximize absolute displacement from origin by moving on X | C ++ program for the above approach ; 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 ; Driver Code ; Input ; Function call
#include <iostream> NEW_LINE using namespace std ; int DistRecursion ( string S , int i , int dist ) { if ( i == S . length ( ) ) return abs ( dist ) ; if ( S [ i ] == ' L ' ) return DistRecursion ( S , i + 1 , dist - 1 ) ; if ( S [ i ] == ' R ' ) return DistRecursion ( S , i + 1 , dist + 1 ) ; return max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) ; } int maxDistance ( string S ) { return DistRecursion ( S , 0 , 0 ) ; } int main ( ) { string S = " ? RRR ? " ; cout << maxDistance ( S ) ; return 0 ; }