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++
#include <bits/stdc++.h>
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));
}
|
Java
import java.io.*;
class GFG
{
static int maximumTip( int []arr1, int []arr2,
int n, int x, int y)
{
if (n == 0 )
return 0 ;
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 ));
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 );
}
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 };
System.out.println(maximumTip(A, B, N, X, Y));
}
}
|
Python3
def maximumTip(arr1, arr2, n, x, 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 )
N = 5
X = 3
Y = 3
A = [ 1 , 2 , 3 , 4 , 5 ]
B = [ 5 , 4 , 3 , 2 , 1 ]
print (maximumTip(A, B, N, X, Y))
|
C#
using System;
public class GFG
{
static int maximumTip( int []arr1, int []arr2,
int n, int x, int y)
{
if (n == 0)
return 0;
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));
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);
}
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 };
Console.WriteLine(maximumTip(A, B, N, X, Y));
}
}
|
Javascript
<script>
function maximumTip(arr1, arr2, n, x, y) {
if (n == 0)
return 0;
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));
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);
}
let N = 5;
let X = 3;
let Y = 3;
let A = [1, 2, 3, 4, 5];
let B = [5, 4, 3, 2, 1];
document.write(maximumTip(A, B, N, X, Y));
</script>
|
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 <bits/stdc++.h>
using namespace std;
int dp[1001][101][101];
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
import java.io.*;
import java.util.HashMap;
class GFG {
static int maximumTip( int [] arr1, int [] arr2, int n,
int x, int y,
HashMap<String, Integer> dd)
{
String key = Integer.toString(n) + "_"
+ Integer.toString(x) + "_"
+ Integer.toString(y);
if (dd.get(key) != null )
return dd.get(key);
if (n == 0 )
return 0 ;
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 dd.get(key);
}
if (y == 0 ) {
int temp = arr1[n - 1 ]
+ maximumTip(arr1, arr2, n - 1 ,
x - 1 , y, dd);
dd.put(key, temp);
return dd.get(key);
}
else {
int temp = arr2[n - 1 ]
+ maximumTip(arr1, arr2, n - 1 , x,
y - 1 , dd);
dd.put(key, temp);
return dd.get(key);
}
}
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 };
HashMap<String, Integer> dd
= new HashMap<String, Integer>();
System.out.println(maximumTip(A, B, N, X, Y, dd));
}
}
|
Python3
def maximumTip(arr1, arr2, n, x, y, dd):
key = str (n) + '_' + str (x) + '_' + str (y)
if key in dd:
return dd[key]
if n = = 0 :
return 0
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 dd[key]
if y = = 0 :
dd[key] = arr1[n - 1 ] + maximumTip(arr1, arr2, n - 1 , x - 1 , y, dd)
return dd[key]
else :
dd[key] = arr2[n - 1 ] + maximumTip(arr1, arr2, n - 1 , x, y - 1 , dd)
return dd[key]
N = 5
X = 3
Y = 3
A = [ 1 , 2 , 3 , 4 , 5 ]
B = [ 5 , 4 , 3 , 2 , 1 ]
dd = {}
print (maximumTip(A, B, N, X, Y, dd))
|
Javascript
<script>
function maximumTip(arr1, arr2, n, x, y, dd) {
key = `${n}_${x}_${y}`;
for ( var key in dd) {
return dd[key];
}
if (n == 0) {
return 0;
}
if (x != 0 && y != 0) {
dd[key] = 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)
);
return dd[key];
}
if (y == 0)
{
dd[key] = arr1[n - 1] + maximumTip(arr1, arr2, n - 1, x - 1, y, dd);
return dd[key];
}
else {
dd[key] = arr2[n - 1] + maximumTip(arr1, arr2, n - 1, x, y - 1, dd);
return dd[key];
}
}
let N = 5;
let X = 3;
let Y = 3;
let A = [1, 2, 3, 4, 5];
let B = [5, 4, 3, 2, 1];
dd = {};
document.write(maximumTip(A, B, N, X, Y, dd));
</script>
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int maximumTip( int [] arr1, int [] arr2, int n,
int x, int y,
Dictionary< string , int > dd)
{
string key = n + "_"
+ x + "_"
+ y;
if (dd.ContainsKey(key))
return dd[key];
if (n == 0)
return 0;
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[key] = temp;
return dd[key];
}
if (y == 0) {
int temp = arr1[n - 1]
+ maximumTip(arr1, arr2, n - 1,
x - 1, y, dd);
dd[key] = temp;
return dd[key];
}
else {
int temp = arr2[n - 1]
+ maximumTip(arr1, arr2, n - 1, x,
y - 1, dd);
dd[key] = temp;
return dd[key];
}
}
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 };
Dictionary< string , int > dd
= new Dictionary< string , int >();
Console.WriteLine(maximumTip(A, B, N, X, Y, dd));
}
}
|
Time Complexity: O(N*X*Y)
Auxiliary Space: O(N*X*Y)
Last Updated :
15 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...