☕ Java Solution | O(n) approach | Runtime 0 ms | Beats 100% 📕
Intuition\nIn order to solve this problem we need to find max1 and max2. \n\nA naive approach to this task will be to sort the array, but we do not want to do i
# Intuition\nIn order to solve this problem we need to find max1 and max2. \n\nA naive approach to this task will be to sort the array, but we do not want to do it because we will get O(n log n) due to sorting. All we need to do is to find max1 and max2 and multiply them. How will we do it?\n\n# Approach\n1. Initialize max1 and max2 and minimum possible values.\n2. Iterate over the array and find max1 and max2 with the following logic:\n\n    ```\n    if (max1 <= nums[i]) {\n        max2 = max1;\n        max1 = nums[i];\n    }\n    if (nums[i] < max1 && nums[i] > max2) {\n        max2 = nums[i];\n    }\n    ```\n3. Do math and return the result.\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(n)$$\n\n# Code\n```\nclass Solution {\n    public int maxProduct(int[] nums) {\n        int max1 = Integer.MIN_VALUE;\n        int max2 = Integer.MIN_VALUE;\n        for (int i = 0; i < nums.length; i++) {\n            if (max1 <= nums[i]) {\n                max2 = max1;\n                max1 = nums[i];\n            }\n            if (nums[i] < max1 && nums[i] > max2) {\n                max2 = nums[i];\n            }\n        }\n        return (max1 - 1) * (max2 - 1);\n    }\n}\n```\n![4x5hdv.jpeg](https://assets.leetcode.com/users/images/280d61e9-531b-45f0-8280-525c8b459c43_1677435734.4834585.jpeg)\n
['Java']