Brute Better Good Optimal <---> Interview Prep.
Approach 1\nUsing Heap Sort (Priority queue)\n\n# Complexity\n- Time complexity: O(n log(n))\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: 
# Approach 1\nUsing Heap Sort (Priority queue)\n\n# Complexity\n- Time complexity: **O(n log(n))**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(n)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int maxProduct(vector<int>& nums) {\n        priority_queue<int> pq;\n        for(auto x: nums){\n            pq.push(x);\n        }\n        int n = pq.top();\n        pq.pop();\n        int m = pq.top();\n\n        return ((n-1)*(m-1));  \n    }\n};\n```\n# Approach 2\n<!-- Describe your approach to solving the problem. -->\nSort using STL function\n# Complexity\n- Time complexity: **O(n log(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```\nclass Solution {\npublic:\n    int maxProduct(vector<int>& nums) {\n        sort(nums.begin(),nums.end());\n        return (nums[nums.size()-1]-1)*(nums[nums.size()-2]-1);\n    }\n};\n```\n# Approach 3\n<!-- Describe your approach to solving the problem. -->\nDifferent Loops to find Max and Second Max element;\n# Complexity\n- Time complexity: **O(2n)**\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```\nclass Solution {\npublic:\n    int maxProduct(vector<int>& nums) {      \n        int first = *max_element(nums.begin(),nums.end());\n        int count = 0;\n        int second=0;\n        for(auto x: nums){\n            if(count == 1 && first == x){\n                second = x;\n                break;\n            };\n            if(count == 0 && first == x){\n                count++;\n                continue;\n            };\n            second = max(second,x);\n        }\n        return (maxi-1)*(second-1);\n    }\n};\n```\n# Approach 4\n<!-- Describe your approach to solving the problem. -->\nUsing Single loop to find Max and second element;\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```\nclass Solution {\npublic:\n    int maxProduct(vector<int>& nums) {\n        int first=0,second=0;\n     \n        for(auto x: nums){\n            if(x > first){\n                second = first;\n                first = x;\n            }\n            else if(x > second){\n                second = x;\n            }}\n        return (first-1)*(second-1);\n    }\n};\n```\n\n\n\n![c44c8f10-3d46-4228-908f-b730f938eebe_1677407738.1776164.jpeg](https://assets.leetcode.com/users/images/25969ed2-0d2a-4298-ba5e-9f88f115b554_1681058346.4601147.jpeg)\n\n
['Two Pointers', 'Sorting', 'Heap (Priority Queue)', 'C++']