0ms | Beats 100% | Tricky
Explanation\nIn the code\n# Complexity\n- Time complexity:\nO(n)\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n    int maximumDifference(ve
# Explanation\nIn the code\n# Complexity\n- Time complexity:\n$$O(n)$$\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n    int maximumDifference(vector<int>& nums) {\n        // store the minimum value while traversing\n        int mn = INT_MAX;\n        // for default the answer is -1, incase of descending order\n        int ans = -1;\n        for(int i : nums)\n        {\n            // if we find any element lesser than mn, change mn\n            if(mn > i)\n            {\n                mn = i;\n            }\n            // if the element is greater, make sure ith and jth index arent same\n            else\n            {\n                if(mn != i)\n                ans = max(ans, i-mn);\n            }\n        }\n        return ans;\n        \n    }\n};\n```
['C++']