✅ Beats 100% - Explained with [ Video ] - Without Sorting- C++/Java/Python/JS  - Visualized
\n\n# YouTube Video Explanation:\n\n\nhttps://youtu.be/5QFTlws__p8\n **If you want a video for this question please write in the comments** \n\n\uD83D\uDD25 Ple
![Screenshot 2023-12-12 063007.png](https://assets.leetcode.com/users/images/203aab06-2f1f-450b-9a4f-509318f68140_1702343450.4889219.png)\n\n# YouTube Video Explanation:\n\n\n[https://youtu.be/5QFTlws__p8](https://youtu.be/5QFTlws__p8)\n<!-- **If you want a video for this question please write in the comments** -->\n\n**\uD83D\uDD25 Please like, share, and subscribe to support our channel\'s mission of making complex concepts easy to understand.**\n\nSubscribe Link: https://www.youtube.com/@leetlogics/?sub_confirmation=1\n\n*Subscribe Goal: 800 Subscribers*\n*Current Subscribers: 738*\n\n---\n\n# Example Explanation\nLet\'s create a step-by-step explanation using a table:\n\n| **Iteration** | **Array Element** | **max1** | **max2** |\n|---------------|-------------------|----------|----------|\n| 1             | 3                 | 3        | -\u221E      |\n| 2             | 4                 | 4        | 3        |\n| 3             | 5                 | 5        | 4        |\n| 4             | 2                 | 5        | 4        |\n\nNow, we have the two maximum elements, `max1` and `max2`. The result is calculated as `(max1 - 1) * (max2 - 1)`:\n\n**Result = (5 - 1) x (4 - 1) = 4 x 3 = 12**\n\nSo, for the input array \\([3, 4, 5, 2]\\), the maximum product is \\(12\\).\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nTo maximize the product `(nums[i]-1) * (nums[j]-1)`, we need to find the two maximum elements in the array. Subtracting 1 from each of these maximum elements will result in the maximum possible product.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Initialize two variables, `max1` and `max2`, to store the two maximum elements. Set them to `Integer.MIN_VALUE`.\n2. Iterate through the array, updating `max1` and `max2` accordingly.\n3. Calculate and return the maximum product using the formula `(max1 - 1) * (max2 - 1)`.\n\n# Complexity\n- Time Complexity: `O(N)`, where N is the length of the array. We iterate through the array once.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space Complexity: `O(1)`, as we use a constant amount of extra space.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int maxProduct(int[] nums) {\n        int max1 = Integer.MIN_VALUE;\n        int max2 = Integer.MIN_VALUE;\n\n        for (int num : nums) {\n            if (num >= max1) {\n                max2 = max1;\n                max1 = num;\n            } else if (num > max2) {\n                max2 = num;\n            }\n        }\n\n        return (max1 - 1) * (max2 - 1);\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int maxProduct(vector<int>& nums) {\n        int max1 = INT_MIN;\n        int max2 = INT_MIN;\n\n        for (int num : nums) {\n            if (num >= max1) {\n                max2 = max1;\n                max1 = num;\n            } else if (num > max2) {\n                max2 = num;\n            }\n        }\n\n        return (max1 - 1) * (max2 - 1);\n    }\n};\n```\n```Python []\nclass Solution(object):\n    def maxProduct(self, nums):\n        max1 = float(\'-inf\')\n        max2 = float(\'-inf\')\n\n        for num in nums:\n            if num >= max1:\n                max2 = max1\n                max1 = num\n            elif num > max2:\n                max2 = num\n\n        return (max1 - 1) * (max2 - 1)\n        \n```\n```JavaScript []\nvar maxProduct = function(nums) {\n    let max1 = Number.MIN_SAFE_INTEGER;\n    let max2 = Number.MIN_SAFE_INTEGER;\n\n    for (const num of nums) {\n        if (num >= max1) {\n            max2 = max1;\n            max1 = num;\n        } else if (num > max2) {\n            max2 = num;\n        }\n    }\n\n    return (max1 - 1) * (max2 - 1);\n};\n```\n![upvote.png](https://assets.leetcode.com/users/images/c71e7caf-a99c-463d-b4b2-0856175d048a_1702343366.7986863.png)\n
['Array', 'Two Pointers', 'Python', 'C++', 'Java', 'JavaScript']