Maximum Tip Calculator | JavaScript implementation of the approach ; Recursive function to calculate sum of maximum tip order taken by X and Y ; When all orders have been taken ; When X cannot take more orders ; When Y cannot take more orders ; When both can take order calculate maximum out of two ; Driver code
let n ; function solve ( i , X , Y , a , b , n ) { if ( i == n ) return 0 ; if ( X <= 0 ) return b [ i ] + solve ( i + 1 , X , Y - 1 , a , b , n ) ; if ( Y <= 0 ) return a [ i ] + solve ( i + 1 , X - 1 , Y , a , b , n ) ; else return Math . max ( a [ i ] + solve ( i + 1 , X - 1 , Y , a , b , n ) , b [ i ] + solve ( i + 1 , X , Y - 1 , a , b , n ) ) ; } let a = [ 1 , 2 , 3 , 4 , 5 ] ; let b = [ 5 , 4 , 3 , 2 , 1 ] ; n = a . length ; let x = 3 , y = 3 ; document . write ( solve ( 0 , x , y , a , b , n ) ) ;