public class Solution {

public int maxProfit(int[] prices) {

```
//variable to keep track of profit
int profit=0;
//Base case: if prices is empty, return 0
if(prices.length==0)
return profit;
//variable to store minimum value in the array
int min=prices[0];
//for loop used to traverse the array
for(int i=1;i<prices.length;i++)
{
//update the profit if greater than before
if(prices[i]-min>profit)
profit=prices[i]-min;
//update minimum if lesser than before
min=Math.min(min,prices[i]);
}
//return newly computed profit
return profit;
}
```

}