Maximum profit after buying and selling stocks with transaction fees
Last Updated :
31 Aug, 2022
Given an array of positive integers containing the price of stocks and transaction fee, the task is to find the maximum profit and the difference of days on which you are getting the maximum profit.
Examples:
Input: arr[] = {6, 1, 7, 2, 8, 4}
transactionFee = 2
Output: 8 1
Input: arr[] = {7, 1, 5, 3, 6, 4}
transactionFee = 1
Output: 5 1
Explanation: Considering the first example: arr[] = {6, 1, 7, 2, 8, 4}, transactionFee = 2
- If we buy and sell on the same day, we will not get any profit that’s why the difference between the buying and selling must be at least 1.
- With the difference of 1 day, if we buy a stock of rupees 1 and sell it rupees 7 with the difference of day 1 which mean purchase on day 2 and sell it next day,then after paying the transaction fee of rupees 2 i.e. 7-1-2=4, we will get profit of 4 rupees, same as if we purchase on day 4 and sell it on day 5 with the difference of day 1 then we get profit of 4 rupees. So the total profit is 8 rupees.
- With the difference of 2 days, we will not get any profit.
- With the difference of 3 days, if we buy stock of rupees 1 and sell it rupees 8 with the difference of 3 days which mean purchase on day 2 and sell it after 3 days then maximum profit after paying the transaction fee of rupees 2 i.e.8-1-2=5 we will get the profit of 5 rupees.
- With the difference of 4 days, if we buy stocks of rupees 1 and sell it rupees 4 with the difference of 4 days which mean purchase on day 2 and sell it after 4 days then after paying the transaction fee of rupees 2 i.e. 4-1-2=1, we will get profit of 1 rupees.
- With the difference of 5 days, we will not get any profit.
Approach:
- Traverse the whole array with the difference of each day.
- Check the profit by subtracting the price of each day including transaction fee.
- Trace the maximum profit and store the diff_days on which we are getting the maximum profit.
- Repeat the above steps until the loop terminates.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max_profit( int a[], int b[], int n, int fee)
{
int i, j, profit;
int l, r, diff_day = 1, sum = 0;
b[0] = 0;
b[1] = diff_day;
for (i = 1; i < n; i++) {
l = 0;
r = diff_day;
sum = 0;
for (j = n - 1; j >= i; j--) {
profit = (a[r] - a[l]) - fee;
if (profit > 0)
sum = sum + profit;
l++;
r++;
}
if (b[0] < sum) {
b[0] = sum;
b[1] = diff_day;
}
diff_day++;
}
return 0;
}
int main()
{
int arr[] = { 6, 1, 7, 2, 8, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
int b[2];
int tranFee = 2;
max_profit(arr, b, n, tranFee);
cout << b[0] << ", " << b[1] << endl;
return 0;
}
|
C
#include <stdio.h>
int max_profit( int a[], int b[], int n, int fee)
{
int i, j, profit;
int l, r, diff_day = 1, sum = 0;
b[0] = 0;
b[1] = diff_day;
for (i = 1; i < n; i++) {
l = 0;
r = diff_day;
sum = 0;
for (j = n - 1; j >= i; j--) {
profit = (a[r] - a[l]) - fee;
if (profit > 0)
sum = sum + profit;
l++;
r++;
}
if (b[0] < sum) {
b[0] = sum;
b[1] = diff_day;
}
diff_day++;
}
return 0;
}
int main()
{
int arr[] = { 6, 1, 7, 2, 8, 4 };
int n = sizeof (arr) / sizeof (arr[0]);
int b[2];
int tranFee = 2;
max_profit(arr, b, n, tranFee);
printf ( "%d, %d" , b[0], b[1]);
return 0;
}
|
Java
import java.util.*;
class solution
{
static int max_profit( int a[], int b[], int n, int fee)
{
int i, j, profit;
int l, r, diff_day = 1 , sum = 0 ;
b[ 0 ]= 0 ;
b[ 1 ]=diff_day;
for (i= 1 ;i<n;i++)
{
l= 0 ;
r=diff_day;
sum= 0 ;
for (j=n- 1 ;j>=i;j--)
{
profit=(a[r]-a[l])-fee;
if (profit> 0 )
{
sum=sum+profit;
}
l++;
r++;
}
if (b[ 0 ] < sum)
{
b[ 0 ] = sum;
b[ 1 ] = diff_day;
}
diff_day++;
}
return 0 ;
}
public static void main(String args[])
{
int arr[] = { 6 , 1 , 7 , 2 , 8 , 4 };
int n = arr.length;
int [] b = new int [ 2 ];
int tranFee = 2 ;
max_profit(arr, b, n, tranFee);
System.out.println(b[ 0 ]+ ", " +b[ 1 ]);
}
}
|
Python3
def max_profit(a, b, n, fee):
i, j, profit = 1 , n - 1 , 0
l, r, diff_day = 0 , 0 , 1
b[ 0 ] = 0
b[ 1 ] = diff_day
for i in range ( 1 , n):
l = 0
r = diff_day
Sum = 0
for j in range (n - 1 , i - 1 , - 1 ):
profit = (a[r] - a[l]) - fee
if (profit > 0 ):
Sum = Sum + profit
l + = 1
r + = 1
if (b[ 0 ] < Sum ):
b[ 0 ] = Sum
b[ 1 ] = diff_day
diff_day + = 1
return 0
arr = [ 6 , 1 , 7 , 2 , 8 , 4 ]
n = len (arr)
b = [ 0 for i in range ( 2 )]
tranFee = 2
max_profit(arr, b, n, tranFee)
print (b[ 0 ], "," , b[ 1 ])
|
C#
using System;
class GFG
{
static int max_profit( int []a, int []b,
int n, int fee)
{
int i, j, profit;
int l, r, diff_day = 1, sum = 0;
b[0] = 0;
b[1] = diff_day;
for (i = 1; i < n; i++)
{
l = 0; r = diff_day; sum = 0;
for (j = n - 1; j >= i; j--)
{
profit = (a[r] - a[l]) - fee;
if (profit > 0)
{
sum = sum + profit;
}
l++;
r++;
}
if (b[0] < sum)
{
b[0] = sum;
b[1] = diff_day;
}
diff_day++;
}
return 0;
}
static public void Main ()
{
int []arr = { 6, 1, 7, 2, 8, 4 };
int n = arr.Length;
int [] b = new int [2];
int tranFee = 2;
max_profit(arr, b, n, tranFee);
Console.WriteLine(b[0] + ", " + b[1]);
}
}
|
PHP
<?php
function max_profit(& $a , & $b , $n , $fee )
{
$diff_day = 1;
$sum = 0;
$b [0] = 0;
$b [1] = $diff_day ;
for ( $i = 1; $i < $n ; $i ++)
{
$l = 0;
$r = $diff_day ;
$sum = 0;
for ( $j = $n - 1; $j >= $i ; $j --)
{
$profit = ( $a [ $r ] - $a [ $l ]) - $fee ;
if ( $profit > 0)
{
$sum = $sum + $profit ;
}
$l ++;
$r ++;
}
if ( $b [0] < $sum )
{
$b [0] = $sum ;
$b [1] = $diff_day ;
}
$diff_day ++;
}
}
$arr = array (6, 1, 7, 2, 8, 4 );
$n = sizeof( $arr );
$b = array ();
$tranFee = 2;
max_profit( $arr , $b , $n , $tranFee );
echo ( $b [0]);
echo ( ", " );
echo ( $b [1]);
?>
|
Javascript
<script>
function max_profit(a , b , n , fee) {
var i, j, profit;
var l, r, diff_day = 1, sum = 0;
b[0] = 0;
b[1] = diff_day;
for (i = 1; i < n; i++) {
l = 0;
r = diff_day;
sum = 0;
for (j = n - 1; j >= i; j--) {
profit = (a[r] - a[l]) - fee;
if (profit > 0) {
sum = sum + profit;
}
l++;
r++;
}
if (b[0] < sum) {
b[0] = sum;
b[1] = diff_day;
}
diff_day++;
}
return 0;
}
var arr = [ 6, 1, 7, 2, 8, 4 ];
var n = arr.length;
var b = Array(2).fill(0);
var tranFee = 2;
max_profit(arr, b, n, tranFee);
document.write(b[0] + ", " + b[1]);
</script>
|
Complexity Analysis:
- Time complexity: O(N2)
- Auxiliary Space: O(1)
Better approach: Same as https://www.geeksforgeeks.org/stock-buy-sell/ but adds diff days as well
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
pair< int , int > max_profit( int prices[], int n,
int transaction_fee = 0)
{
int start = 0;
int end = 1;
int profit = 0;
int max_profit_till_now = INT_MIN;
int diff = 0;
while (start < n - 1 && end < n) {
while (start < n - 1
&& prices[start] > prices[start + 1])
start += 1;
end = start + 1;
while (end < n - 1
&& prices[end] < prices[end + 1]) {
end += 1;
if (end == n)
continue ;
}
int cur_profit
= prices[end] - prices[start] - transaction_fee;
if (cur_profit > 0)
profit += cur_profit;
if (max_profit_till_now < cur_profit) {
max_profit_till_now = cur_profit;
diff = end - start;
}
start = end + 1;
}
return make_pair(profit, diff);
}
int main()
{
int prices[] = { 6, 1, 7, 2, 8, 4 };
int n = 6;
pair< int , int > res = max_profit(prices, n, 2);
cout << "(" << res.first << ", " << res.second << ")"
<< endl;
}
|
Java
import java.util.*;
class GFG {
static int [] max_profit( int prices[], int n,
int transaction_fee)
{
int start = 0 ;
int end = 1 ;
int profit = 0 ;
int max_profit_till_now = Integer.MIN_VALUE;
int diff = 0 ;
while (start < n - 1 && end < n) {
while (start < n - 1
&& prices[start] > prices[start + 1 ])
start += 1 ;
end = start + 1 ;
while (end < n - 1
&& prices[end] < prices[end + 1 ]) {
end += 1 ;
if (end == n)
continue ;
}
int cur_profit = prices[end] - prices[start]
- transaction_fee;
if (cur_profit > 0 )
profit += cur_profit;
if (max_profit_till_now < cur_profit) {
max_profit_till_now = cur_profit;
diff = end - start;
}
start = end + 1 ;
}
int [] res = { profit, diff };
return res;
}
public static void main(String[] args)
{
int prices[] = { 6 , 1 , 7 , 2 , 8 , 4 };
int n = 6 ;
int [] res = max_profit(prices, n, 2 );
System.out.println( "(" + res[ 0 ] + ", " + res[ 1 ]
+ ")" );
}
}
|
Python3
from typing import List , Tuple
def max_profit(prices: List [ int ], transaction_fee: int = 0 ) - > Tuple [ int , int ]:
n = len (prices)
start = 0
end = 1
profit = 0
max_profit_till_now = float ( '-inf' )
diff = 0
while start < n - 1 and end < n:
while start < n - 1 and prices[start] > prices[start + 1 ]:
start + = 1
end = start + 1
while end < n - 1 and prices[end] < prices[end + 1 ]:
end + = 1
if end = = n:
continue
cur_profit = prices[end] - prices[start] - transaction_fee
if cur_profit > 0 :
profit + = cur_profit
if max_profit_till_now < cur_profit:
max_profit_till_now = cur_profit
diff = end - start
start = end + 1
return profit, diff
print (max_profit([ 6 , 1 , 7 , 2 , 8 , 4 ], 2 ))
|
C#
using System;
using System.Collections.Generic;
class GFG {
static int [] max_profit( int [] prices, int n,
int transaction_fee)
{
int start = 0;
int end = 1;
int profit = 0;
int max_profit_till_now = Int32.MinValue;
int diff = 0;
while (start < n - 1 && end < n) {
while (start < n - 1
&& prices[start] > prices[start + 1])
start += 1;
end = start + 1;
while (end < n - 1
&& prices[end] < prices[end + 1]) {
end += 1;
if (end == n)
continue ;
}
int cur_profit = prices[end] - prices[start]
- transaction_fee;
if (cur_profit > 0)
profit += cur_profit;
if (max_profit_till_now < cur_profit) {
max_profit_till_now = cur_profit;
diff = end - start;
}
start = end + 1;
}
int [] res = { profit, diff };
return res;
}
public static void Main( string [] args)
{
int [] prices = { 6, 1, 7, 2, 8, 4 };
int n = 6;
int [] res = max_profit(prices, n, 2);
Console.WriteLine( "(" + res[0] + ", " + res[1]
+ ")" );
}
}
|
Javascript
function max_profit(prices, transaction_fee = 0)
{
let n = prices.length
let start = 0
let end = 1
let profit = 0
max_profit_till_now = -1000000
let diff = 0
while (start < n - 1 && end < n)
{
while (start < n - 1 && prices[start] > prices[start + 1])
start += 1
end = start + 1
while (end < n - 1 && prices[end] < prices[end + 1])
{
end += 1
if (end == n)
continue
}
let cur_profit = prices[end] - prices[start] - transaction_fee
if (cur_profit > 0)
profit += cur_profit
if (max_profit_till_now < cur_profit)
{
max_profit_till_now = cur_profit
diff = end - start
}
start = end + 1;
}
return [profit, diff]
}
console.log(max_profit([6, 1, 7, 2, 8, 4], 2))
|
Complexity Analysis:
- Time complexity: O(N)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...