Select numbers in such way to maximize the amount of money
Last Updated :
16 Jun, 2022
Given two arrays A1 and A2 of N numbers. There are two people A and B who select numbers out of N. If A selects i-th number, then he will be paid A1[i] amount of money and if B selects i-th number then he will be paid A2[i] amount of money but A cannot select more than X numbers and B cannot select more than Y numbers. The task is to select N numbers in such a way that the amount of total money is maximized at the end.
Note: X + Y >= N
Examples:
Input: N = 5, X = 3, Y = 3
A1[] = {1, 2, 3, 4, 5},
A2= {5, 4, 3, 2, 1}
Output: 21
B will take the first 3 orders and A
will take the last two orders.
Input: N = 2, X = 1, Y = 1
A1[] = {10, 10}, A2= {20, 20}
Output: 30
Approach: Let us create a new array C such that C[i] = A2[i] – A1[i]. Now we will sort the array C in decreasing order. Note that the condition X + Y >= N guarantees that we will be able to assign the number to any one of the persons. Assume that for some i, A1[i] > A2[i] and you assigned an order to B, the loss encountered due to this assignment is C[i]. Similarly, for some i, A2[i] > A1[i] and you assigned a number to A, the loss encountered due to this assignment is C[i]. As we want to minimize the loss encountered, it is better to process the numbers having high possible losses, because we can try to reduce the loss in the starting part. There is no point in selecting a number with a high loss after assigning a number with less loss. Hence we initially assign all numbers to A initially, then subtract the loss from them greedily. Once the assigned order number is under X, then we store the maximal of that.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maximize( int A1[], int A2[], int n,
int x, int y)
{
int c[n];
int sum = 0;
for ( int i = 0; i < n; i++) {
c[i] = A2[i] - A1[i];
sum += A1[i];
}
sort(c, c + n, greater< int >());
int maxi = -1;
for ( int i = 0; i < n; i++) {
sum += c[i];
if (i + 1 >= (n - x))
maxi = max(sum, maxi);
}
return maxi;
}
int main()
{
int A1[] = { 1, 2, 3, 4, 5 };
int A2[] = { 5, 4, 3, 2, 1 };
int n = 5;
int x = 3, y = 3;
cout << maximize(A1, A2, n, x, y);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int maximize( int A1[], int A2[], int n,
int x, int y)
{
int [] c = new int [n];
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
c[i] = A2[i] - A1[i];
sum += A1[i];
}
int temp;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (c[i] < c[i+ 1 ])
{
temp = c[i];
c[i] = c[i + 1 ];
c[i + 1 ] = temp;
}
}
int maxi = - 1 ;
for ( int i = 0 ; i < n; i++)
{
sum += c[i];
if (i + 1 >= (n - x))
maxi = Math.max(sum, maxi);
}
return maxi;
}
public static void main(String args[])
{
int A1[] = { 1 , 2 , 3 , 4 , 5 };
int A2[] = { 5 , 4 , 3 , 2 , 1 };
int n = 5 ;
int x = 3 , y = 3 ;
System.out.println(maximize(A1, A2, n, x, y));
}
}
|
Python3
def maximize(A1, A2, n, x, y):
c = [ 0 for i in range (n)]
Sum = 0
for i in range (n):
c[i] = A2[i] - A1[i]
Sum + = A1[i]
c.sort()
c = c[:: - 1 ]
maxi = - 1
for i in range (n):
Sum + = c[i]
if (i + 1 > = (n - x)):
maxi = max ( Sum , maxi)
return maxi
A1 = [ 1 , 2 , 3 , 4 , 5 ]
A2 = [ 5 , 4 , 3 , 2 , 1 ]
n = 5
x, y = 3 , 3
print (maximize(A1, A2, n, x, y))
|
C#
using System;
class GFG
{
static int maximize( int [] A1, int [] A2, int n,
int x, int y)
{
int [] c = new int [n];
int sum = 0;
for ( int i = 0; i < n; i++)
{
c[i] = A2[i] - A1[i];
sum += A1[i];
}
int temp;
for ( int i = 0; i < n - 1; i++)
{
if (c[i] < c[i+1])
{
temp = c[i];
c[i] = c[i + 1];
c[i + 1] = temp;
}
}
int maxi = -1;
for ( int i = 0; i < n; i++)
{
sum += c[i];
if (i + 1 >= (n - x))
maxi = Math.Max(sum, maxi);
}
return maxi;
}
public static void Main()
{
int [] A1 = { 1, 2, 3, 4, 5 };
int [] A2 = { 5, 4, 3, 2, 1 };
int n = 5;
int x = 3, y = 3;
Console.WriteLine(maximize(A1, A2, n, x, y));
}
}
|
PHP
<?php
function maximize( $A1 , $A2 , $n , $x , $y )
{
# Array to store the loss
$c = array ();
# Initial sum
$sum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$c [ $i ] = $A2 [ $i ] - $A1 [ $i ];
$sum += $A1 [ $i ];
}
rsort( $c );
$maxi = -1;
for ( $i = 0; $i < $n ; $i ++)
{
$sum += $c [ $i ];
if ( $i + 1 >= ( $n - $x ))
$maxi = max( $sum , $maxi );
}
return $maxi ;
}
# Driver Code
$A1 = array ( 1, 2, 3, 4, 5 );
$A2 = array ( 5, 4, 3, 2, 1 );
$n = 5;
$x = 3;
$y = 3;
echo maximize( $A1 , $A2 , $n , $x , $y );
?>
|
Javascript
<script>
function maximize(A1, A2, n,
x, y)
{
let c = Array(n).fill(0);
let sum = 0;
for (let i = 0; i < n; i++)
{
c[i] = A2[i] - A1[i];
sum += A1[i];
}
let temp;
for (let i = 0; i < n - 1; i++)
{
if (c[i] < c[i+1])
{
temp = c[i];
c[i] = c[i + 1];
c[i + 1] = temp;
}
}
let maxi = -1;
for (let i = 0; i < n; i++)
{
sum += c[i];
if (i + 1 >= (n - x))
maxi = Math.max(sum, maxi);
}
return maxi;
}
let A1 = [ 1, 2, 3, 4, 5 ];
let A2 = [ 5, 4, 3, 2, 1 ];
let n = 5;
let x = 3, y = 3;
document.write(maximize(A1, A2, n, x, y));
</script>
|
Time Complexity: O(N*logN), as we are using an inbuilt sort function that costs N*logN time.
Auxiliary Space: O(N), as we are using extra space of the array c. Where N is the number of elements in the arrays.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...