```
Here we have to find min value till ith element and profit will be the difference between ith element & min so far.
int maxProfit(int* p, int pS) {
int i, min=p[0], prof=0;
for(i=1; i<pS; i++)
if(p[i]>min)
prof = prof > p[i]-min ? prof : p[i]-min;
else
min = p[i];
return prof;
}
```