💥|| EXPLAINED IN HINGLISH || OPTIMAL || BEGINNER FRIENDLY || 💥
Intuition\n1)START:\nBanayein ek max heap priority_queue ko istemal karke.\n\n2)Data Insertion:\nHar element nums array mein traverse karein.\nHar ek element ko
# Intuition\n1)**START**:\nBanayein ek max heap priority_queue ko istemal karke.\n\n2)**Data Insertion:**\nHar element nums array mein traverse karein.\nHar ek element ko max heap mein daalein (maxheap.push(i)).\n\n3)**Do Sabse Bade Elements**:\ni)Top method se max heap se sabse bada element (a) nikalein.\nUsse hatayein (maxheap.pop()).\nii)Fir se top method se doosra sabse bada element (b) nikalein.\nUsse bhi hatayein (maxheap.pop()).\n\n4)**Result Calculation**:\nIn dono bade elements se ek chhota karke ((a - 1) * (b - 1)) result nikalein.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n1)inserting elements into the max heap: O(N * log(N))\n\nHere, \'N\' is the number of elements in the \'nums\' array.\nFor each element, inserting into the max heap takes logarithmic time.\n\n2)Extracting two largest elements: O(2 * log(N)) \u2248 O(log(N))\n\nExtracting the top element from the max heap is a logarithmic operation.\nSince we do it twice, we consider it as O(log(N)).\n\n3)Overall Time Complexity: O(N * log(N))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n1)Priority Queue (max heap): O(N)\n\nIn the worst case, the priority queue can store all \'N\' elements of the \'nums\' array.\n\n2)integer Variables (a and b): O(1)\n\nConstant space is used for storing two variables.\n\n3)Overall Space Complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```C++ []\nclass Solution {\npublic:\n    int maxProduct(vector<int>& nums) {\n// Create a max heap (priority queue)\n        priority_queue<int>maxheap;\n// Insert elements into the max heap\n        for(auto i : nums)\n        {\n            maxheap.push({i});\n        }\n // Extract two largest elements\n        int a = maxheap.top();\n        maxheap.pop();\n        int b= maxheap.top();\n        maxheap.pop();\n// Calculate and return the result\n        return ((a-1)*(b-1));\n        \n    }\n};\n```\n```java []\n\nclass Solution {\n    public int maxProduct(int[] nums) {\n        // Create a max heap (priority queue)\n        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n        // Insert elements into the max heap\n        for (int num : nums) {\n            maxHeap.offer(num);\n        }\n\n        // Extract two largest elements\n        int a = maxHeap.poll();\n        int b = maxHeap.poll();\n\n        // Calculate and return the result\n        return (a - 1) * (b - 1);\n    }\n}\n\n```\n```python []\n def maxProduct(self, nums):\n        # Create a max heap (priority queue)\n        max_heap = [-num for num in nums]\n        heapq.heapify(max_heap)\n\n        # Extract two largest elements\n        a = -heapq.heappop(max_heap)\n        b = -heapq.heappop(max_heap)\n\n        # Calculate and return the result\n        return (a - 1) * (b - 1)\n```\n\n\n
['Array', 'Sorting', 'Heap (Priority Queue)', 'Python', 'C++', 'Java']