Maximum Tip Calculator

• Last Updated : 02 Dec, 2021

Rahul and Ankit are the only two waiters in the Royal Restaurant. Today, the restaurant received N orders. The amount of tips may differ when handled by different waiters and given as arrays A[] and B[] such that if Rahul takes the ith Order, he would be tipped A[i] rupees, and if Ankit takes this order, the tip would be B[i] rupees.

In order to maximize the total tip value, they decided to distribute the order among themselves. One order will be handled by one person only. Also, due to time constraints, Rahul cannot take more than X orders and Ankit cannot take more than Y orders. It is guaranteed that X + Y is greater than or equal to N, which means that all the orders can be handled by either Rahul or Ankit. The task is to find out the maximum possible amount of total tip money after processing all the orders.

Examples:

Input: N = 5, X = 3, Y = 3, A[] = {1, 2, 3, 4, 5}, B[] = {5, 4, 3, 2, 1}
Output: 21
Explanation:
Step 1: 5 is included from Ankit’s array
Step 2: 4 is included from Ankit’s array
Step 3: As both of them has same value 3 then choose any one of them
Step 4: 4 is included from Rahul’s array
Step 4: 5 is included from Rahul’s array
Therefore, the maximum possible amount of total tip money sums up to 21.

Input: N = 7, X = 3, Y = 4, A[] = {8, 7, 15, 19, 16, 16, 18}, B[] = {1, 7, 15, 11, 12, 31, 9}
Output: 110

Naive Approach: The simplest approach is to traverse the given arrays and start traversing both the arrays simultaneously and pick the maximum element among them and reduce the count of X if the element is taken from X else the count of Y. If one of the X or Y becomes 0, traverse other non-zero array and add its value to the maximum profit. As in every step, there is a choice to be made, this is similar to the 0-1 Knapsack Problem, in which decisions are made whether to include or exclude an element.

Below is the implementation of the above approach:

C++

 // C++ program for the above approach#include using namespace std; // Function that finds the maximum tips// from the given arrays as per the// given conditionsint maximumTip(vector &arr1,vector & arr2,               int n, int x, int y){         // Base Condition    if (n == 0)        return 0;             // If both have non-zero count then    // return max element from both array    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));     // Traverse first array, as y    // count has become 0    if (y == 0)        return arr1[n - 1] + maximumTip(arr1, arr2, n - 1,                                                    x - 1, y);                                                         // Traverse 2nd array, as x    // count has become 0    else        return arr2[n - 1] + maximumTip(arr1, arr2, n - 1,                                                 x, y - 1);} // Drive Codeint main(){    int N = 5;    int X = 3;    int Y = 3;         vector A = { 1, 2, 3, 4, 5 };    vector B = { 5, 4, 3, 2, 1 };         // Function Call    cout << (maximumTip(A, B, N, X, Y));} // This code is contributed by mohit kumar 29

