Maximum Tip Calculator | C ++ program for the above approach ; Function that finds the maximum tips from the given arrays as per the given conditions ; Base Condition ; If both have non - zero count then return max element from both array ; Traverse first array , as y count has become 0 ; Traverse 2 nd array , as x count has become 0 ; Drive Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumTip ( vector < int > & arr1 , vector < int > & arr2 , int n , int x , int y ) { if ( n == 0 ) return 0 ; if ( x != 0 and y != 0 ) return max ( arr1 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x - 1 , y ) , arr2 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x , y - 1 ) ) ; if ( y == 0 ) return arr1 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x - 1 , y ) ; else return arr2 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x , y - 1 ) ; } int main ( ) { int N = 5 ; int X = 3 ; int Y = 3 ; vector < int > A = { 1 , 2 , 3 , 4 , 5 } ; vector < int > B = { 5 , 4 , 3 , 2 , 1 } ; cout << ( maximumTip ( A , B , N , X , Y ) ) ; }