Maximum Difference Between Increasing Elements
Intuition\nThe problem requires finding the maximum difference between two elements in an array nums such that the larger element comes after the smaller elemen
# Intuition\nThe problem requires finding the maximum difference between two elements in an array `nums` such that the larger element comes after the smaller element. The brute force approach naturally comes to mind\u2014comparing all pairs of elements to check the difference and updating the maximum difference whenever a larger one is found.\n\n# Approach\n1. Initialize `ans` to `-1` to store the maximum difference. If no valid pair is found, return `-1`.\n2. Iterate through the array with two nested loops:\n   - The outer loop picks the first element `nums[i]`.\n   - The inner loop picks a second element `nums[j]` where ( j > i ).\n3. For each pair:\n   - Check if `nums[j] > nums[i]` (to satisfy the condition that the larger element comes after the smaller one).\n   - If true, calculate the difference `nums[j] - nums[i]`.\n   - Update `ans` if this difference is greater than the current value of `ans`.\n4. After iterating through all pairs, return the value of `ans`.\n\n# Complexity\n\n- **Time Complexity**:  \n  The nested loops result in a time complexity of ( O(n^2) ), where ( n ) is the length of the input array. Each element in the array is compared with every subsequent element.\n\n- **Space Complexity**:  \n  The space complexity is ( O(1) ) because no additional data structures are used. Only a single variable `ans` is maintained to store the result.\n\n# Code\n```java []\nclass Solution {\n    public int maximumDifference(int[] nums) {\n        int ans = -1;\n        for (int i = 0; i < nums.length; i++) {\n            for (int j = i + 1; j < nums.length; j++) {\n                if (nums[j] > nums[i] && ans < nums[j] - nums[i]) {\n                    ans = nums[j] - nums[i];\n                }\n            }\n        }\n        return ans;\n    }\n}\n```
['Array', 'Java']