Maximize absolute displacement from origin by moving on X | Java 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
import java . util . * ; class GFG { static int DistRecursion ( String S , int i , int dist ) { char [ ] ch = S . toCharArray ( ) ; if ( i == ch . length ) return Math . abs ( dist ) ; if ( ch [ i ] == ' L ' ) return DistRecursion ( S , i + 1 , dist - 1 ) ; if ( ch [ i ] == ' R ' ) return DistRecursion ( S , i + 1 , dist + 1 ) ; return Math . max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) ; } static int maxDistance ( String S ) { return DistRecursion ( S , 0 , 0 ) ; } public static void main ( String [ ] args ) { String S = " ? RRR ? " ; System . out . print ( maxDistance ( S ) ) ; } }