forked from Manish57-droid/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buy_sell_stock.py
36 lines (27 loc) · 939 Bytes
/
buy_sell_stock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'''
Problem -
You are given an array prices where prices[i] is the price of a given stock on the ith day.
You want to maximize your profit by choosing a single day to buy one stock and choosing a
different day in the future to sell that stock.
Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.
Example -
Input:
prices = [7,1,5,3,6,4]
Output:
5
'''
# Function to find profit
def max_profit(arr, n):
max_ele = arr[-1]
max_diff = 0
for i in range(n - 1, -1, -1):
ele = arr[i]
if (max_ele - ele) < 0:
max_ele = ele
max_diff = max(max_ele - ele, max_diff)
return max_diff
# Main Code
n = int(input("Enter Size of Array : "))
arr = list(map(int, input("Enter Elements : ").split()))
res = max_profit(arr, n)
print("Max Profit is : " + str(res))