Java

 /*package whatever //do not write package name here */import java.io.*; class GFG{     // Function that finds the maximum tips// from the given arrays as per the// given conditionsstatic int maximumTip(int []arr1,int []arr2,               int n, int x, int y){         // Base Condition    if (n == 0)        return 0;             // If both have non-zero count then    // return max element from both array    if (x != 0 && y != 0)        return Math.max(            arr1[n - 1] + maximumTip(arr1, arr2, n - 1,                                                 x - 1, y),            arr2[n - 1] + maximumTip(arr1, arr2, n - 1, x,                                                 y - 1));     // Traverse first array, as y    // count has become 0    if (y == 0)        return arr1[n - 1] + maximumTip(arr1, arr2, n - 1,                                                    x - 1, y);                                                         // Traverse 2nd array, as x    // count has become 0    else        return arr2[n - 1] + maximumTip(arr1, arr2, n - 1,                                                 x, y - 1);} // Drive Code    public static void main (String[] args) {       int N = 5;    int X = 3;    int Y = 3;         int A[] = { 1, 2, 3, 4, 5 };    int B[] = { 5, 4, 3, 2, 1 };         // Function Call                     System.out.println(maximumTip(A, B, N, X, Y));    }}     // This code is contributed by Potta Lokesh

Python3

 # Python program for the above approach # Function that finds the maximum tips# from the given arrays as per the# given conditionsdef maximumTip(arr1, arr2, n, x, y):     # Base Condition    if n == 0:        return 0     # If both have non-zero count then    # return max element from both array    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)            )     # Traverse first array, as y    # count has become 0    if y == 0:        return arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y)     # Traverse 2nd array, as x    # count has become 0    else:        return arr2[n - 1] + maximumTip(arr1, arr2, n-1, x, y-1)  # Drive CodeN = 5X = 3Y = 3A = [1, 2, 3, 4, 5]B = [5, 4, 3, 2, 1] # Function Callprint(maximumTip(A, B, N, X, Y))

Javascript


Output
21

Time Complexity: O(2N)
Auxiliary Space: O(1)

Efficient Approach: The above approach can be optimized by using Dynamic Programming and Memoization. If execution is traced for the values of N, X, Y, it can be seen that are there are Overlapping Subproblems. These overlapping subproblems can be computed once and stored and used when the same subproblem is called in the recursive call. Below are the steps:

• Initialize a Map/Dictionary to store the overlapping subproblems result. The keys of the map will be combined values of N, X, and Y.
• At each recursive call, check if a given key is present in the map then return the value from the map itself.
• Else, call the function recursively and store the value in the map and return the stored value.
• If X and Y are non-zero, recursively call function and take the maximum of the value returned when X is used and when Y is used.
• If X or Y is zero, recursively call for the non-zero array.
• After the above recursive calls end, then print the maximum possible amount of tip calculated.

Below is the implementation of the above approach:

C++

 #include using namespace std;int dp;int rec(int level, int x, int y, int arr1[], int arr2[],        int n){    if (level == n)        return 0;    if (x == 0 && y == 0)        return 0;    if (x == 0)        return arr2[level]               + rec(level + 1, x, y - 1, arr1, arr2, n);    if (y == 0)        return arr1[level]               + rec(level + 1, x - 1, y, arr1, arr2, n);    if (dp[level][x][y] != -1)        return dp[level][x][y];    int ans = max(rec(level + 1, x - 1, y, arr1, arr2, n)                      + arr1[level],                  rec(level + 1, x, y - 1, arr1, arr2, n)                      + arr2[level]);    return dp[level][x][y] = ans;} void solve(){    int n = 7, x = 3, y = 4;    int arr1[] = { 8, 7, 15, 19, 16, 16, 18 },        arr2[] = { 1, 7, 15, 11, 12, 31, 9 };    memset(dp, -1, sizeof(dp));    cout << rec(0, x, y, arr1, arr2, n);}int main(){    solve();    return 0;}

Java

 // Java program for the above approachimport java.io.*;import java.util.HashMap; class GFG {     // Function that finds the maximum tips    // from the given arrays as per the    // given conditions    static int maximumTip(int[] arr1, int[] arr2, int n,                          int x, int y,                          HashMap dd)    {        // Create key of N, X, Y        String key = Integer.toString(n) + "_"                     + Integer.toString(x) + "_"                     + Integer.toString(y);               // Return if the current state is        // already calculated        if (dd.get(key) != null)            return dd.get(key);        // Base Condition        if (n == 0)            return 0;         // If both have non-zero count then store and        // return max element from both array        if (x != 0 && y != 0) {            int temp = Math.max(                arr1[n - 1]                    + maximumTip(arr1, arr2, n - 1, x - 1,                                 y, dd),                arr2[n - 1]                    + maximumTip(arr1, arr2, n - 1, x,                                 y - 1, dd));            dd.put(key, temp);            // Return the current state result            return dd.get(key);        }         // if y is zero, only x value        // can be used        if (y == 0) {            int temp = arr1[n - 1]                       + maximumTip(arr1, arr2, n - 1,                                    x - 1, y, dd);            dd.put(key, temp);            // Return the current state result            return dd.get(key);        }         // if x is zero, only y value        // can be used        else {            int temp = arr2[n - 1]                       + maximumTip(arr1, arr2, n - 1, x,                                    y - 1, dd);            dd.put(key, temp);            // Return the current state result            return dd.get(key);        }    }     // Driver Code    public static void main(String[] args)    {        int N = 5;        int X = 3;        int Y = 3;         int A[] = { 1, 2, 3, 4, 5 };        int B[] = { 5, 4, 3, 2, 1 };               // Stores the results of the        // overlapping state        HashMap dd            = new HashMap();               // Function Call        System.out.println(maximumTip(A, B, N, X, Y, dd));    }} // This code is contributed by MuskanKalra1

Python3

 # Python program for the above approach  # Function that finds the maximum tips# from the given arrays as per the# given conditionsdef maximumTip(arr1, arr2, n, x, y, dd):     # Create key of N, X, Y    key = str(n) + '_' + str(x) + '_' + str(y)     # Return if the current state is    # already calculated    if key in dd:        return dd[key]     # Base Condition    if n == 0:        return 0     # Store and return    if x != 0 and y != 0:        dd[key] = max(            arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y, dd),            arr2[n-1] + maximumTip(arr1, arr2, n-1, x, y-1, dd)        )         # Return the current state result        return dd[key]     # If y is zero, only x value    # can be used    if y == 0:        dd[key] = arr1[n-1] + maximumTip(arr1, arr2, n-1, x-1, y, dd)         # Return the current state result        return dd[key]     # If x is zero, only y value    # can be used    else:         dd[key] = arr2[n-1] + maximumTip(arr1, arr2, n-1, x, y-1, dd)         # Return the current state result        return dd[key]  # Drive CodeN = 5X = 3Y = 3A = [1, 2, 3, 4, 5]B = [5, 4, 3, 2, 1] # Stores the results of the# overlapping statedd = {} # Function Callprint(maximumTip(A, B, N, X, Y, dd))

Javascript


Output
21

Time Complexity: O(N*X*Y)
Auxiliary Space: O(N*X*Y)

My Personal Notes arrow_drop_up