Best 3 Approaches [0ms , 7ms , 9ms] [Java]
\n1. Most optimal solution  T.C- O(n) , S.C -O(1)\n\nclass Solution {\n\n    public int maxProduct(int[] nums) {\n        int max1 = Integer.MIN_VALUE; // Integ
\n**1. Most optimal solution  T.C- O(n) , S.C -O(1)**\n\nclass Solution {\n\n    public int maxProduct(int[] nums) {\n        int max1 = Integer.MIN_VALUE; // Integer.MIN_VALUE -> To handel negative numbers in array\n        int max2 = Integer.MIN_VALUE; // Integer.MIN_VALUE -> To handel negative numbers in array\n        for(int n:nums){\n            if(n>max1){\n                max2 = max1;\n                max1 = n;  //first max element in array\n            }\n            else if(n>max2){\n                max2 = n; //second max element in array\n            }\n        }\n        return (max1-1)*(max2-1);\n    }\n}\n\n\n**2. Time-space tradeoff using priority queue T.C-O(n log n),S.C-O(n)**\n\nclass Solution {\n\n    public int maxProduct(int[] nums) {\n        PriorityQueue<Integer> pq=new PriorityQueue<>(Collections.reverseOrder()); //max-heap\n        \n        \n        for(int n:nums) pq.offer(n);\n        \n        int max1=pq.poll();    //first max element in array\n        int max2=pq.poll();    //second max element in array\n      \n        return (max1-1)*(max2-1);\n    }\n}\n\n**3. Bruteforce solution  T.C- O(n^2) , S.C -O(1)**\n\nclass Solution {\n\n    public int maxProduct(int[] nums) {\n        \n        int max=0;\n        for(int i=0;i<nums.length;i++){\n            for(int j=i+1;j<nums.length;j++){\n                int temp=(nums[i]-1)*(nums[j]-1);\n                max=Math.max(max,temp);\n            }\n        }\n        return max;\n    }\n}\n
['Heap (Priority Queue)', 'Java']