def profit(arr):
pro = 0
for i in range(len(arr) - 1):
for j in range(i + 1, len(arr)):
if arr[j] - arr[i] > pro:
pro = arr[j] - arr[i]
return pro
array = [70, 150, 230, 280, 10, 505, 665]
print("We can make maximum profit of", profit(array))
def best_time_to_buy_and_sell_stock(arr):
buy = arr[0]
sell = 0
for i in range(len(arr) – 1):
if buy > arr[i]:
buy = arr[i]
sell = 0
for j in range(i + 1, len(arr)):
sell = max(sell, arr[j])
print(f”Right time to buy is at {arr.index(buy)} position and sell is at {arr.index(sell)}”)
def best_time_to_buy_and_sell_stock(arr):
buy = arr[0]
sell = 0
for i in range(len(arr) – 1):
if buy > arr[i]:
buy = arr[i]
sell = 0
for j in range(i + 1, len(arr)):
sell = max(sell, arr[j])
print(f”Right time to buy is at {arr.index(buy)} position and sell is at {arr.index(sell)}”)
arr = [15, 10, 245, 6, 655, 3, 13]
best_time_to_buy_and_sell_stock(arr)