Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Best time to buy and sell stock
MEDIUM
30 mins
Greedy
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Greedy
-
-
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
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
userLevel
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
yellow-spark
0/80
Avg time to solve 30 mins
Success Rate 72 %
Share
0 upvotes

Problem Statement

You are given an array of integers, ‘PRICES’ of size ‘N’, where ‘PRICES[i]’ is the price of a given stock on an ‘i’th day.

You want to maximize the profit by choosing a single day to buy one stock and a different day to sell that stock.

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
Reset Code
Full screen
Auto
copy-code
Console