✅☑[C++/Java/Python/JavaScript] || 3 Approaches || Beats 100% || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1(Brute Force)\n1. The function iterates through the elements
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(Brute Force)***\n1. The function iterates through the elements in the `nums` array using two nested loops (`i` and `j`) to consider all possible pairs of elements.\n1. For each pair of elements (`nums[i], nums[j]`), it calculates the product of `(nums[i] - 1) * (nums[j] - 1)`.\n1. It updates the `ans` variable to hold the maximum product found among all pairs considered.\n1. Finally, it returns the maximum product of two distinct elements in the array.\n\n# Complexity\n- *Time complexity:*\n   $$O(n^2)$$\n    \n\n- *Space complexity:*\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    // Function to find the maximum product of two distinct elements in the \'nums\' array\n    int maxProduct(vector<int>& nums) {\n        int ans = 0; // Initializing the variable \'ans\' to store the maximum product\n        \n        // Nested loops to iterate through pairs of elements in the \'nums\' array\n        for (int i = 0; i < nums.size(); i++) {\n            for (int j = i + 1; j < nums.size(); j++) {\n                // Calculating the product of (nums[i] - 1) and (nums[j] - 1)\n                // and updating \'ans\' to hold the maximum product found so far\n                ans = max(ans, (nums[i] - 1) * (nums[j] - 1));\n            }\n        }\n        \n        return ans; // Returning the maximum product of two distinct elements\n    }\n};\n\n\n\n```\n```C []\nint maxProduct(int nums[], int numsSize) {\n    int ans = 0;\n    \n    for (int i = 0; i < numsSize; i++) {\n        for (int j = i + 1; j < numsSize; j++) {\n            ans = (ans > (nums[i] - 1) * (nums[j] - 1)) ? ans : (nums[i] - 1) * (nums[j] - 1);\n        }\n    }\n    \n    return ans;\n}\n\n\n\n```\n```Java []\n\nclass Solution {\n    public int maxProduct(int[] nums) {\n        int ans = 0;\n        for (int i = 0; i < nums.length; i++) {\n            for (int j = i + 1; j < nums.length; j++) {\n                ans = Math.max(ans, (nums[i] - 1) * (nums[j] - 1));\n            }\n        }\n        \n        return ans;\n    }\n}\n\n```\n```python3 []\nclass Solution:\n    def maxProduct(self, nums: List[int]) -> int:\n        ans = 0\n        for i in range(len(nums)):\n            for j in range(i + 1, len(nums)):\n                ans = max(ans, (nums[i] - 1) * (nums[j] - 1))\n        \n        return ans\n\n```\n\n```javascript []\nfunction maxProduct(nums) {\n    let ans = 0;\n\n    for (let i = 0; i < nums.length; i++) {\n        for (let j = i + 1; j < nums.length; j++) {\n            ans = Math.max(ans, (nums[i] - 1) * (nums[j] - 1));\n        }\n    }\n\n    return ans;\n}\n\n```\n\n---\n\n#### ***Approach 2(Sorting)***\n1. The function sorts the input array `nums` in ascending order using `sort(nums.begin(), nums.end())`.\n1. It then retrieves the largest element (`x`) and the second largest element (`y`) from the sorted array.\n1. The function returns the product of `(x - 1) * (y - 1)`, which is the maximum possible product of two distinct elements in the array after subtracting 1 from each of the two largest elements.\n\n# Complexity\n- *Time complexity:*\n   $$O(nlogn)$$\n    \n\n- *Space complexity:*\n   $$O(logn)$$ or $$O(n)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    // Function to find the maximum product of two distinct elements in the \'nums\' array\n    int maxProduct(vector<int>& nums) {\n        sort(nums.begin(), nums.end()); // Sorting the \'nums\' array in ascending order\n        \n        int x = nums[nums.size() - 1]; // Getting the largest element\n        int y = nums[nums.size() - 2]; // Getting the second largest element\n        \n        return (x - 1) * (y - 1); // Returning the product of (largest - 1) * (second largest - 1)\n    }\n};\n\n\n\n```\n```C []\n\nint compare(const void *a, const void *b) {\n    return (*(int*)a - *(int*)b);\n}\n\nint maxProduct(int* nums, int numsSize) {\n    qsort(nums, numsSize, sizeof(int), compare);\n\n    int x = nums[numsSize - 1];\n    int y = nums[numsSize - 2];\n\n    return (x - 1) * (y - 1);\n}\n\n\n```\n```Java []\nclass Solution {\n    public int maxProduct(int[] nums) {\n        Arrays.sort(nums);\n        int x = nums[nums.length - 1];\n        int y = nums[nums.length - 2];\n        return (x - 1) * (y - 1);\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    def maxProduct(self, nums: List[int]) -> int:\n        nums.sort()\n        x = nums[-1]\n        y = nums[-2]\n        return (x - 1) * (y - 1)\n\n```\n\n```javascript []\nfunction maxProduct(nums) {\n    nums.sort((a, b) => a - b);\n\n    const x = nums[nums.length - 1];\n    const y = nums[nums.length - 2];\n\n    return (x - 1) * (y - 1);\n}\n\n```\n\n---\n#### ***Approach 3(Second Biggest)***\n1. The function `maxProduct` iterates through each element (`num`) in the `nums` array.\n1. It maintains two variables, `biggest` and `secondBiggest`, to keep track of the largest and second largest numbers encountered in the array.\n1. If the current `num` is greater than the current `biggest`, it updates `secondBiggest` to the previous value of `biggest` and updates `biggest` to the current `num`.\n1. If the current `num` is not greater than `biggest`, it updates `secondBiggest` if necessary (keeping track of the second largest number encountered).\n1. Finally, it returns the product of `(biggest - 1) * (secondBiggest - 1)`, which is the maximum product of two distinct elements in the array after subtracting 1 from each of the two largest elements.\n\n# Complexity\n- *Time complexity:*\n   $$O(n)$$\n    \n\n- *Space complexity:*\n   $$O(1)$$\n    \n\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    // Function to find the maximum product of two distinct elements in the \'nums\' array\n    int maxProduct(vector<int>& nums) {\n        int biggest = 0;         // Variable to store the largest number in \'nums\'\n        int secondBiggest = 0;   // Variable to store the second largest number in \'nums\'\n        \n        for (int num : nums) {   // Iterating through each number in \'nums\'\n            if (num > biggest) {\n                // If the current number is greater than \'biggest\',\n                // update \'secondBiggest\' and \'biggest\'\n                secondBiggest = biggest;\n                biggest = num;\n            } else {\n                // If the current number is not greater than \'biggest\',\n                // update \'secondBiggest\' if necessary (keeping track of the second largest)\n                secondBiggest = max(secondBiggest, num);\n            }\n        }\n        \n        return (biggest - 1) * (secondBiggest - 1); // Return the product of (biggest - 1) and (secondBiggest - 1)\n    }\n};\n\n\n\n```\n```C []\nint maxProduct(int* nums, int numsSize) {\n    int biggest = INT_MIN; // Variable to store the largest number in \'nums\'\n    int secondBiggest = INT_MIN; // Variable to store the second largest number in \'nums\'\n\n    for (int i = 0; i < numsSize; i++) { // Iterating through each number in \'nums\'\n        if (nums[i] > biggest) {\n            // If the current number is greater than \'biggest\',\n            // update \'secondBiggest\' and \'biggest\'\n            secondBiggest = biggest;\n            biggest = nums[i];\n        } else {\n            // If the current number is not greater than \'biggest\',\n            // update \'secondBiggest\' if necessary (keeping track of the second largest)\n            secondBiggest = max(secondBiggest, nums[i]);\n        }\n    }\n\n    return (biggest - 1) * (secondBiggest - 1); // Return the product of (biggest - 1) and (secondBiggest - 1)\n}\n\n\n\n```\n```Java []\nclass Solution {\n    public int maxProduct(int[] nums) {\n        int biggest = 0;\n        int secondBiggest = 0;\n        for (int num : nums) {\n            if (num > biggest) {\n                secondBiggest = biggest;\n                biggest = num;\n            } else {\n                secondBiggest = Math.max(secondBiggest, num);\n            }\n        }\n        \n        return (biggest - 1) * (secondBiggest - 1);\n    }\n}\n\n\n```\n```python3 []\nclass Solution:\n    def maxProduct(self, nums: List[int]) -> int:\n        biggest = 0\n        second_biggest = 0\n        for num in nums:\n            if num > biggest:\n                second_biggest = biggest\n                biggest = num\n            else:\n                second_biggest = max(second_biggest, num)\n        \n        return (biggest - 1) * (second_biggest - 1)\n\n```\n\n```javascript []\nfunction maxProduct(nums) {\n    let biggest = Number.MIN_SAFE_INTEGER; // Variable to store the largest number in \'nums\'\n    let secondBiggest = Number.MIN_SAFE_INTEGER; // Variable to store the second largest number in \'nums\'\n\n    for (let i = 0; i < nums.length; i++) { // Iterating through each number in \'nums\'\n        if (nums[i] > biggest) {\n            // If the current number is greater than \'biggest\',\n            // update \'secondBiggest\' and \'biggest\'\n            secondBiggest = biggest;\n            biggest = nums[i];\n        } else {\n            // If the current number is not greater than \'biggest\',\n            // update \'secondBiggest\' if necessary (keeping track of the second largest)\n            secondBiggest = Math.max(secondBiggest, nums[i]);\n        }\n    }\n\n    return (biggest - 1) * (secondBiggest - 1); // Return the product of (biggest - 1) and (secondBiggest - 1)\n}\n\n```\n\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Array', 'C', 'Sorting', 'Heap (Priority Queue)', 'C++', 'Java', 'Python3', 'JavaScript']