2 Approaches to solve the problem- Using stack and general method
By Using stack \n class Solution {\npublic:\n    int maximumDifference(vector& nums) {\n        int ans = -1;\n        stack st;\n        st.push(nums[0]);\n   
1. By Using stack \n class Solution {\npublic:\n    int maximumDifference(vector<int>& nums) {\n        int ans = -1;\n        stack<int> st;\n        st.push(nums[0]);\n        int diff = 0;\n        for(int i = 1 ; i<nums.size() ; i++){\n            if(nums[i]>st.top()){\n                diff = nums[i]-st.top();\n                ans = max(ans , diff);\n            }\n            else{\n                st.pop();\n                st.push(nums[i]);\n            }\n        }\n        return ans;     \n    }\n};\n\n2. Generalize approach - by taking first element of nums as the minimum and proceed accordingly-\n\nclass Solution {\npublic:\n    int maximumDifference(vector<int>& nums) {\n        \n        int mn=nums[0];\n        int ans=-1;\n        \n        for(int i=1;i<nums.size();i++){\n            if(nums[i]<mn)\n                mn=nums[i];\n            ans=max(ans,nums[i]-mn);\n        }\n        if(ans==0)\n            ans=-1;\n        return ans;\n    }\n};\n
[]