# Maximum Tip Calculator | Set 2

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 **i ^{th}** 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:21Explanation:

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 **i ^{th}** 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**i**order of Rahul and Ankit respectively.^{th} - 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**.

- If the value of
- After completing the above steps, print 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;` `}` |

## 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)