Leetcode daily problems 🔥 | Easy Explanation | C++✔ | JAVA✔ | PYTHON✔ | C ✔|
Please upvote , if you find it a little helpful\uD83D\uDE22\n\n---\n\n\n# Approach 1 : Using sorting\n# Intuition\nThe goal is to find the maximum product diffe
# ***Please upvote , if you find it a little helpful*\uD83D\uDE22**\n\n---\n\n\n# Approach 1 : Using sorting\n# Intuition\nThe goal is to find the maximum product difference between two pairs of distinct indices.\n\n# Approach\nThe algorithm utilizes sorting to find the maximum product difference between two pairs of distinct indices.\n\n1. **Sorting:**\n   - Sort the `nums` array in ascending order.\n\n2. **Calculation:**\n   - Calculate the product difference using the formula:\n     - `(nums[n-1] * nums[n-2]) - (nums[0] * nums[1])`\n     - where `n` is the size of the array.\n\n\n# Complexity\n- **Time complexity**: O(n log n), where n is the size of the array due to the sorting operation.\n\n- **Space complexity:** O(1)\n\n<iframe src="https://leetcode.com/playground/5AF2rnnq/shared" frameBorder="0" width="400" height="300"></iframe>\n\n### Python code\n```PYTHON\n#python code\nclass Solution:\n    def maxProductDifference(self, nums: List[int]) -> int:\n        nums.sort()\n        n = len(nums)\n        return (nums[n - 1] * nums[n - 2]) - (nums[0] * nums[1])\n\n```\n---\n\n\n---\n\n\n\n# APPROACH 2 : Optimised approach\n# Intuition\nThe goal is to find the maximum product difference between two pairs of distinct indices.\n\n# Approach\nWe can maintain the smallest and largest values encountered during the iteration through the array. By keeping track of the two smallest and two largest elements, we can compute the product difference efficiently.\n\nThe algorithm initializes four variables (`smallest1`, `smallest2`, `largest1`, and `largest2`) to store the two smallest and two largest elements.\n\n1. **Initialization:**\n   - `smallest1` = `smallest2` = INT_MAX\n   - `largest1` = `largest2` = INT_MIN\n\n2. **Iteration:**\n   - Iterate through the array, updating variables based on the current element.\n     - If `num` <= `smallest1`, update `smallest2` and `smallest1`.\n     - If `num` <= `smallest2`, update only `smallest2`.\n     - If `num` >= `largest1`, update `largest2` and `largest1`.\n     - If `num` >= `largest2`, update only `largest2`.\n\n3. **Calculation:**\n   - After the iteration, calculate the product difference:\n     - `(largest1 * largest2) - (smallest1 * smallest2)`.\n\n\n# Complexity\n- **Time complexity**: The algorithm iterates through the array once, making it a linear time complexity, i.e., O(n), where n is the size of the array.\n\n- **Space complexity:** The algorithm uses a constant amount of space for the variables, resulting in O(1) space complexity.\n\n\n<iframe src="https://leetcode.com/playground/8Pc5KWrd/shared" frameBorder="0" width="400" height="300"></iframe>\n\n### Python code\n```PYTHON \n#python code\nclass Solution:\n    def maxProductDifference(self, nums: List[int]) -> int:\n        smallest1, smallest2 = float(\'inf\'), float(\'inf\')\n        largest1, largest2 = float(\'-inf\'), float(\'-inf\')\n\n        for num in nums:\n            if num <= smallest1:\n                smallest2, smallest1 = smallest1, num\n            elif num <= smallest2:\n                smallest2 = num\n\n            if num >= largest1:\n                largest2, largest1 = largest1, num\n            elif num >= largest2:\n                largest2 = num\n\n        return (largest1 * largest2) - (smallest1 * smallest2)\n\n\n```\n
['C', 'Python', 'C++', 'Java', 'Python3']