✅ Accepted Java/C Code
\n# Algorithm\n- keep min. value as first element of array\n- Just travesrse left to right of array\n  - Change value of min. only when you get a number smaller
\n# Algorithm\n- keep min. value as first element of array\n- Just travesrse left to right of array\n  - Change value of min. only when you get a number smaller than it\n  - Change value of profit only when you get a number greater than it\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n<!-- - Space complexity: -->\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```JAVA []\nclass Solution {\npublic int maxProfit(int[] prices) {\n    int profit = 0, min = prices[0], temp;\n    for(int i=1; i<prices.length; i++){\n        if(prices[i]<min) min = prices[i];\n        temp = prices[i] - min;\n        if(temp > profit) profit = temp;\n    }\n    return profit;\n}\n}\n```\n```C []\nint maxProfit(int* prices, int pricesSize){\n    int profit = 0, min = prices[0], temp;\n    for(int i=1; i<pricesSize; i++){\n        if(prices[i]<min) min = prices[i];\n        temp = prices[i] - min;\n        if(temp > profit) profit = temp;\n    }\n    return profit;\n}\n```\n> ## Upvote if you find helpful\n
['Array', 'Dynamic Programming', 'C', 'Java']