Skip to content
Related Articles

Related Articles

Improve Article

Maximum Tip Calculator | Set 2

  • Last Updated : 29 Jun, 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 5: 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 naive approach to solve this article is discussed in the previous article.



Efficient Approach: The idea is to use the greedy technique to solve the problem. Sort the N orders in decreasing order on the basis of the difference between Rahul’s tip and Ankit’s tip. Then, traverse all the orders and take the maximum tip of the ith order if it is possible. Follow the steps below to solve the problem:

  • Initialize ans as 0 to store the maximum possible tip.
  • Create a vector of pair of integers, V of size N such that the first and second element of V[i] stores the tip of the ith order of Rahul and Ankit respectively.
  • Sort the vector, V in decreasing order on the basis of the difference between the tips.
  • Traverse the vector, V using the variable i
    • If the value of Y is 0 OR the condition V[i].first ≥ V[i].second holds true, then add the value of V[i].first to ans and decrement X by 1.
    • Otherwise, add the value of V[i].second to ans and decrement Y by 1.
  • After completing the above steps, print the value of the ans as the result.

Below is the implementation of the above approach: 

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Utility Compare function 
// for sorting the array
bool compare(pair<int, int> p1, pair<int, int> p2)
{
    return abs(p1.first - p1.second)
           > abs(p2.first - p2.second);
}
  
// Function to find the maximum possible amount of total
// tip money after processing all the orders
void maxTip(int A[], int B[], int X, int Y, int N)
{
    // Store all the N orders
    vector<pair<int, int> > tips(N);
  
    // Traverse all the orders and add them
    // to the vector, V
    for (int i = 0; i < N; i++) {
        tips[i].first = A[i];
        tips[i].second = B[i];
    }
  
    // Sort the vector in decreasing
      // order of absolute
    // difference of the tips value
    sort(tips.begin(), tips.end(), compare);
  
    // Store the required result
    int ans = 0;
  
    // Traverse all the orders
    for (int i = 0; i < N; i++) {
  
        // Check if Y is 0 or Rahul's tip value
        // is greater than or equal to that of Ankit
        if (Y == 0
            || (X > 0 && tips[i].first >= tips[i].second)) {
  
            // Update the overall result and
            // decrement X by 1
            ans += tips[i].first;
            X--;
        }
  
        else {
  
            // Update the overall result and
            // decrement Y by 1
            ans += tips[i].second;
            Y--;
        }
    }
  
    // Print the result
    cout << ans;
}
  
// Driver Code
int main()
{
    // Given input
    int N = 5, X = 3, Y = 3;
    int A[] = { 1, 2, 3, 4, 5 };
    int B[] = { 5, 4, 3, 2, 1 };
  
    // Function Call
    maxTip(A, B, X, Y, N);
  
    return 0;
}


Output

21

Time Complexity: O(N * log(N))
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :