Easy O(n) TC || O(1 SC) || solution in C++;
\nclass Solution \n{\npublic:\n    int maxProfit(vector<int>& prices) \n    {\n        int buy=prices[0],ans=0;\n        for(int i=1;i<prices.size();i++)\n     
```\nclass Solution \n{\npublic:\n    int maxProfit(vector<int>& prices) \n    {\n        int buy=prices[0],ans=0;\n        for(int i=1;i<prices.size();i++)\n        {\n            ans=max(ans,prices[i]-buy);\n            buy=min(buy,prices[i]);\n        }\n        return ans;\n    }\n};\n```\n***Please upvote if you have got any help from my code.Thank you.***
['C']