Similar to 121. Best Time to Buy and Sell Stock
C++ || 100 BEAT || 100% EFFICIENT\uD83D\uDD25|| OPTIMAL SOLUTION || EASY TO UNDERSTAND ||\n\n- In Best Time to Buy and Sell Stock is that we don\'t need to retu
# C++ || 100 BEAT || 100% EFFICIENT\uD83D\uDD25|| OPTIMAL SOLUTION || EASY TO UNDERSTAND ||\n\n- In Best Time to Buy and Sell Stock is that we don\'t need to return -1 if no profit can be made.\n#  Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem requires finding the maximum difference between two elements in the nums array such that the larger element appears after the smaller element. Essentially, we\'re looking for the maximum nums[j] - nums[i] where j > i.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n### Initialization:\n\n- max_diff is initialized to the first element of the array (nums[0]). This variable keeps track of the minimum value encountered so far.\nans is initialized to 0. This variable will store the maximum difference found.\n### Iterate through the array:\n\n- The for loop iterates through each element in the nums array.\n### In each iteration:\n- Update max_diff: max_diff is updated to be the minimum of the current max_diff and the current element (nums[i]). This ensures max_diff always holds the smallest value encountered up to the current index.\n- Update ans: ans is updated to be the maximum of the current ans and the difference between the current element and max_diff (nums[i] - max_diff). This calculates the potential maximum difference if the current element is considered as the selling price and max_diff as the buying price.\nReturn the Result:\n\n- After the loop, the function checks if ans is 0. If it is, it means no valid pair was found that satisfies the condition (i.e., no element was greater than any previous element). In this case, the function returns -1.\nOtherwise, it returns ans, which contains the maximum difference.\n# Return the Result:\n- After the loop, the function checks if ans is 0. If it is, it means no valid pair was found that satisfies the condition (i.e., no element was greater than any previous element). In this case, the function returns -1.\n- Otherwise, it returns ans, which contains the maximum difference.\n# Complexity\nTime complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n- The algorithm iterates through the nums array exactly once. Each iteration consists of constant-time operations (comparing and updating max_diff and ans). Therefore, the time complexity is linear, O(n), where n is the number of elements in the nums array.\n\nSpace complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n- The algorithm uses a constant amount of additional space (two integer variables max_diff and ans), regardless of the size of the input array. Therefore, the space complexity is constant, O(1).\n\n# Code\n```\nclass Solution {\npublic:\n    int maximumDifference(vector<int>& nums) {\n        int max_diff=nums[0];\n        int ans=0;\n        for(int i=0;i<nums.size();i++){\n            max_diff=min(max_diff,nums[i]);\n            ans=max(ans,nums[i]-max_diff);\n        }\n        return (ans==0)?-1:ans;\n    }\n};\n```
['C++']