[Python , Javascript]  Easy solution with very clear Explanation
Finding the Best Day to Buy and Sell StockThe question requires us to find the best day to buy and sell stock to maximize profit.Some may think that simply find
## Finding the Best Day to Buy and Sell Stock

The question requires us to find the best day to buy and sell stock to maximize profit.

Some may think that simply finding the minimum and maximum numbers in the array will yield the maximum profit. However, there's a catch:

### Example 1:
```python
prices = [3, 4, 1, 6]
```
- Minimum = 1
- Maximum = 6
- Profit = `max - min = 6 - 1 = 5` (which is correct)

### Example 2:
```python
prices = [7, 6, 4, 3, 1]
```
- Minimum = 1 (Day 6)
- Maximum = 7 (Day 1)
- Max profit = `7 - 1 = 6`

Although it seems correct, you **cannot buy on day 6 and sell on day 1** because you must buy before selling.

---

*So what is the best way to find the max profit lets see 😃*

### Explanation:
We initialize two pointers:
- **Left (Buy Pointer)** at the first position
- **Right (Sell Pointer)** at the second position

We also initialize `max_profit = 0`.

### Algorithm:
We use a **while loop** that runs until the right pointer reaches the end of the array.

#### Example Walkthrough:
```python
prices = [7, 1, 5, 3, 6, 4]
```

**Note:**
prices[left] --> buy stock

prices[right] --> sell stock



| Step | Left Price | Right Price | Profit | Action |
|------|-----------|------------|--------|---------|
| 1    | 7         | 1          | -6     | Move left pointer to right position, increment right pointer |
| 2    | 1         | 5          | 4      | Update max profit, move right pointer |
| 3    | 1         | 3          | 2      | Max profit remains 4, move right pointer |
| 4    | 1         | 6          | 5      | Update max profit to 5, move right pointer |
| 5    | 1         | 4          | 3      | Max profit remains 5, move right pointer |

#### Detail explanation 

**Step 1:**
`price[left] = 7`, `price[right] = 1`, `profit = -6`
Here, price[left] is greater than price[right], so we will move the left pointer to the right position and increment our right pointer by 1. We always want our left pointer to be at the minimum.

**Step 2:**

`price[left] = 1`,` price[right] = 5`, `profit = 4`
Here, price[left] is less than price[right], which means we will get a profit. So, we will update our max_profit and move our right pointer alone.

**Step 3:**

`price[left] = 1`, `price[right] = 3`,` profit = 2`
Here, price[left] is less than price[right], which means we will get a profit. We will check our max_profit, which was previously 4. Now our current profit is 2, so we will check which is maximum and update our max_profit and move our right pointer alone.

**Step 4:**

`price[left] = 1`, `price[right] = 6`, `profit = 5`
Here, price[left] is less than price[right], which means we will get a profit. We will check our max_profit, which was previously 4. Now our current profit is 5, so we will check which is maximum and update our max_profit and move our right pointer alone.

**Step 5:**

`price[left] = 1`, `price[right] = 4`, `profit = 3`
Same logic as above.


![Stock Market Graph](https://assets.leetcode.com/users/images/c0c86dc7-f7fa-4be7-85f9-61e629aa67ae_1643686591.6894035.jpeg)


### Complexity Analysis:
- **Time Complexity:** `O(n)` (single pass through the array)
- **Space Complexity:** `O(1)` (constant space used)

---

## Solution Implementation:

```python []
class Solution:
    def maxProfit(self, prices):
        left = 0  # Buy
        right = 1  # Sell
        max_profit = 0
        while right < len(prices):
            current_profit = prices[right] - prices[left]
            if prices[left] < prices[right]:
                max_profit = max(current_profit, max_profit)
            else:
                left = right
            right += 1
        return max_profit
```

```javascript []
const maxProfit = (prices) => {
  let left = 0; // Buy
  let right = 1; // Sell
  let max_profit = 0;
  while (right < prices.length) {
    if (prices[left] < prices[right]) {
      let profit = prices[right] - prices[left];
      max_profit = Math.max(max_profit, profit);
    } else {
      left = right;
    }
    right++;
  }
  return max_profit;
};
```

---
### Conclusion:
This approach ensures that we always buy at the lowest price before selling, optimizing for maximum profit in `O(n)` time.



`UPVOTE if you like 😃. If you have any questions, feel free to ask!`
['Two Pointers', 'Python', 'JavaScript']