Maximum profit by selling N items at two markets
Last Updated :
22 Dec, 2022
Given two arrays, A[] and B[] each of length N where A[i] and B[i] are the prices of the ith item when sold in market A and market B respectively. The task is to maximize the profile of selling all the N items, but there is a catch: if you went to market B then you can not return. For example, if you sell the first k items in market A and you have to sell the rest of the items in market B.
Examples:
Input: A[] = {2, 3, 2}, B[] = {10, 3, 40}
Output: 53
Sell all the items in market B in order to
maximize the profit i.e. (10 + 3 + 40) = 53.
Input: A[] = {7, 5, 3, 4}, B[] = {2, 3, 1, 3}
Output: 19
Approach:
- Create a prefix sum array preA[] where preA[i] will store the profit when the items A[0…i] are sold in market A.
- Create a suffix sum array suffB[] where suffB[i] will store the profit when item B[i…n-1] is sold in market B.
- Now the problem is reduced to finding an index i such that (preA[i] + suffB[i + 1]) is the maximum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProfit( int profitA[], int profitB[], int n)
{
int preSum[n];
preSum[0] = profitA[0];
for ( int i = 1; i < n; i++) {
preSum[i] = preSum[i - 1] + profitA[i];
}
int suffSum[n];
suffSum[n - 1] = profitB[n - 1];
for ( int i = n - 2; i >= 0; i--) {
suffSum[i] = suffSum[i + 1] + profitB[i];
}
int res = preSum[n - 1];
for ( int i = 1; i < n - 1; i++) {
res = max(res, preSum[i] + suffSum[i + 1]);
}
res = max(res, suffSum[0]);
return res;
}
int main()
{
int profitA[] = { 2, 3, 2 };
int profitB[] = { 10, 30, 40 };
int n = sizeof (profitA) / sizeof ( int );
cout << maxProfit(profitA, profitB, n);
return 0;
}
|
Java
class GFG
{
static int maxProfit( int profitA[], int profitB[], int n)
{
int preSum[] = new int [n];
preSum[ 0 ] = profitA[ 0 ];
for ( int i = 1 ; i < n; i++)
{
preSum[i] = preSum[i - 1 ] + profitA[i];
}
int suffSum[] = new int [n];
suffSum[n - 1 ] = profitB[n - 1 ];
for ( int i = n - 2 ; i >= 0 ; i--)
{
suffSum[i] = suffSum[i + 1 ] + profitB[i];
}
int res = preSum[n - 1 ];
for ( int i = 1 ; i < n - 1 ; i++)
{
res = Math.max(res, preSum[i] + suffSum[i + 1 ]);
}
res = Math.max(res, suffSum[ 0 ]);
return res;
}
public static void main (String[] args)
{
int profitA[] = { 2 , 3 , 2 };
int profitB[] = { 10 , 30 , 40 };
int n = profitA.length;
System.out.println(maxProfit(profitA, profitB, n));
}
}
|
Python3
def maxProfit(profitA, profitB, n) :
preSum = [ 0 ] * n;
preSum[ 0 ] = profitA[ 0 ];
for i in range ( 1 , n) :
preSum[i] = preSum[i - 1 ] + profitA[i];
suffSum = [ 0 ] * n;
suffSum[n - 1 ] = profitB[n - 1 ];
for i in range (n - 2 , - 1 , - 1 ) :
suffSum[i] = suffSum[i + 1 ] + profitB[i];
res = preSum[n - 1 ];
for i in range ( 1 , n - 1 ) :
res = max (res, preSum[i] + suffSum[i + 1 ]);
res = max (res, suffSum[ 0 ]);
return res;
if __name__ = = "__main__" :
profitA = [ 2 , 3 , 2 ];
profitB = [ 10 , 30 , 40 ];
n = len (profitA);
print (maxProfit(profitA, profitB, n));
|
C#
using System;
class GFG
{
static int maxProfit( int []profitA,
int []profitB, int n)
{
int []preSum = new int [n];
preSum[0] = profitA[0];
for ( int i = 1; i < n; i++)
{
preSum[i] = preSum[i - 1] + profitA[i];
}
int []suffSum = new int [n];
suffSum[n - 1] = profitB[n - 1];
for ( int i = n - 2; i >= 0; i--)
{
suffSum[i] = suffSum[i + 1] + profitB[i];
}
int res = preSum[n - 1];
for ( int i = 1; i < n - 1; i++)
{
res = Math.Max(res, preSum[i] +
suffSum[i + 1]);
}
res = Math.Max(res, suffSum[0]);
return res;
}
public static void Main(String[] args)
{
int []profitA = { 2, 3, 2 };
int []profitB = { 10, 30, 40 };
int n = profitA.Length;
Console.WriteLine(maxProfit(profitA, profitB, n));
}
}
|
Javascript
<script>
function maxProfit(profitA, profitB, n) {
let preSum = new Array(n);
preSum[0] = profitA[0];
for (let i = 1; i < n; i++) {
preSum[i] = preSum[i - 1] + profitA[i];
}
let suffSum = new Array(n);
suffSum[n - 1] = profitB[n - 1];
for (let i = n - 2; i >= 0; i--) {
suffSum[i] = suffSum[i + 1] + profitB[i];
}
let res = preSum[n - 1];
for (let i = 1; i < n - 1; i++) {
res = Math.max(res, preSum[i] + suffSum[i + 1]);
}
res = Math.max(res, suffSum[0]);
return res;
}
let profitA = [2, 3, 2];
let profitB = [10, 30, 40];
let n = profitA.length;
document.write(maxProfit(profitA, profitB, n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Alternate Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int maxProfit(vector< int > a, vector< int > b, int n)
{
int maxP = -1;
for ( int i = 0; i < n + 1; i++) {
int sumA = 0;
for ( int j = 0; j < min(i, ( int )a.size()); j++)
sumA += a[j];
int sumB = 0;
for ( int j = i; j < b.size(); j++)
sumB += b[j];
maxP = max(maxP, sumA + sumB);
}
return maxP;
}
int main()
{
vector< int > a = { 2, 3, 2 };
vector< int > b = { 10, 30, 40 };
cout << maxProfit(a, b, 4);
return 0;
}
|
Java
class GFG {
static int maxProfit( int [] a, int [] b, int n)
{
int maxP = - 1 ;
for ( int i = 0 ; i < n + 1 ; i++) {
int sumA = 0 ;
for ( int j = 0 ; j < Math.min(i, a.length); j++)
sumA += a[j];
int sumB = 0 ;
for ( int j = i; j < b.length; j++)
sumB += b[j];
maxP = Math.max(maxP, sumA + sumB);
}
return maxP;
}
public static void main(String args[])
{
int [] a = { 2 , 3 , 2 };
int [] b = { 10 , 30 , 40 };
System.out.println(maxProfit(a, b, 4 ));
}
}
|
Python3
def maxProfit (a, b, n):
maxP = - 1
for i in range ( 0 , n + 1 ):
sumA = sum (a[:i])
sumB = sum (b[i:])
maxP = max (maxP, sumA + sumB)
return maxP
if __name__ = = "__main__" :
a = [ 2 , 3 , 2 ]
b = [ 10 , 30 , 40 ]
print (maxProfit(a, b, 4 ))
|
C#
using System;
public class GFG
{
public static int maxProfit( int [] a, int [] b, int n)
{
var maxP = -1;
for ( int i = 0; i < n + 1; i++)
{
var sumA = 0;
for ( int j = 0; j < Math.Min(i,a.Length); j++)
{
sumA += a[j];
}
var sumB = 0;
for ( int j = i; j < b.Length; j++)
{
sumB += b[j];
}
maxP = Math.Max(maxP,sumA + sumB);
}
return maxP;
}
public static void Main(String[] args)
{
int [] a = {2, 3, 2};
int [] b = {10, 30, 40};
Console.WriteLine(GFG.maxProfit(a, b, 4));
}
}
|
Javascript
<script>
function maxProfit(a, b, n)
{
let maxP = -1;
for (let i = 0; i < n + 1; i++) {
let sumA = 0;
for (let j = 0; j < Math.min(i, a.length); j++)
sumA += a[j];
let sumB = 0;
for (let j = i; j < b.length; j++)
sumB += b[j];
maxP = Math.max(maxP, sumA + sumB);
}
return maxP;
}
let a = [ 2, 3, 2 ];
let b = [ 10, 30, 40 ];
document.write(maxProfit(a, b, 4));
</script>
|
Time Complexity : O(N)
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...