Problem title
Difficulty
Avg time to solve

Distinct Subsequences
Moderate
10 mins
Delete Kth node From End
Moderate
15 mins
Design a stack that supports getMin() in O(1) time and O(1) extra space
Moderate
15 mins
Ceil from BST
Easy
15 mins
Best Time to Buy and Sell Stock III
Hard
--
Clone Graph
Moderate
25 mins
Maximum XOR With an Element From Array
Hard
50 mins
Covid Vaccination
Moderate
--
Floor in BST
Moderate
30 mins
Loop Reconnect
Easy
45 mins
24

Best Time to Buy and Sell Stock II

Difficulty: MEDIUM
Contributed By
Avg. time to solve
22 min

Problem Statement

You have been given stock values/prices for N number of days. Every i-th day signifies the price of a stock on that day. Your task is to find the maximum profit which you can make by buying and selling the stocks.

 Note :
You may make as many transactions as you want but can not have more than one transaction at a time i.e, if you have the stock, you need to sell it first, and then only you can buy it again.
Input format :
The first line contains an integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.

The first line of each test case or query contains an integer 'N' representing the total number of days for which you have stock prices.

The second line contains 'N' single space-separated integers representing the price of the stock on i-th day.
Output Format :
For each test case, print the maximum profit that you can earn.

Output for every test case will be printed in a separate line.
Note :
You are not required to print anything explicitly. Just implement the function.
Constraints :
1 <= t <= 10^2
0 <= N <= 10^5
Time Limit: 1 sec
Sample Input 1 :
1
7
1 2 3 4 5 6 7
Sample Output 1 :
6
Explanation :
We can make the maximum profit by buying the stock on the first day and selling it on the last day.
Sample Input 2 :
1
7
7 6 5 4 3 2 1
Sample Output 2 :
0
Explanation :
We can make the maximum profit by not buying the stock.
Reset Code
Full screen
copy-code
Console