Easiest Solutions in C++ / Python3 / Java / C# / Python / C / Go  - beats 100%
Intuition\n\n\n\n\n\n Describe your first thoughts on how to solve this problem. \nC++ []\nclass Solution {\npublic:\n    int maximumDifference(vector<int>& num
# Intuition\n![0ehh83fsnh811.jpg](https://assets.leetcode.com/users/images/e5d1a69a-2826-41c4-b24c-7bb97a93bd0b_1712279860.8320851.jpeg)\n\n![Screenshot 2024-04-05 064409.png](https://assets.leetcode.com/users/images/a1fe0b5b-218d-470d-a99c-a68f0a780293_1712279755.1649346.png)\n![Screenshot 2024-04-05 064401.png](https://assets.leetcode.com/users/images/04b1fed3-d858-49e1-8851-3f2cbfb5bb8e_1712279760.7696466.png)\n![Screenshot 2024-04-05 064048.png](https://assets.leetcode.com/users/images/1a8d21b6-4aa7-4439-88c1-b62844c2ecef_1712279765.9882593.png)\n<!-- Describe your first thoughts on how to solve this problem. -->\n```C++ []\nclass Solution {\npublic:\n    int maximumDifference(vector<int>& nums) {\n        int maxi = -1;\n        for (int i = 0; i < nums.size() - 1; i++) {\n            for (int j = i + 1; j < nums.size(); ++j)\n                if (nums[i] < nums[j])\n                    maxi = max(maxi, nums[j] - nums[i]);\n        }\n        return maxi;\n    }\n};\n```\n```python3 []\nclass Solution:\n    def maximumDifference(self, nums: List[int]) -> int:\n        maxi = -1\n        for i in range(len(nums) - 1):\n            for j in range(i + 1, len(nums)):\n                if nums[i] < nums[j]:\n                    maxi = max(maxi, nums[j] - nums[i])\n        return maxi\n```\n```C# []\npublic class Solution {\n    public int MaximumDifference(int[] nums) {\n        int l = 0, r = 1, res = Int32.MinValue;\n        while (r < nums.Length) {\n            if (nums[r] > nums[l]) {\n                res = Math.Max(res, nums[r] - nums[l]);\n                r++;\n            } else {\n                l = r++;\n            };\n        }\n        return (res == Int32.MinValue ? -1 : res);\n    }\n}\n```\n```java []\nclass Solution {\n    public int maximumDifference(int[] nums) {\n        int l = 0, r = 1, res = Integer.MIN_VALUE;\n        while (r < nums.length) {\n            if (nums[r] > nums[l]) {\n                res = Math.max(res, nums[r] - nums[l]);\n                r++;\n            } else {\n                l = r++;\n            };\n        }\n        return (res == Integer.MIN_VALUE ? -1 : res);\n    }\n}\n```\n```python []\nclass Solution(object):\n    def maximumDifference(self, nums):\n        """\n        :type nums: List[int]\n        :rtype: int\n        """\n        buy = nums[0]\n        profit = -1\n        for i, num in enumerate(nums):\n            if i == 0:\n                continue\n            current_profit = num - buy\n            if buy < num:\n                profit = max(current_profit, profit)\n            else:\n                buy = num\n        return profit\n```\n```C []\nint maximumDifference(int* nums, int numsSize) {\n    int l = 0, r = 1, res = INT_MIN;\n    while (r < numsSize) {\n        if (nums[r] > nums[l]) {\n            res = fmax(res, nums[r] - nums[l]);\n            r++;\n        } else {\n            l = r++;\n        };\n    }\n    return (res == INT_MIN ? -1 : res);\n}\n```\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n      O(nlogn)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n      O(1)\n# Code\n```\npublic class Solution {\n    public int MaximumDifference(int[] nums) {\n        int l = 0, r = 1, res = Int32.MinValue;\n        while (r < nums.Length) {\n            if (nums[r] > nums[l]) {\n                res = Math.Max(res, nums[r] - nums[l]);\n                r++;\n            } else {\n                l = r++;\n            };\n        }\n        return (res == Int32.MinValue ? -1 : res);\n    }\n}\n```\n# Thanks for viewing .. keep supporting me . Please upvote\n![th.jpeg](https://assets.leetcode.com/users/images/0ea618b2-bd34-4831-80c5-47743b49f705_1712280048.1561732.jpeg)\n
['Array', 'C', 'Python', 'C++', 'Java', 'Python3', 'C#']