Open In App

Maximum Tip Calculator | Set 2

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.*;
 
class Pair{
    int x, y;
    Pair(int a, int b){
        x = a;
        y = b;
    }
}
 
class GFG {
    public static void main (String[] args) {
        // 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
        int ans = maxTip(A, B, N, X, Y);
         
        System.out.println(ans);
    }
     
    static int maxTip(int[] a, int[] b, int n, int x, int y) {
        // list to store pairs
        ArrayList<Pair> tips = new ArrayList<>();
         
        // adding tip values to list
        for(int i = 0; i < n; i++){
            tips.add(new Pair(a[i], b[i]));
        }
         
        // sorting list by absolute difference
        Collections.sort(tips, (a1, b1)-> {
            return Math.abs(b1.x - b1.y) - Math.abs(a1.x - a1.y) ;
        });
         
        // System.out.println(tips);
         
        int ans = 0;
        // traversing the list
        for(int i = 0; i < n; i++){
            Pair p = tips.get(i);
             
            // if first should be considered
            if(y == 0 || (p.x >= p.y && x > 0)){
                ans += p.x;
                x--;
            }
            else if(y > 0) {
                ans += p.y;
                y--;
            }
        }
         
        return ans;
    }
}


Python3




from functools import cmp_to_key
 
# Utility Compare function
# for sorting the array
def compare(p1, p2):
    return abs(p2[0] - p2[1]) - abs(p1[0] - p1[1])
 
# Function to find the maximum possible amount of total
# tip money after processing all the orders
def maxTip(A, B, X, Y, N):
    # Store all the N orders
    tips = []
 
    # Traverse all the orders and add them
    # to the list, tips
    for i in range(N):
        tips.append((A[i], B[i]))
 
    # Sort the list in decreasing
    # order of absolute
    # difference of the tips value
    tips.sort(key=lambda x: -abs(x[0] - x[1]))
 
    # Store the required result
    ans = 0
 
    # Traverse all the orders
    for i in range(N):
 
        # Check if Y is 0 or Rahul's tip value
        # is greater than or equal to that of Ankit
        if Y == 0 or (X > 0 and tips[i][0] >= tips[i][1]):
 
            # Update the overall result and
            # decrement X by 1
            ans += tips[i][0]
            X -= 1
 
        else:
 
            # Update the overall result and
            # decrement Y by 1
            ans += tips[i][1]
            Y -= 1
 
    # Print the result
    print(ans)
 
# Driver Code
 
# Given input
N, X, Y = 5, 3, 3
A = [1, 2, 3, 4, 5]
B = [5, 4, 3, 2, 1]
 
# Function Call
maxTip(A, B, X, Y, N)
 
# This code is contributed by phasing17.


C#




using System;
using System.Collections.Generic;
using System.Linq;
 
class Pair
{
    public int x, y;
    public Pair(int a, int b)
    {
        x = a;
        y = b;
    }
}
 
class GFG
{
    static void Main(string[] args)
    {
        // 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
        int ans = maxTip(A, B, N, X, Y);
 
        Console.WriteLine(ans);
    }
 
    static int maxTip(int[] a, int[] b, int n, int x, int y)
    {
        // list to store pairs
        List<Pair> tips = new List<Pair>();
 
        // adding tip values to list
        for (int i = 0; i < n; i++)
        {
            tips.Add(new Pair(a[i], b[i]));
        }
 
        // sorting list by absolute difference
        tips.Sort((a1, b1) => Math.Abs(b1.x - b1.y) - Math.Abs(a1.x - a1.y));
 
        int ans = 0;
        // traversing the list
        for (int i = 0; i < n; i++)
        {
            Pair p = tips[i];
 
            // if first should be considered
            if (y == 0 || (p.x >= p.y && x > 0))
            {
                ans += p.x;
                x--;
            }
            else if (y > 0)
            {
                ans += p.y;
                y--;
            }
        }
 
        return ans;
    }
}


Javascript




<script>
 
// JavaScript program for the above approach
 
 
// Utility Compare function
// for sorting the array
function compare(p1, p2)
{
    return Math.abs(p2[0] - p2[1]) - Math.abs(p1[0] - p1[1]);
}
 
// Function to find the maximum possible amount of total
// tip money after processing all the orders
function maxTip(A, B, X, Y, N)
{
    // Store all the N orders
    let tips = new Array(N);
 
    // Traverse all the orders and add them
    // to the vector, V
    for (let i = 0; i < N; i++) {
        tips[i] = new Array();
        tips[i][0] = A[i];
        tips[i][1] = B[i];
    }
 
    // Sort the vector in decreasing
    // order of absolute
    // difference of the tips value
    tips.sort(compare);
 
    // Store the required result
    let ans = 0;
 
    // Traverse all the orders
    for (let 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][0] >= tips[i][1])) {
 
            // Update the overall result and
            // decrement X by 1
            ans += tips[i][0];
            X--;
        }
 
        else {
 
            // Update the overall result and
            // decrement Y by 1
            ans += tips[i][1];
            Y--;
        }
    }
 
    // Print the result
    document.write(ans,"</br>");
}
 
// Driver Code
 
// Given input
let N = 7, X = 3, Y = 4;
let A = [8, 7, 15, 19, 16, 16, 18];
let B = [1, 7, 15, 11, 12, 31, 9];
 
 
// Function Call
maxTip(A, B, X, Y, N);
 
// This code is contributed by shinjanpatra.
</script>


Output

21

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



Last Updated : 13 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads