Python Program to Find Maximum profit by buying and selling a share at most twice
Maximum profit by buying and selling a share at most twice
Here on this page, we will learn to create Python Program to calculate Maximum profit by buying and selling a share at most twice when all the prices are given in an array.
Example :
- Input : price = [2, 30, 15, 10, 8, 25, 80]
- Output : 100
Algorithm
- Initialize a variable ans with value zero
- Iterating from 0 to l using for loop with variable i
- Run the nested loop with variable j from i+1 to l
- If arr[j] > arr[i] then put a equals to arr[j] – arr[i] & b = next(arr[ j+1 : ]. Check if ans is less then b+a then put ans = b +a
Algorithm for next function
- Initialize a variable ans with value zero
- Iterate using for loop between range 0 to the length of arr
- Run a nested loop using variable j from i+1 to length of arr
- For each iteration put a equals to arr[i] and b = arr[j]
- check if b is greater than a and and is lea then b – a then assign b-a to ans
Python Code
Run
def next(arr): ans = 0 for i in range(len(arr)): for j in range(i + 1, len(arr)): a = arr[i] b = arr[j] if b > a and ans < b - a: ans = b - a return ans def stock_price(arr, l): ans = 0 for i in range(l): for j in range(i + 1, l): if arr[j] > arr[i]: a = arr[j] - arr[i] b = next(arr[j + 1:]) if ans < b + a: ans = b + a return ans price = [2, 30, 15, 10, 8, 25, 80] print("Maximum profit is :", stock_price(price, len(price)))
Output
Maximum profit is : 100
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
For similar questions click on the given button.
Login/Signup to comment
ef profit(arr):
maxx=0
j=len(arr)-1
k=j-1
i=0
n=i+1
while j>2:
k=j-1
while k>1:
while i<k-1:
n=i+1
while nmaxx:
maxx=a
n+=1
i=i+1
k=k-1
i=0
j-=1
return maxx
arr=[2, 30, 15, 10, 8, 25, 80]
print(profit(arr))
Kindly join us at Discord Channel for technical queries