Easy solutions, beginner friendly (97 % beats rate)
Intuition\n- We aim to find the maximum difference between two elements in the array, satisfying the given conditions.\n- To find the maximum difference, we nee
# Intuition\n- We aim to find the maximum difference between two elements in the array, satisfying the given conditions.\n- To find the maximum difference, we need to identify the smallest element nums[i] where i < j and nums[i] < nums[j].\n- If such pairs exist, we calculate the difference nums[j] - nums[i] and update the maximum difference encountered so far.\nIf no such pair exists, we return -1 indicating that there is no valid difference.\n\n---\n\n\n# Approach\n- Initialize max_difference to -1, which will store the maximum difference between elements satisfying the conditions.\n- Initialize left as the first element of the array nums[0].\n- Initialize right as 1, indicating that we start considering elements from the second position.\n- Use a while loop to iterate through the array from the second position onwards (right < nums.length).\n- Inside the loop, check if left < nums[right]. If true, calculate the difference nums[right] - left and update max_difference to the maximum of the current max_difference and the calculated difference.\n- If left is not less than nums[right], update left to nums[right]. This ensures that left always points to the smallest element encountered so far.\n- Increment right to move to the next element in the array.\n- Once the loop ends, return max_difference, which holds the maximum difference between elements satisfying the conditions. If no such pair exists, the function returns -1.\n\n# Complexity\n- Time complexity:o(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: o(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfunction maximumDifference(nums: number[]): number {\n    let  max_difference = -1, left= nums[0], right = 1;\n\n    while(right < nums.length){\n        if(left <nums[right]){\n            max_difference = Math.max(max_difference, nums[right]-left);\n        } else {\n            left = nums[right];\n    }\n    right++;\n    }\n    return max_difference;\n};\n```
['C++', 'TypeScript']