# Maximizing Profit in Stock Buy Sell in Java

It is one of the prominent problems asked in the technical interviews. In this problem, an integer array is given that represents the cost of the stock on different dates. Note that one can purchase and sell the stock any number of times. In this section, we will discuss the maximizing profit problem in a stock buy sell in Java. Let's understand the problem.

Consider the following array.

int arr[] = {50, 90, 130, 155, 20, 267, 347}

Here, the cost of the stock on day 1 is 50. Next day, the cost becomes 90, and 130 on the third day, and so on. Thus, a person can purchase a stock on the first day, and can sell on the fourth day. Thus, the profit becomes 155 - 50 = 105. Again, the stock can be purchased on the fifth day and should be sold on the seventh day. Thus, the profit becomes 347 - 20 = 327, and the total profit becomes 105 + 327 = 432 that is the maximum profit earned.

## Approach: Naive

In this approach, a stock is purchased and then sold out every day when it is profitable. Also, one needs to keep track of the maximum profit earned so far. The following program depicts the same.

FileName: MaxProfitStock.java

Output:

```The price of the stock on different days is:
50 90 130 155 20 267 347
The maximum profit earned is: 432
```

## Approach: Efficient

In order to make the solution more efficient, we will not be using recursion. In this approach, we will find the local minima and maxima. The local minima will be treated as the starting index, and the local maxima will be treated as the ending index. If local minima do not exist, then return. We will also be updating the solution by updating the count of buy-sell pairs. Observe the following code.

FileName: MaxProfitStock1.java

Output:

```The price of the stock on different days is:
50 90 130 155 20 267 347

Buy the stock on day: 1	 Sell the stock on day: 4
Buy the stock on day: 5	 Sell the stock on day: 7
Thus, the total profit is: 432
```

Complexity Analysis: The outer-loop runs till j = 0 to size - 1. The two inner loops increment the value of j in each iteration. Therefore, these two inner loops help j to reach size - 1 faster. Therefore, the time complexity of the program is O(size). The program also uses some extra space to store the days where the stock is bought or sold. Thus, the space complexity of the program is O(size), as the number of days cannot exceed the number of elements present in the array.

## Approach: Valley Peak

In this approach, we require to look for the next greater element and subtract it from the current element. We do so because the difference keeps on increasing unless we hit a minimum. Note that, we have not used any extra space. The time complexity of this approach still remains O(size).

The following Java program depicts the same.

FileName: MaxProfitStock2.java

Output:

```The price of the stock on different days is:
50 90 130 155 20 267 347
The maximum profit earned is: 432
```

#### Note 1: If the prices of the stock are arranged in descending order. Then, one cannot earn a profit.

For example:

int p[] = { 800, 795, 505, 440, 311, 261, 181, 101, 40, 5};

The cost of a stock is decreasing day by day. Therefore, if a person purchases a stock on a day, then any day after the stock has been purchased will have the cost of the stock less than the purchased cost. Hence, one will bear a loss.

#### Note 2: There can be many variations of this problem. One of the variations is to maximize the profit when a stock is allowed to be sold and bought only one time. Now, this problem is similar to the problem where one has to find the maximum difference between two elements, where the element of the higher value comes after the element of the smaller value.

If we take the price of the stock on consecutive days as int p[] = {50, 90, 130, 155, 20, 267, 347}, then the maximum profit for one time buy and sell is 347 - 20 = 327.

FileName: MaxProfitStock3.java

Output:

```The price of the stock on different days is:
50 90 130 155 20 267 347
The maximum profit earned is: 327
```

Similarly, one can write the code to maximize the profit when the buying and selling of a stock are allowed at most two times, or at most three times, and so on.

## Help Others, Please Share   