Close
Topic list
Best time to buy and sell stock
MEDIUM
30 mins
Greedy
Arrays
Topics (Covered in this problem)
Problem solved
Skill meter
Greedy
-
-
Arrays
-
-
Other topics
Problem solved
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

# Best time to buy and sell stock

Contributed by
Ratnesh
Medium
0/80
Avg time to solve 30 mins
Success Rate 72 %
Share

## Problem Statement

#### Return the maximum profit you can achieve from this transaction.

##### Note :
``````You can’t sell a stock before you buy one.
``````
##### Example:
``````Let’s say, ‘PRICES’ =[7, 1, 5, 4, 3, 6]

Purchase stock on day two, where the price is one, and sell it on day six, where the price is 6, profit = 6 - 1 = 5.

Hence we return 5.
``````
Detailed explanation ( Input/output format, Notes, Constraints, Images )
##### Sample Input 1:
``````6
7 1 5 4 3 6
``````
##### Sample Output 1 :
``````5
``````
##### Explanation Of Sample Input 1:

Purchase stock on day two, where the price is one, and sell it on day six, where the price is 6, profit = 6 - 1 = 5.

##### Sample Input 2:
``````5
5 4 3 2 1
``````
##### Sample Output 2 :
``````0
``````
Auto
Console