Best Time to Buy and Sell Stock

Moderate
0/80
Average time to solve is 20m
profile
Contributed by
406 upvotes
Asked in companies
IntuitOptumOYO

Problem statement

You are given an array/list 'prices' where the elements of the array represent the prices of the stock as they were yesterday and indices of the array represent minutes. Your task is to find and return the maximum profit you can make by buying and selling the stock. You can buy and sell the stock only once.

Note:

You can’t sell without buying first.
For Example:
For the given array [ 2, 100, 150, 120],
The maximum profit can be achieved by buying the stock at minute 0 when its price is Rs. 2 and selling it at minute 2 when its price is Rs. 150.
So, the output will be 148.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains a single integer β€˜T’ denoting the number of test cases to be run. Then the test cases follow.

The first line of each test case contains a single integer β€˜N’, representing the size of the array.

The second line of each test case contains β€˜N’ space-separated integers representing the elements of the given array.
Output Format:
For each test case, print a single integer representing the maximum profit you can achieve. If the maximum profit is negative, print 0.

Output for each test case will be printed in a separate line.
Note :
You are not required to print anything, it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
2 <= N <= 10^4
1 <= ARR[i] <= 10^9

Time Limit: 1 sec.
Sample Input 1:
2
4
1 2 3 4
4
2 2 2 2
Sample Output 1:
3
0
Explanation For Sample Output 1:
For the first test case, it’s optimal to buy the stock at minute 0 and sell it at minute 3 to get a maximum profit of 3.

For the second test case, the maximum profit will be 0 for all possible ways of buying and selling stock.
Sample Input 2:
2
6
17 20 11 9 12 6
4
98 101 66 72
Sample Output 2:
3
6
Hint

Try to check all the possible ways of buying and selling stocks.

Approaches (2)
Brute Force

We will check all the possible ways of buying and selling stocks. We will fix the time we are buying the stock and check all the possible minutes we can sell this stock and update the maximum profit we can get. Now we will take the maximum profit for all the possible buying time and that would be our final maximum profit.

 

Algorithm: 
 

  1. Initialize an integer variable β€˜maxProfit’ to store the maximum profit we can achieve and initialize it with 0. Since if the maximum profit is negative we have to return 0.
  2. Now iterate over the array.
    1. Consider the current index as the buying minute.
    2. Now for this buying minute iterate over all the minutes after that(when we can sell the stock)
      1. Store the maximum profit in a variable called β€˜CurMaxProfit’.
    3. Then take the maximum of all the β€˜CurMaxProfit’ and store it in the β€˜MaxProfit’ variable
  3. Finally, return the β€˜MaxProfit’ value.
Time Complexity

O(N^2), where β€˜N’ is the size of the array.

 

Since we are iterating over all the possible buying time and for each we are considering all the selling time, the final time complexity will be O(N^2).

Space Complexity

O(1)

 

Constant space is used.

Code Solution
(100% EXP penalty)
Best Time to Buy and Sell Stock
Full screen
Console