Problem title
Difficulty
Avg time to solve

Point to Greatest Value Node
Easy
10 mins
Diagonal Traversal
Moderate
15 mins
Sub Query Sum
Easy
10 mins
Check if the Word is present in Sentence or not
Easy
15 mins
Create Sequence
Easy
10 mins
Closest Binary Search Tree Value
Easy
15 mins
Add One to Linked List
Easy
10 mins
Exactly One Child
Easy
15 mins
Predecessor and Successor In BST
Moderate
25 mins
Pair sum in a BST
Easy
15 mins
8

Best Time to Buy and Sell

Difficulty: MEDIUM
Contributed By
Ashwani |Level 1
Avg. time to solve
20 min
Success Rate
80%

Problem Statement

You are given an array(PRICES) of stock prices for N consecutive days. Your task is to find the maximum profit that you can make by completing as many transactions as you like, where a transaction denotes buying one and selling one share of the stock.

Note:

You must sell the stock before you buy it again.
Input Format:
The first line of input contains an integer value N, denoting the size of the input array.

The second line contains N single space-separated integers, denoting the prices on each day.
Output Format:
The only output line contains an integer, denoting the maximum profit.

Note:

You are not required to print the output, it has already been taken care of. Just implement the function. 
Constraints:
1 <= N <= 5 * 10^4
0 <= PRICES[i] <= 10^4    

Time Limit: 1sec
Sample Input 1:
6
2 4 7 1 3 5
Sample Output 1:
9
Explanation for Sample Input 1:
As we are allowed to do any number of transactions to maximize the profit,
The first transaction we will do is to buy on day 1 (PRICE = 2) and sell on day 3 (PRICE = 7), making a profit of  5 (7 - 2).
The second transaction we will do is to buy on day 3 (PRICE = 1) and sell on day = 6 (PRICE = 5), making a profit of 4 (5 - 1).
Total profit = 5 + 4 = 9.
Sample Input 2:
4
1 2 3 4
Sample Output 2:
3
Reset Code
Full screen
copy-code
Console