# Stocks are profitable

Posted: 28 Nov, 2020

Difficulty: Moderate

#### 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.
```

##### 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.
```

Approach 1

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:

- 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.
- Now iterate over the array.
- Consider the current index as the buying minute.
- Now for this buying minute iterate over all the minutes after that(when we can sell the stock)
- Store the maximum profit in a variable called ‘CurMaxProfit’.

- Then take the maximum of all the ‘CurMaxProfit’ and store it in the ‘MaxProfit’ variable

- Finally, return the ‘MaxProfit’ value.

Approach 2

We will iterate over the array and as we go along we will update the buying price as the minimum of the prices till now and will also update the ‘MaxProfit’ variable if selling at this time is more profitable.

Algorithm:

- Initialize a variable ‘BUY’ to store the buying price and ‘MaxProfit’ to store the maximum profit we can achieve.
- Now we will iterate over the array.
- If the current price is less than our buying price, we will update that.
- Else if selling at this minute gives us more profit, then we will update our ‘MaxProfit’ variable as ‘Prices[i]’ - ‘BUY’.

- Finally, return the ‘MaxProfit’ variable.

SIMILAR PROBLEMS

# Game of 3

Posted: 11 Jul, 2021

Difficulty: Easy

# Lexicographic Permutation Rank

Posted: 13 Jul, 2021

Difficulty: Moderate

# Zero Pair Sum

Posted: 22 Jul, 2021

Difficulty: Moderate

# Implement a Queue

Posted: 27 Jul, 2021

Difficulty: Easy

# Remove K Corner Elements

Posted: 31 Jul, 2021

Difficulty: Easy