question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
find-the-maximum-sum-of-node-values | Simple solution with linear time complexity and constant extra space complexity | simple-solution-with-linear-time-complex-zwgf | Intuition\n\nFirst of, let\'s think about situation when the operation was applied two time for the same edge, or in particular, to the same vertex. As of verte | pioneer10 | NORMAL | 2024-03-02T21:48:06.546809+00:00 | 2024-03-02T21:48:06.546830+00:00 | 79 | false | # Intuition\n\nFirst of, let\'s think about situation when the operation was applied two time for the same edge, or in particular, to the same vertex. As of vertex, the second operation reverts the first one since `(nums[u] XOR k) XOR k == nums[u]`. Two conclusions out of this:\n\n1. we may consider at most one operati... | 1 | 0 | ['Dynamic Programming', 'Go'] | 2 |
find-the-maximum-sum-of-node-values | Easiest logical implementation | without DP or Tree | easiest-logical-implementation-without-d-g1a1 | Intuition\nWe have to find the max sum we can achieve by doing xor of element and k.\n# Approach\n- Lets update each element whose value increases after xor wit | DikshaMakkar | NORMAL | 2024-03-02T19:14:51.224130+00:00 | 2024-03-02T19:14:51.224155+00:00 | 40 | false | # Intuition\nWe have to find the max sum we can achieve by doing xor of element and k.\n# Approach\n- Lets update each element whose value increases after xor with k.\n- Also track the count of number of elements updated with xor -- cnt variable.\n- The problem says that we have to take two nodes which are connected(el... | 1 | 0 | ['C++'] | 1 |
find-the-maximum-sum-of-node-values | Greedy approach | greedy-approach-by-aman_g011-q44n | \n\n# Code\n\n#define ll long long\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n ll | Aman_G011 | NORMAL | 2024-03-02T18:53:51.956612+00:00 | 2024-03-02T18:53:51.956640+00:00 | 18 | false | \n\n# Code\n```\n#define ll long long\nclass Solution {\npublic:\n long long maximumValueSum(vector<int>& nums, int k, vector<vector<int>>& edges) {\n ll n = nums.size();\n vector<int> g, l;\n for(int i = 0 ; i < n ; i++){\n int x = (nums[i] ^ k);\n if(x > nums[i]){\n ... | 1 | 0 | ['C++'] | 1 |
find-the-maximum-sum-of-node-values | Python, O(n), no edges needed, with intuition | python-on-no-edges-needed-with-intuition-rga1 | Intuition\nWe could think of our problem in next way: for each node we can take either nums[i] or nums[i]^k into the sum. We can think of node as being in even | raggzy | NORMAL | 2024-03-02T18:38:41.731036+00:00 | 2024-03-02T19:00:22.059403+00:00 | 194 | false | # Intuition\nWe could think of our problem in next way: for each node we can take either `nums[i]` or `nums[i]^k` into the sum. We can think of node as being in `even` and `odd` (modified) states. That is some `2**n` combinations. However some combinations are invalid. \n\nWe can prove, that required and sufficient con... | 1 | 0 | ['Dynamic Programming', 'Python3'] | 2 |
maximum-product-of-two-elements-in-an-array | C++ Biggest and Second Biggest | c-biggest-and-second-biggest-by-votrubac-ui88 | Just one linear scan to find the biggest m1, and the second biggest m2 numbers.\n\ncpp\nint maxProduct(vector<int>& nums) {\n\tauto m1 = 0, m2 = 0;\n\tfor (auto | votrubac | NORMAL | 2020-05-31T04:07:36.197286+00:00 | 2020-05-31T04:23:21.938363+00:00 | 15,429 | false | Just one linear scan to find the biggest `m1`, and the second biggest `m2` numbers.\n\n```cpp\nint maxProduct(vector<int>& nums) {\n\tauto m1 = 0, m2 = 0;\n\tfor (auto n: nums) {\n\t\tif (n > m1)\n m2 = exchange(m1, n);\n\t\telse\n\t\t\tm2 = max(m2, n);\n\t}\n\treturn (m1 - 1) * (m2 - 1);\n}\n``` | 141 | 3 | [] | 22 |
maximum-product-of-two-elements-in-an-array | [Java/Python 3] Find the max 2 numbers. | javapython-3-find-the-max-2-numbers-by-r-akgo | \n\njava\n public int maxProduct(int[] nums) {\n int mx1 = Integer.MIN_VALUE, mx2 = mx1;\n for (int n : nums) {\n if (n > mx1) {\n | rock | NORMAL | 2020-05-31T04:02:24.814215+00:00 | 2022-04-01T05:27:44.282474+00:00 | 13,554 | false | \n\n```java\n public int maxProduct(int[] nums) {\n int mx1 = Integer.MIN_VALUE, mx2 = mx1;\n for (int n : nums) {\n if (n > mx1) {\n mx2 = mx1;\n mx1 = n;\n }else if (n > mx2) {\n mx2 = n;\n }\n }\n return (mx1... | 82 | 8 | [] | 11 |
maximum-product-of-two-elements-in-an-array | ✅ Beats 100% - Explained with [ Video ] - Without Sorting- C++/Java/Python/JS - Visualized | beats-100-explained-with-video-without-s-r8rm | \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 | lancertech6 | NORMAL | 2023-12-12T01:13:43.515981+00:00 | 2023-12-12T01:51:15.316167+00:00 | 14,477 | false | \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... | 81 | 0 | ['Array', 'Two Pointers', 'Python', 'C++', 'Java', 'JavaScript'] | 7 |
maximum-product-of-two-elements-in-an-array | Python/JS/Go/C++ O(n) by linear scan. [w/ Comment] 有中文解題文章 | pythonjsgoc-on-by-linear-scan-w-comment-nvyrq | \u4E2D\u6587\u89E3\u984C\u6587\u7AE0\n\nO(n) by linear scan.\n\n---\n\nHint:\n\nMaintain a record of first largest number as well as second largest\n\n---\n\nIm | brianchiang_tw | NORMAL | 2020-06-22T06:23:27.001872+00:00 | 2023-12-12T07:30:28.270252+00:00 | 6,790 | false | [\u4E2D\u6587\u89E3\u984C\u6587\u7AE0](https://vocus.cc/article/65780468fd897800013c9ad1)\n\nO(n) by linear scan.\n\n---\n\n**Hint**:\n\nMaintain a record of first largest number as well as second largest\n\n---\n\n**Implementation**:\n\nPython:\n\n```\nclass Solution(object):\n def maxProduct(self, nums):\n\n ... | 48 | 0 | ['Math', 'C', 'Iterator', 'Python', 'C++', 'Go', 'Python3', 'JavaScript'] | 9 |
maximum-product-of-two-elements-in-an-array | Accepted Java O(N), O(1) (beats 100%) | accepted-java-on-o1-beats-100-by-pd93-nbk2 | \nTime : O(N)\nSpace : O(1)\nIterate array\n 1.) if cur val is more than max1, put curr val in max1 and max1 val in max2\n 2.) else if curr val is more th | pd93 | NORMAL | 2020-05-31T04:02:34.657969+00:00 | 2020-10-08T03:45:20.666647+00:00 | 7,912 | false | \nTime : O(N)\nSpace : O(1)\nIterate array\n 1.) if cur val is more than max1, put curr val in max1 and max1 val in max2\n 2.) else if curr val is more than max2, put curr val in max2\n\n```\nclass Solution {\n public int maxProduct(int[] nums) {\n int max1 = 0;\n int max2 = 0;\n for(int i... | 48 | 5 | ['Java'] | 6 |
maximum-product-of-two-elements-in-an-array | [JavaScript] Easy to understand - 3 solutions | javascript-easy-to-understand-3-solution-jhci | Since the integers in nums are in range [1, 10^3], the key to this problem is to find out the biggest and the second biggest number in nums.\n\n## SOLUTION 1\n\ | poppinlp | NORMAL | 2020-06-01T15:54:38.490669+00:00 | 2020-06-01T15:54:38.490746+00:00 | 2,654 | false | Since the integers in `nums` are in range `[1, 10^3]`, the key to this problem is to find out the biggest and the second biggest number in `nums`.\n\n## SOLUTION 1\n\nWe use 2 variables to maintain our targets during the traversal of `nums`. It\'s pretty straight forward.\n\n```js\nconst maxProduct = nums => {\n let m... | 36 | 3 | ['JavaScript'] | 4 |
maximum-product-of-two-elements-in-an-array | 【Video】Give me 5 minutes - 2 solutions - How we think about a solution | video-give-me-5-minutes-2-solutions-how-x7zey | Intuition\nKeep the largest number so far\n\n---\n\n# Solution Video\n\nhttps://youtu.be/JV5Kuinx-m0\n\n\u25A0 Timeline of the video\n\n0:05 Explain algorithm o | niits | NORMAL | 2023-12-12T00:44:00.111309+00:00 | 2023-12-13T06:13:29.667803+00:00 | 4,668 | false | # Intuition\nKeep the largest number so far\n\n---\n\n# Solution Video\n\nhttps://youtu.be/JV5Kuinx-m0\n\n\u25A0 Timeline of the video\n\n`0:05` Explain algorithm of Solution 1\n`3:18` Coding of solution 1\n`4:18` Time Complexity and Space Complexity of solution 1\n`4:29` Step by step algorithm of solution 1\n`4:36` Ex... | 30 | 0 | ['C++', 'Java', 'Python3', 'JavaScript'] | 4 |
maximum-product-of-two-elements-in-an-array | ✅☑[C++/Java/Python/JavaScript] || 3 Approaches || Beats 100% || EXPLAINED🔥 | cjavapythonjavascript-3-approaches-beats-1vys | PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n#### Approach 1(Brute Force)\n1. The function iterates through the elements | MarkSPhilip31 | NORMAL | 2023-12-12T00:21:52.167242+00:00 | 2023-12-12T00:21:52.167266+00:00 | 4,148 | false | # PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n#### ***Approach 1(Brute Force)***\n1. The function iterates through the elements in the `nums` array using two nested loops (`i` and `j`) to consider all possible pairs of elements.\n1. For each pair of elements (`nums[i], nums[... | 27 | 0 | ['Array', 'C', 'Sorting', 'Heap (Priority Queue)', 'C++', 'Java', 'Python3', 'JavaScript'] | 4 |
maximum-product-of-two-elements-in-an-array | C++ || 7 different approaches || lots of STL and the optimal solution || clean code | c-7-different-approaches-lots-of-stl-and-e18m | If you are looking for the best solution to the problem skip to approach 7. That said the other approach outline some different ideas that could be useful in si | heder | NORMAL | 2022-10-21T17:58:11.246140+00:00 | 2025-01-02T16:49:19.521579+00:00 | 1,562 | false | If you are looking for the best solution to the problem skip to approach 7. That said the other approach outline some different ideas that could be useful in similar situations.
Please let me know if you have more ideas or suggestions for one of the approaches below.
**Please upvote if you learned something new. :)**... | 17 | 0 | ['C'] | 2 |
maximum-product-of-two-elements-in-an-array | C++ || O(n) solution || Easy Approach | c-on-solution-easy-approach-by-himanshut-o8fs | ```\nclass Solution {\npublic:\n int maxProduct(vector& nums) {\n int max1 = INT_MIN;\n int flag = 0;\n for(int i = 0;i<nums.size();i++) | himanshutiwariji | NORMAL | 2022-01-08T21:11:33.586681+00:00 | 2022-01-08T21:11:33.586721+00:00 | 1,545 | false | ```\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int max1 = INT_MIN;\n int flag = 0;\n for(int i = 0;i<nums.size();i++){\n if(max1 < nums[i]){\n max1 = nums[i];\n flag = i;\n }\n }\n int max2= INT_MIN;\n ... | 14 | 0 | ['C'] | 0 |
maximum-product-of-two-elements-in-an-array | ✅✅|| EASY PEASY || C#, Java, Python || 🔥🔥🔥 | easy-peasy-c-java-python-by-ilxamovic-41tl | Intuition\nThe problem involves finding the maximum product of two elements in an array. The provided code suggests an approach of iteratively updating the two | ilxamovic | NORMAL | 2023-12-12T04:19:43.605609+00:00 | 2023-12-12T04:19:43.605640+00:00 | 1,169 | false | # Intuition\nThe problem involves finding the maximum product of two elements in an array. The provided code suggests an approach of iteratively updating the two largest elements in the array as we traverse through it.\n\n# Approach\n1. Initialize two variables, `max1` and `max2`, to store the two largest elements in t... | 13 | 0 | ['Java', 'Python3', 'C#'] | 1 |
maximum-product-of-two-elements-in-an-array | C++ O(n) | c-on-by-ayushme007-sroz | \nclass Solution {\npublic:\n int maxProduct(vector<int>& a) {\n \n int i,max1,max2;\n max1=max2=INT_MIN;\n for(int i=0;i<a.size( | ayushme007 | NORMAL | 2020-06-04T12:03:59.822843+00:00 | 2022-03-05T09:16:29.389654+00:00 | 2,117 | false | ```\nclass Solution {\npublic:\n int maxProduct(vector<int>& a) {\n \n int i,max1,max2;\n max1=max2=INT_MIN;\n for(int i=0;i<a.size();i++){\n if(a[i]>max1){\n max2 = max1;\n max1 = a[i];\n }\n else if(a[i]>max2){\n ... | 13 | 0 | [] | 5 |
maximum-product-of-two-elements-in-an-array | 【Video】Give me 5 minutes - 2 solutions - How we think about a solution | video-give-me-5-minutes-2-solutions-how-ock3g | Intuition\nKeep the largest number so far\n\n---\n\n# Solution Video\n\nhttps://youtu.be/JV5Kuinx-m0\n\n\u25A0 Timeline of the video\n\n0:05 Explain algorithm o | niits | NORMAL | 2024-05-19T13:47:36.902899+00:00 | 2024-05-19T13:47:36.902931+00:00 | 463 | false | # Intuition\nKeep the largest number so far\n\n---\n\n# Solution Video\n\nhttps://youtu.be/JV5Kuinx-m0\n\n\u25A0 Timeline of the video\n\n`0:05` Explain algorithm of Solution 1\n`3:18` Coding of solution 1\n`4:18` Time Complexity and Space Complexity of solution 1\n`4:29` Step by step algorithm of solution 1\n`4:36` Ex... | 12 | 0 | ['C++', 'Java', 'Python3', 'JavaScript'] | 0 |
maximum-product-of-two-elements-in-an-array | [Python] Simple & Faster than 99.27% of submissions | python-simple-faster-than-9927-of-submis-zwpv | \nclass Solution(object):\n def maxProduct(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n max_1 = max(nu | parkershamblin | NORMAL | 2020-06-25T09:21:40.653333+00:00 | 2020-06-25T09:22:53.006348+00:00 | 2,320 | false | ```\nclass Solution(object):\n def maxProduct(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n max_1 = max(nums)\n nums.remove(max_1)\n max_2 = max(nums)\n return (max_1-1)*(max_2-1)\n\t\n``` | 12 | 1 | ['Python', 'Python3'] | 1 |
maximum-product-of-two-elements-in-an-array | 3 Approach to solve using C++ STL | 3-approach-to-solve-using-c-stl-by-akb7-n1me | Before getting into the problem this is somewhat easy problem and introduction to concept called as Heap.\n\nHere we need to get the maximum product so sort the | akb7 | NORMAL | 2021-07-09T06:02:50.735513+00:00 | 2021-07-09T06:02:50.735559+00:00 | 1,506 | false | *Before getting into the problem this is somewhat easy problem and introduction to concept called as **Heap**.*\n\nHere we need to get the maximum product so sort the array and multiply last-1 and penultimate number - 1 will give us the answer.\n\nTime Complexity: O(nlogn).\n```\nclass Solution {\npublic:\n int maxP... | 11 | 0 | ['C', 'Heap (Priority Queue)', 'C++'] | 2 |
maximum-product-of-two-elements-in-an-array | simple python solution | simple-python-solution-by-kedaar_kalyan-sg4i | ```\n def maxProduct(self, nums: List[int]) -> int:\n nums.sort()\n return (nums[-1]-1)*(nums[-2]-1) | Kedaar_Kalyan | NORMAL | 2020-12-02T12:22:29.583649+00:00 | 2020-12-02T12:22:29.583675+00:00 | 1,055 | false | ```\n def maxProduct(self, nums: List[int]) -> int:\n nums.sort()\n return (nums[-1]-1)*(nums[-2]-1) | 10 | 1 | [] | 2 |
maximum-product-of-two-elements-in-an-array | 2 C++ 1 pass, priority_queue O(1) vs 3 Python 1-line codes|| 0ms beats 100% | 2-c-1-pass-priority_queue-o1-vs-3-python-gkxk | Intuition\n Describe your first thoughts on how to solve this problem. \nIterate the nums to find the max & 2nd max.\n\n2nd O(1) C++ solution uses priority_queu | anwendeng | NORMAL | 2023-12-12T04:54:57.307053+00:00 | 2023-12-12T10:58:38.247155+00:00 | 1,633 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIterate the `nums` to find the max & 2nd max.\n\n2nd O(1) C++ solution uses priority_queue of size at most 3 which is also fast & beats 100%.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nPython codes are provided ... | 9 | 0 | ['Array', 'Sorting', 'Heap (Priority Queue)', 'C++', 'Python3'] | 2 |
maximum-product-of-two-elements-in-an-array | Python 3 -> Using heap | python-3-using-heap-by-mybuddy29-0k3i | Suggestions to make it better are always welcomed.\n\n\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n # approach 1: find 2 max num | mybuddy29 | NORMAL | 2022-04-23T17:11:35.306319+00:00 | 2022-04-23T17:12:34.964770+00:00 | 1,522 | false | **Suggestions to make it better are always welcomed.**\n\n```\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n # approach 1: find 2 max numbers in 2 loops. T = O(n). S = O(1)\n\t\t# approach 2: sort and then get the last 2 max elements. T = O(n lg n). S = O(1)\n\t\t# approach 3: build min he... | 9 | 0 | ['Heap (Priority Queue)', 'Python3'] | 4 |
maximum-product-of-two-elements-in-an-array | Max and Second Max number in Array | max-and-second-max-number-in-array-by-hi-5fzq | public int maxProduct(int[] nums) {\n \n int max = Integer.MIN_VALUE;\n int secondMax = Integer.MIN_VALUE;\n \n for(int i = | hitler069 | NORMAL | 2020-07-04T16:31:13.089192+00:00 | 2020-07-04T16:31:13.089222+00:00 | 14,115 | false | public int maxProduct(int[] nums) {\n \n int max = Integer.MIN_VALUE;\n int secondMax = Integer.MIN_VALUE;\n \n for(int i = 0;i< nums.length;i++){\n \n if(nums[i] > max){ \n secondMax = max;\n max = nums[i];\n }else if... | 9 | 1 | [] | 1 |
maximum-product-of-two-elements-in-an-array | ✅Beats 100 % Easy Clean Code✅✅✅ | beats-100-easy-clean-code-by-sourav_n06-k4l2 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Tim | sourav_n06 | NORMAL | 2023-12-12T02:26:18.086093+00:00 | 2023-12-12T02:26:18.086130+00:00 | 1,132 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time compl... | 8 | 1 | ['Array', 'Sorting', 'Heap (Priority Queue)', 'C++', 'Java', 'JavaScript'] | 2 |
maximum-product-of-two-elements-in-an-array | ✅ 2 line solution || ✅ simple || ✅ python | 2-line-solution-simple-python-by-naveen_-kutf | Intuition\nThe problem asks for the maximum product of two distinct elements in the given list. Sorting the list in ascending order allows us to access the two | naveen_kumar_g | NORMAL | 2023-12-12T01:41:01.472626+00:00 | 2023-12-12T01:41:01.472663+00:00 | 1,224 | false | # Intuition\nThe problem asks for the maximum product of two distinct elements in the given list. Sorting the list in ascending order allows us to access the two largest elements at the end of the sorted list.\n\n\n# Approach\nThe approach involves sorting the input list in ascending order. After sorting, the two large... | 8 | 0 | ['Python3'] | 5 |
maximum-product-of-two-elements-in-an-array | ✅Java Simple Solution || ✅Runtime 0ms || ✅ Beats100% | java-simple-solution-runtime-0ms-beats10-4qn8 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | ahmedna126 | NORMAL | 2023-08-01T14:02:12.458979+00:00 | 2023-11-07T11:44:23.208589+00:00 | 453 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 8 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | [JavaScript] Elegant one line solution using sort and reduce (with explanation) | javascript-elegant-one-line-solution-usi-d18z | Unfortunately, due use of .sort() method time complexity is \u0398(n^2) at best\n\njavascript\nconst maxProduct = (nums) => nums.sort((a, b) => a - b).splice(-2 | mrhyde | NORMAL | 2020-10-30T07:09:31.130636+00:00 | 2020-10-30T07:16:53.330023+00:00 | 608 | false | Unfortunately, due use of `.sort()` method time complexity is \u0398(n^2) at best\n\n```javascript\nconst maxProduct = (nums) => nums.sort((a, b) => a - b).splice(-2).reduce((i, j) => (i-1)*(j-1))\n```\n\nExplanation:\n1. `.sort((a, b) => a - b)` - sort array elements in ascending order (by default it sorts elements al... | 8 | 1 | ['JavaScript'] | 0 |
maximum-product-of-two-elements-in-an-array | Python O(n) | python-on-by-minionjosh-u4z0 | \n def maxProduct(self, nums: List[int]) -> int:\n \n max1, max2 = -1, -1\n for n in nums:\n if n >= max1:\n m | minionjosh | NORMAL | 2020-07-31T18:45:01.799839+00:00 | 2020-07-31T18:45:01.799872+00:00 | 694 | false | ```\n def maxProduct(self, nums: List[int]) -> int:\n \n max1, max2 = -1, -1\n for n in nums:\n if n >= max1:\n max1, max2 = n, max1\n elif n > max2:\n max2 = n\n \n \n return (max1-1) * (max2-1)\n``` | 8 | 0 | ['Python'] | 0 |
maximum-product-of-two-elements-in-an-array | 2 methods || sorting || priority queue || Brute force to optimized | 2-methods-sorting-priority-queue-brute-f-g2uo | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n 1. sort and access | ankitraj15 | NORMAL | 2023-06-16T05:47:20.701664+00:00 | 2023-06-16T05:47:20.701695+00:00 | 727 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n 1. sort and access last 2 elements\n 2. use max heap i.e. priority queue and acess top 2 elements. \n\n# Complexity\n- Time complexity:\n<!-- Add your time comp... | 7 | 0 | ['Array', 'Sorting', 'Heap (Priority Queue)', 'C++'] | 2 |
maximum-product-of-two-elements-in-an-array | C++ ONE PASS 100% time 99.9% space (with explanation) | c-one-pass-100-time-999-space-with-expla-81pj | Here variable a will store max value of array nums, while b will store 2nd max value\n\n\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n | joshuako | NORMAL | 2021-03-27T09:00:55.264572+00:00 | 2021-03-27T09:00:55.264600+00:00 | 637 | false | Here variable `a` will store max value of array `nums`, while `b` will store 2nd max value\n\n```\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n int a = 0, b = 0;\n for (int i = 0;i < nums.size();i++) {\n if (nums[i] > a) {\n b = a; // delegate previous ma... | 7 | 0 | [] | 0 |
maximum-product-of-two-elements-in-an-array | 1 liner - Python 3 | 1-liner-python-3-by-mb557x-nn1h | Approach:\nThe list is first sorted. After sorting, 1 is subtracted from the last and the second last elements and their product is returned.\n\nclass Solution: | mb557x | NORMAL | 2020-07-15T05:47:28.645441+00:00 | 2020-07-15T05:47:28.645474+00:00 | 856 | false | Approach:\nThe list is first sorted. After sorting, ```1``` is subtracted from the last and the second last elements and their **product** is returned.\n```\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n return ((sorted(nums)[-1]) - 1) * ((sorted(nums)[-2] - 1))\n```\n```\n#Runtime: 56ms\n... | 7 | 3 | ['Python3'] | 3 |
maximum-product-of-two-elements-in-an-array | Best 3 Approaches [0ms , 7ms , 9ms] [Java] | best-3-approaches-0ms-7ms-9ms-java-by-cr-w92c | \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 | crusifixx | NORMAL | 2022-11-24T08:37:21.133152+00:00 | 2023-01-08T14:06:32.538074+00:00 | 1,235 | false | \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 ... | 6 | 0 | ['Heap (Priority Queue)', 'Java'] | 1 |
maximum-product-of-two-elements-in-an-array | Simple Java Solution, 100% Faster (Easy to Understand) | simple-java-solution-100-faster-easy-to-fbld7 | Time Complexity : O(N)\n\nclass Solution {\n public int maxProduct(int[] nums) {\n \n int max = Integer.MIN_VALUE;\n int sec_max = Integ | Shiv_Mishra | NORMAL | 2021-10-08T10:14:23.609154+00:00 | 2021-10-08T10:14:23.609199+00:00 | 1,039 | false | Time Complexity : O(N)\n```\nclass Solution {\n public int maxProduct(int[] nums) {\n \n int max = Integer.MIN_VALUE;\n int sec_max = Integer.MIN_VALUE;\n \n for(int i=0;i<nums.length;i++){\n if(nums[i] > max){\n sec_max = max;\n max = nums[... | 6 | 0 | ['Array', 'Java'] | 2 |
maximum-product-of-two-elements-in-an-array | JavaScript solution: 86% faster, 100% less memory | javascript-solution-86-faster-100-less-m-k9l4 | \nvar maxProduct = function(nums) {\n nums.sort((a,b)=>b-a);\n return (nums[0]-1)*(nums[1]-1);\n};\n | cyberain | NORMAL | 2020-06-04T07:40:47.275872+00:00 | 2020-06-04T07:40:47.275920+00:00 | 616 | false | ```\nvar maxProduct = function(nums) {\n nums.sort((a,b)=>b-a);\n return (nums[0]-1)*(nums[1]-1);\n};\n``` | 6 | 1 | ['JavaScript'] | 2 |
maximum-product-of-two-elements-in-an-array | [Python] Built-in sort method (100 % speed, 100 % mem) | python-built-in-sort-method-100-speed-10-lhtl | Built-in sort method \n\nWhy write efficient code when it\'s been written for you? \n\n\nclass Solution(object):\n def maxProduct(self, nums):\n nums. | drblessing | NORMAL | 2020-05-31T12:49:53.104970+00:00 | 2020-05-31T12:50:42.755790+00:00 | 767 | false | # Built-in sort method \n\nWhy write efficient code when it\'s been written for you? \n\n```\nclass Solution(object):\n def maxProduct(self, nums):\n nums.sort()\n return (nums[-1] -1) * (nums[-2]-1)\n """\n :type nums: List[int]\n :rtype: int\n """\n \n```\n\n## **10... | 6 | 4 | ['Python', 'Python3'] | 3 |
maximum-product-of-two-elements-in-an-array | 2 Different Approach using Sorting and Priority Queue (Heaps) | 2-different-approach-using-sorting-and-p-tzvb | First Approach Using Sorting \n# Intuition\nTo maximize the product (nums[i]\u22121)\xD7(nums[j]\u22121), you need to find the two largest numbers in the array | _sxrthakk | NORMAL | 2024-07-20T04:55:50.295934+00:00 | 2024-07-20T04:55:50.295966+00:00 | 849 | false | # First Approach Using Sorting \n# Intuition\nTo maximize the product (nums[i]\u22121)\xD7(nums[j]\u22121), you need to find the two largest numbers in the array since subtracting 1 from a larger number will still result in a relatively large value. Sorting the array helps to easily identify the two largest numbers, an... | 5 | 0 | ['Array', 'Sorting', 'Heap (Priority Queue)', 'C++'] | 0 |
maximum-product-of-two-elements-in-an-array | 【Video】Give me 5 minutes - 2 solutions - How we think about a solution | video-give-me-5-minutes-2-solutions-how-2rd1w | Intuition\nKeep the largest number so far\n\n---\n\n# Solution Video\n\nhttps://youtu.be/JV5Kuinx-m0\n\n\u25A0 Timeline of the video\n\n0:05 Explain algorithm o | niits | NORMAL | 2024-04-24T16:34:39.436356+00:00 | 2024-04-24T16:34:39.436386+00:00 | 367 | false | # Intuition\nKeep the largest number so far\n\n---\n\n# Solution Video\n\nhttps://youtu.be/JV5Kuinx-m0\n\n\u25A0 Timeline of the video\n\n`0:05` Explain algorithm of Solution 1\n`3:18` Coding of solution 1\n`4:18` Time Complexity and Space Complexity of solution 1\n`4:29` Step by step algorithm of solution 1\n`4:36` Ex... | 5 | 0 | ['C++', 'Java', 'Python3', 'JavaScript'] | 0 |
maximum-product-of-two-elements-in-an-array | 🚀🚀 Beats 100% | 0ms 🔥🔥 | beats-100-0ms-by-sanket_deshmukh_07-nkbn | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | The_Eternal_Soul | NORMAL | 2023-12-12T03:37:01.203847+00:00 | 2023-12-12T03:37:01.203869+00:00 | 434 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\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. $... | 5 | 0 | ['Array', 'C', 'Sorting', 'Heap (Priority Queue)', 'Python', 'C++', 'Java', 'Python3', 'JavaScript'] | 1 |
maximum-product-of-two-elements-in-an-array | 💥|| EXPLAINED IN HINGLISH || OPTIMAL || BEGINNER FRIENDLY || 💥 | explained-in-hinglish-optimal-beginner-f-5vrb | 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 | Sautramani | NORMAL | 2023-12-12T03:22:43.133607+00:00 | 2023-12-12T03:27:37.957017+00:00 | 279 | false | # 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 hatayei... | 5 | 0 | ['Array', 'Sorting', 'Heap (Priority Queue)', 'Python', 'C++', 'Java'] | 3 |
maximum-product-of-two-elements-in-an-array | Java One Liner Solution | java-one-liner-solution-by-janhvi__28-qwpt | \nclass Solution {\n public int maxProduct(int[] nums) {\n Arrays.sort(nums);\n return (nums[nums.length-1]-1)*(nums[nums.length-2]-1);\n }\ | Janhvi__28 | NORMAL | 2022-10-23T18:23:18.030333+00:00 | 2022-10-23T18:23:18.030388+00:00 | 620 | false | ```\nclass Solution {\n public int maxProduct(int[] nums) {\n Arrays.sort(nums);\n return (nums[nums.length-1]-1)*(nums[nums.length-2]-1);\n }\n}\n``` | 5 | 0 | ['Java'] | 1 |
maximum-product-of-two-elements-in-an-array | [C++] 2 solns | c-2-solns-by-turbotorquetiger-t810 | \n// Sorting Soln\n// Time: O(N.logN)\n// Space: O(1)\n\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) \n {\n sort(nums.begin(), nu | ihavehiddenmyid | NORMAL | 2021-07-08T11:45:56.469347+00:00 | 2021-07-08T11:45:56.469389+00:00 | 469 | false | ```\n// Sorting Soln\n// Time: O(N.logN)\n// Space: O(1)\n\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) \n {\n sort(nums.begin(), nums.end());\n return (nums[nums.size() - 1] - 1) * (nums[nums.size() - 2] - 1);\n }\n};\n```\n```\n// Heaps Soln\n// Time: O(N.logK)\n// Space: O(N)... | 5 | 0 | ['C', 'Sorting', 'Heap (Priority Queue)', 'C++'] | 3 |
maximum-product-of-two-elements-in-an-array | Very EASY java solution | very-easy-java-solution-by-gauravtaparia-ey0f | class Solution {\n public int maxProduct(int[] nums) {\n Arrays.sort(nums);\n return ((nums[nums.length-1]-1)*(nums[nums.length-2]-1));\n }\ | GauravTaparia | NORMAL | 2021-06-25T18:18:00.357075+00:00 | 2021-06-25T18:18:00.357120+00:00 | 580 | false | class Solution {\n public int maxProduct(int[] nums) {\n Arrays.sort(nums);\n return ((nums[nums.length-1]-1)*(nums[nums.length-2]-1));\n }\n} | 5 | 1 | ['Java'] | 1 |
maximum-product-of-two-elements-in-an-array | 2 lines java | 2-lines-java-by-logesh056-7fdl | {Arrays.sort(nums);\n int l=nums.length-1;\n return nums.length>1?((nums[l-1]-1)*(nums[l]-1)):0;\n } | logesh056 | NORMAL | 2020-07-10T10:25:38.678414+00:00 | 2020-07-10T10:25:38.678446+00:00 | 614 | false | {Arrays.sort(nums);\n int l=nums.length-1;\n return nums.length>1?((nums[l-1]-1)*(nums[l]-1)):0;\n } | 5 | 1 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | Python Solution - 90% Speed - O(n) Time, O(1) Space Complexity | python-solution-90-speed-on-time-o1-spac-76s7 | Python Solution - 90% Speed - O(n) Time, O(1) Space Complexity\n\nThe code below tracks two variables (a,b) representing the two highest values found in the arr | aragorn_ | NORMAL | 2020-07-02T16:23:01.936987+00:00 | 2021-05-19T23:31:19.154437+00:00 | 1,190 | false | **Python Solution - 90% Speed - O(n) Time, O(1) Space Complexity**\n\nThe code below tracks two variables (a,b) representing the two highest values found in the array. After one linear pass, we can use these values to return the answer.\nCheers,\n```\nclass Solution:\n def maxProduct(self, nums):\n a = b = fl... | 5 | 0 | ['Python', 'Python3'] | 3 |
maximum-product-of-two-elements-in-an-array | For Absolute Beginner !!! Must See Solution | for-absolute-beginner-must-see-solution-mqwv5 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n- First we sort the whole array\n- with the help of for loop we will take | RamHD | NORMAL | 2024-06-12T15:45:47.470042+00:00 | 2024-06-12T15:45:47.470085+00:00 | 7 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- First we sort the whole array\n- with the help of for loop we will take last and second last number present in sorted array\n- simply do the needful in the end\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexit... | 4 | 0 | ['C++'] | 0 |
maximum-product-of-two-elements-in-an-array | 🔥🔥 Explicación y Resolución Del Ejercicio En Español Java | Python | PHP | JavaScript | C++ 🔥🔥 | explicacion-y-resolucion-del-ejercicio-e-opiu | Lectura\nProducto m\xE1ximo de dos elementos en un array(Java, Python, PHP, C++, Javascript)\n\nDado un array nums, obtenga los 2 valores mayores y devuelva el | hermescoder02 | NORMAL | 2023-12-15T04:33:14.189568+00:00 | 2024-03-22T19:08:09.868964+00:00 | 32 | false | # Lectura\n*Producto m\xE1ximo de dos elementos en un array(Java, Python, PHP, C++, Javascript)*\n\nDado un array nums, obtenga los 2 valores mayores y devuelva el siguiente resultado :\n\n (mayor - 1) * (segundo_mayor - 1)\n\n. Space complexity: O(1).\n\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n return prod | MikPosp | NORMAL | 2023-12-12T12:09:04.539270+00:00 | 2023-12-12T12:50:57.057959+00:00 | 273 | false | # Code #1 - The Fastest\nTime complexity: $$O(n)$$. Space complexity: $$O(1)$$.\n```\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n return prod(x-1 for x in nlargest(2,nums)) \n```\n\n# Code #2 - The Least Efficient\nTime complexity: $$O(n*log(n))$$. Space complexity: $$O(n)$$.\n```... | 4 | 0 | ['Array', 'Sorting', 'Heap (Priority Queue)', 'Python', 'Python3'] | 0 |
maximum-product-of-two-elements-in-an-array | C++ || Easy Solution || O(N) Solution | c-easy-solution-on-solution-by-deepak140-gdag | Intuition\nLet us say the pair is nums[i] and nums[j] (where nums[i]>=nums[j]) where i!=j and 0<=i,j=mx we found an element greater than mx so our mx is nums[i] | deepak1408 | NORMAL | 2023-12-12T07:29:26.141000+00:00 | 2023-12-12T07:29:26.141027+00:00 | 570 | false | # Intuition\nLet us say the pair is nums[i] and nums[j] (where nums[i]>=nums[j]) where i!=j and 0<=i,j<n.\n(nums[i]-1)*(nums[j]-1) will be maximum if nums[i] and nums[j] are the first and second maximum elements in the array respectively.\n\n# Approach\n- **Sorting:**\nLet us sort the array last two elements will be ou... | 4 | 0 | ['C++'] | 0 |
maximum-product-of-two-elements-in-an-array | Java | Find 2 max nums | Simple | Clean code | java-find-2-max-nums-simple-clean-code-b-bcmm | 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# Co | judgementdey | NORMAL | 2023-12-12T06:32:28.845380+00:00 | 2023-12-12T06:32:28.845413+00:00 | 345 | false | # 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 {\n public int maxProduct(int[] nums) {\n int max1 = 0, max2 = 0;\n\n for (var n : nums) {\n ... | 4 | 0 | ['Array', 'Java'] | 0 |
maximum-product-of-two-elements-in-an-array | Python3 Solution | python3-solution-by-motaharozzaman1996-9evf | \n\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n nums.sort()\n return (nums[-1]-1)*(nums[-2]-1)\n | Motaharozzaman1996 | NORMAL | 2023-12-12T03:03:34.519922+00:00 | 2023-12-12T03:03:34.519950+00:00 | 273 | false | \n```\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n nums.sort()\n return (nums[-1]-1)*(nums[-2]-1)\n``` | 4 | 0 | ['Python', 'Python3'] | 1 |
maximum-product-of-two-elements-in-an-array | 0ms||beats 100%users||beginner friendly||easy approach | 0msbeats-100usersbeginner-friendlyeasy-a-seey | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Hitheash | NORMAL | 2023-12-12T01:53:08.460590+00:00 | 2023-12-12T01:53:08.460625+00:00 | 441 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 4 | 0 | ['C++'] | 3 |
maximum-product-of-two-elements-in-an-array | Java | 2 liner | Easy to Understand | for Beginners | 0 ms | fast💡 | java-2-liner-easy-to-understand-for-begi-l397 | Approach\n- using quicksort to sort nums\n- returning [last element - 1] * [second last element - 1]\n\n# Complexity\n- Time complexity: O(n log n)\n\n- Space c | AbirDey | NORMAL | 2023-04-22T05:12:53.823285+00:00 | 2023-04-22T05:12:53.823306+00:00 | 379 | false | # Approach\n- using quicksort to sort nums\n- returning [last element - 1] * [second last element - 1]\n\n# Complexity\n- Time complexity: O(n log n)\n\n- Space complexity: O(log n)\n\n# Code\n```\nclass Solution {\n public int maxProduct(int[] nums) {\n Arrays.sort(nums);\n return (nums[nums.length-1]... | 4 | 0 | ['Array', 'Sorting', 'Java'] | 0 |
maximum-product-of-two-elements-in-an-array | ☕ Java Solution | O(n) approach | Runtime 0 ms | Beats 100% 📕 | java-solution-on-approach-runtime-0-ms-b-ohqf | 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 | B10nicle | NORMAL | 2023-02-25T16:20:27.054839+00:00 | 2023-02-26T18:22:21.303538+00:00 | 552 | false | # 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... | 4 | 0 | ['Java'] | 1 |
maximum-product-of-two-elements-in-an-array | EASY AND NAIVE SOLUTION USING C++ | easy-and-naive-solution-using-c-by-king_-1dax | Intuition\n Describe your first thoughts on how to solve this problem. \nNO NEED OF SORTING\n# Approach\n Describe your approach to solving the problem. \nTwo V | King_of_Kings101 | NORMAL | 2022-12-27T14:02:56.162692+00:00 | 2022-12-27T14:04:54.808495+00:00 | 520 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nNO NEED OF SORTING\n# Approach\n<!-- Describe your approach to solving the problem. -->\nTwo Variable and one time traversal to array.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ --> O(n)\n\n- Spac... | 4 | 0 | ['Array', 'C++'] | 0 |
maximum-product-of-two-elements-in-an-array | Java solution with 0ms runtime | Beats 100% | No Sorting | java-solution-with-0ms-runtime-beats-100-v2de | If you found it easy to understand, Please do upvote :)\nThankyou!!\n------------------------------------------------------------------------------------------- | JavithSadhamHussain | NORMAL | 2022-12-03T11:27:22.842788+00:00 | 2022-12-03T11:27:22.842838+00:00 | 563 | false | **If you found it easy to understand, Please do upvote :)\nThankyou!!**\n**---------------------------------------------------------------------------------------------------------------**\n\n\n**-------------... | 4 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | Java Solution | Two Approaches - Heap based and find the 2 max numbers. | java-solution-two-approaches-heap-based-wanee | Heap Based Solution : Time complexity : O(n)\n\n\nclass Solution {\n\n public int maxProduct(int[] nums) {\n PriorityQueue<Integer> pq = new PriorityQ | 1605448777 | NORMAL | 2022-08-20T18:03:04.442995+00:00 | 2022-08-20T18:03:04.443044+00:00 | 684 | false | **Heap Based Solution :** Time complexity : O(n)\n\n```\nclass Solution {\n\n public int maxProduct(int[] nums) {\n PriorityQueue<Integer> pq = new PriorityQueue<>();\n for (int i = 0; i < 2; i++) pq.add(nums[i]);\n for (int i = 2; i < nums.length; i++) {\n if (pq.peek() < nums[i]) {\... | 4 | 0 | ['Heap (Priority Queue)', 'Java'] | 0 |
maximum-product-of-two-elements-in-an-array | Heap Java Solution | heap-java-solution-by-codertuhin094-07nk | \nclass Solution {\n public int maxProduct(int[] nums) {\n PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());\n for ( | codertuhin094 | NORMAL | 2021-08-25T08:28:29.012399+00:00 | 2021-08-25T08:28:29.012439+00:00 | 237 | false | ```\nclass Solution {\n public int maxProduct(int[] nums) {\n PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());\n for (int i =0;i<nums.length;i++){\n pq.add(nums[i]);\n }\n\n int n1 = pq.poll();\n int n2 = pq.poll();\n\n return (n1-1)*(n... | 4 | 0 | [] | 0 |
maximum-product-of-two-elements-in-an-array | Java beats 100% without sorting | java-beats-100-without-sorting-by-red_hu-a28l | \nclass Solution {\n public int maxProduct(int[] nums) {\n int max1 = nums[0]>nums[1]?nums[0]:nums[1];\n int max2 = nums[0]<nums[1]?nums[0]:num | red_hue | NORMAL | 2021-08-20T09:16:09.337783+00:00 | 2021-08-20T09:16:09.337811+00:00 | 220 | false | ```\nclass Solution {\n public int maxProduct(int[] nums) {\n int max1 = nums[0]>nums[1]?nums[0]:nums[1];\n int max2 = nums[0]<nums[1]?nums[0]:nums[1];\n for(int i=2;i<nums.length;i++){\n if(nums[i]>max1){\n max2=max1;\n max1=nums[i]; \n ... | 4 | 0 | [] | 0 |
maximum-product-of-two-elements-in-an-array | [C++]| 0 ms| Faster than 100% | Speedy solution | c-0-ms-faster-than-100-speedy-solution-b-b04n | ```\nint maxProduct(vector& nums) {\n int max1=-1,max2=-1;\n for(auto i: nums){\n auto temp = max(i , min(max1,max2));\n max2 = max( | dexter_2000 | NORMAL | 2021-06-06T07:11:50.339946+00:00 | 2021-06-06T07:11:50.339993+00:00 | 436 | false | ```\nint maxProduct(vector<int>& nums) {\n int max1=-1,max2=-1;\n for(auto i: nums){\n auto temp = max(i , min(max1,max2));\n max2 = max(max1,max2);\n max1=temp;\n }\n return (max1-1)*(max2-1);\n } | 4 | 1 | ['C', 'C++'] | 1 |
maximum-product-of-two-elements-in-an-array | Java O(n) solution, 100% faster | java-on-solution-100-faster-by-backend_e-1i0x | ```\n public int maxProduct(int[] nums) {\n int len = nums.length;\n if(len == 1){\n return nums[0];\n }\n int first = I | Backend_engineer123 | NORMAL | 2021-03-24T17:45:08.910284+00:00 | 2021-03-24T17:46:12.735679+00:00 | 314 | false | ```\n public int maxProduct(int[] nums) {\n int len = nums.length;\n if(len == 1){\n return nums[0];\n }\n int first = Integer.MIN_VALUE;\n int second = Integer.MIN_VALUE;\n\n if(len == 2){\n return (nums[0]-1) * (nums[1]-1);\n }\n for(int i ... | 4 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | C++ Short and Simple O(n) Solution | c-short-and-simple-on-solution-by-yehudi-aawq | \nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n // Find first max\n int m1_ind = max_element(nums.begin(), nums.end()) - nu | yehudisk | NORMAL | 2020-12-17T15:13:22.697089+00:00 | 2020-12-17T15:13:22.697131+00:00 | 272 | false | ```\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n // Find first max\n int m1_ind = max_element(nums.begin(), nums.end()) - nums.begin();\n int m1 = nums[m1_ind]-1;\n nums[m1_ind] = 0;\n // Find second max\n int m2_ind = max_element(nums.begin(), nums.end... | 4 | 3 | ['C'] | 1 |
maximum-product-of-two-elements-in-an-array | JAVA | 2-line Solution | java-2-line-solution-by-onefineday01-cfi2 | \nclass Solution {\n public int maxProduct(int[] nums) {\n Arrays.sort(nums);\n return Math.max(((nums[0]-1)*(nums[1]-1)), ((nums[nums.length-2 | onefineday01 | NORMAL | 2020-06-07T10:36:39.866574+00:00 | 2020-06-07T10:37:43.864775+00:00 | 489 | false | ```\nclass Solution {\n public int maxProduct(int[] nums) {\n Arrays.sort(nums);\n return Math.max(((nums[0]-1)*(nums[1]-1)), ((nums[nums.length-2]-1)*(nums[nums.length-1]-1)));\n }\n}\n```\n**do upvote if you like the solution** | 4 | 1 | ['Java'] | 1 |
maximum-product-of-two-elements-in-an-array | C++ | Largest and second Largest | Commented with explanation | c-largest-and-second-largest-commented-w-fj67 | Find the largest and second largest from the array, by traversing the array once\n It is possible that the largest number = second largest number, eg. [1,5,4,5] | ayzastark | NORMAL | 2020-06-03T08:55:35.796090+00:00 | 2020-06-03T08:55:35.796135+00:00 | 489 | false | * Find the largest and second largest from the array, by traversing the array *once*\n* It is possible that the largest number = second largest number, eg. ```[1,5,4,5]``` \n\n```\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) \n {\n int max1 = INT_MIN; //largest number\n int max2 = ... | 4 | 0 | ['C', 'C++'] | 0 |
maximum-product-of-two-elements-in-an-array | Java all positive or negative | java-all-positive-or-negative-by-hobiter-0i66 | \npublic int maxProduct(int[] nums) {\n Arrays.sort(nums);\n return Math.max((nums[0] - 1) * (nums[1] - 1), (nums[nums.length - 1] - 1) * (nums[nu | hobiter | NORMAL | 2020-05-31T04:01:05.048202+00:00 | 2020-05-31T04:01:05.048387+00:00 | 480 | false | ```\npublic int maxProduct(int[] nums) {\n Arrays.sort(nums);\n return Math.max((nums[0] - 1) * (nums[1] - 1), (nums[nums.length - 1] - 1) * (nums[nums.length - 2] - 1));\n }\n``` | 4 | 2 | [] | 0 |
maximum-product-of-two-elements-in-an-array | SIMPLE MAX-HEAP C++ SOLUTION | simple-max-heap-c-solution-by-jeffrin200-7lb2 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Jeffrin2005 | NORMAL | 2024-08-01T16:57:27.692441+00:00 | 2024-08-01T16:57:27.692477+00:00 | 18 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:o(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... | 3 | 0 | ['C++'] | 0 |
maximum-product-of-two-elements-in-an-array | Beats 100% in Runtime | beats-100-in-runtime-by-nurliaidin-xhzz | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Nurliaidin | NORMAL | 2023-12-20T12:19:22.013721+00:00 | 2023-12-20T12:19:22.013782+00:00 | 190 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++', 'JavaScript'] | 0 |
maximum-product-of-two-elements-in-an-array | Maximum Product Of Two Elements In Array||Beats 100%💯|| Full commented code with explanation✅✅ | maximum-product-of-two-elements-in-array-wld5 | Approach\n Describe your approach to solving the problem. \n- largest and secondLargest are initialized to the minimum integer value. \n- The code iterates thro | Vishu6403 | NORMAL | 2023-12-20T09:55:27.302508+00:00 | 2023-12-20T09:56:33.469479+00:00 | 1,114 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\n- `largest` and `secondLargest` are initialized to the minimum integer value. \n- The code iterates through the input array nums. For each element `nums[i]`, it compares with the current largest and secondLargest.\n- If `nums[i]` is greater than the c... | 3 | 0 | ['Array', 'Sorting', 'Java'] | 0 |
maximum-product-of-two-elements-in-an-array | ✅ EASY C++ SOLUTION ☑️ | easy-c-solution-by-2005115-x988 | PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT\n# CONNECT WITH ME\n### https://www.linkedin.com/in/pratay-nandy-9ba57b229/\n#### https://www.instagram.com/pratay_nand | 2005115 | NORMAL | 2023-12-12T14:57:54.329467+00:00 | 2023-12-12T14:57:54.329501+00:00 | 638 | false | # **PLEASE UPVOTE MY SOLUTION IF YOU LIKE IT**\n# **CONNECT WITH ME**\n### **[https://www.linkedin.com/in/pratay-nandy-9ba57b229/]()**\n#### **[https://www.instagram.com/pratay_nandy/]()**\n\n# Approach\nThe provided C++ code is aiming to find the maximum product of two distinct elements in the given vector `nums`. It ... | 3 | 0 | ['Array', 'C++'] | 1 |
maximum-product-of-two-elements-in-an-array | Beats 100% of users with C++ , Brute Force Approach ( Constant Space ) | beats-100-of-users-with-c-brute-force-ap-si8o | Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n\n\n\n\n\nif you like the approach please upvote it\n\n\n\n\n\n\nif you like the ap | abhirajpratapsingh | NORMAL | 2023-12-12T10:45:21.630186+00:00 | 2023-12-12T10:45:21.630206+00:00 | 7 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n\n\n\n\n\nif you like the approach please upvote it\n\n\n\n\n\n\nif you like the approach please upvote it\n\n# Approach... | 3 | 0 | ['Math', 'C++'] | 0 |
maximum-product-of-two-elements-in-an-array | JAVA Solution Explained in HINDI | java-solution-explained-in-hindi-by-the_-kdax | https://youtu.be/4BXBsrpglNk\n\nFor explanation, watch the above video and do like, share and subscribe the channel. \u2764\uFE0F\n# Code\n\nclass Solution {\n | The_elite | NORMAL | 2023-12-12T04:34:22.801502+00:00 | 2023-12-12T04:34:22.801527+00:00 | 455 | false | https://youtu.be/4BXBsrpglNk\n\nFor explanation, watch the above video and do like, share and subscribe the channel. \u2764\uFE0F\n# Code\n```\nclass Solution {\n public int maxProduct(int[] nums) {\n \n Arrays.sort(nums);\n int x = nums[nums.length - 1];\n int y = nums[nums.length - 2];\... | 3 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | Try with Heap | Simple Solution | try-with-heap-simple-solution-by-saim75-3wdy | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | saim75 | NORMAL | 2023-12-12T03:10:31.128062+00:00 | 2023-12-12T03:10:31.128085+00:00 | 42 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['Python3'] | 0 |
maximum-product-of-two-elements-in-an-array | C# Solution for Maximum Product of Two Elements In An Array Problem | c-solution-for-maximum-product-of-two-el-g143 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind the provided solution: find the two largest elements in the array | Aman_Raj_Sinha | NORMAL | 2023-12-12T03:00:09.079690+00:00 | 2023-12-12T03:00:09.079719+00:00 | 453 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind the provided solution: find the two largest elements in the array and then calculate their product after subtracting 1 from each, giving the maximum possible value of (nums[i]-1)*(nums[j]-1).\n\n# Approach\n<!-- Descr... | 3 | 0 | ['C#'] | 0 |
maximum-product-of-two-elements-in-an-array | Rust/Python 2 lines sort. Also can do linear | rustpython-2-lines-sort-also-can-do-line-2ots | Intuition\nOf course you can just check each pair and it will pass, but you do not need to. If the values are all positive, the maximum will be attained with ma | salvadordali | NORMAL | 2023-12-12T02:33:03.839603+00:00 | 2023-12-12T02:33:03.839624+00:00 | 142 | false | # Intuition\nOf course you can just check each pair and it will pass, but you do not need to. If the values are all positive, the maximum will be attained with maximum values of two elements in the product.\n\nSo all you need is to get two maximum values in this list. You can do this in linear time (find one maximum an... | 3 | 0 | ['Python', 'Rust'] | 0 |
maximum-product-of-two-elements-in-an-array | C++ || No Sorting || O(N) || O(1) | c-no-sorting-on-o1-by-n_i_v_a_s-t11z | Code\n\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n // Intuition\n // The idea in here is pretty simple. We will find the | __SAI__NIVAS__ | NORMAL | 2023-12-12T01:41:51.997462+00:00 | 2023-12-12T01:41:51.997488+00:00 | 486 | false | # Code\n```\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n // Intuition\n // The idea in here is pretty simple. We will find the maximum two elemens of the nums vector and then return the required answer.\n int firstMax = -1;\n int secondMax = -1;\n for(auto &e... | 3 | 0 | ['C++'] | 3 |
maximum-product-of-two-elements-in-an-array | Java || 100% || 0(N) | java-100-0n-by-saurabh_kumar1-a2bo | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | saurabh_kumar1 | NORMAL | 2023-08-06T17:23:47.421780+00:00 | 2023-08-06T17:23:47.421815+00:00 | 3 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:0(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:0(1)\n<!-- Add your space complexity here, e.g. $$O... | 3 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | Simple Solution || Beats 100% || 0(n) time ||0(1) space | simple-solution-beats-100-0n-time-01-spa-gb9q | you can also use sorting method but that takes o(nlogn) time\n# Below code take o(n) time\n# Please upvote if you feel it easy\n# Code\n\nclass Solution {\n | ProfessionalMonk | NORMAL | 2023-07-09T05:23:42.207588+00:00 | 2023-07-09T05:23:42.207608+00:00 | 177 | false | # you can also use sorting method but that takes o(nlogn) time\n# Below code take o(n) time\n# Please upvote if you feel it easy\n# Code\n```\nclass Solution {\n public int maxProduct(int[] nums) {\n int first=0;\n int second=0;\n for(int i=0;i<nums.length;i++) {\n if(nums[i]>first) {... | 3 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | Brute Better Good Optimal <---> Interview Prep. | brute-better-good-optimal-interview-prep-6y7r | 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: | lokesh1911e | NORMAL | 2023-04-09T16:40:13.786533+00:00 | 2023-04-09T16:45:32.160448+00:00 | 68 | false | # 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>& num... | 3 | 0 | ['Two Pointers', 'Sorting', 'Heap (Priority Queue)', 'C++'] | 0 |
maximum-product-of-two-elements-in-an-array | Java | Beats 100% | O(n) | java-beats-100-on-by-vasujhawar2001-cv6t | Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(1)\n\n# Code\n\nclass Solution {\n public int maxProduct(int[] nums) {\n int max1 = 0;\n | vasujhawar2001 | NORMAL | 2023-04-03T08:34:43.204918+00:00 | 2023-04-03T08:34:43.204949+00:00 | 429 | false | # Complexity\n- Time complexity: O(N)\n\n- Space complexity: O(1)\n\n# Code\n```\nclass Solution {\n public int maxProduct(int[] nums) {\n int max1 = 0;\n int max2 = 0;\n int result = 0;\n for(int i=0; i<nums.length;i++){\n if(nums[i]>max1){\n max2 = max1;\n ... | 3 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | Simple Java Solution || T.C = O(n), S.C. = O(1) | simple-java-solution-tc-on-sc-o1-by-jaya-egac | 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# Co | Jayant334 | NORMAL | 2023-03-27T09:30:56.696103+00:00 | 2023-03-27T09:30:56.696143+00:00 | 912 | false | # 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 {\n public int maxProduct(int[] nums) {\n int max1=0 , max2=0;\n for(int i=0;i<nums.len... | 3 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | only in one loop java | only-in-one-loop-java-by-bhupendra082002-92dw | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | bhupendra082002 | NORMAL | 2023-03-19T15:44:12.801910+00:00 | 2023-03-19T15:44:12.801942+00:00 | 724 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['Java'] | 2 |
maximum-product-of-two-elements-in-an-array | C++ Solution | c-solution-by-asad_sarwar-3ht4 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | Asad_Sarwar | NORMAL | 2023-02-06T19:48:51.048642+00:00 | 2023-02-26T04:36:38.569522+00:00 | 1,087 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 3 | 0 | ['C++'] | 0 |
maximum-product-of-two-elements-in-an-array | C++ 0ms Solution using priority queue | c-0ms-solution-using-priority-queue-by-p-5ku7 | \n# Code\n\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<pair<int,int>> pq;\n for(int i=0;i<nums.size();i++ | Paras027 | NORMAL | 2023-01-06T21:23:50.640659+00:00 | 2023-01-06T21:23:50.640691+00:00 | 651 | false | \n# Code\n```\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<pair<int,int>> pq;\n for(int i=0;i<nums.size();i++)\n {\n pq.push({nums[i],i});\n }\n int a = pq.top().first;\n pq.pop();\n int b =pq.top().first;\n int a... | 3 | 0 | ['Array', 'Sorting', 'Heap (Priority Queue)', 'C++'] | 1 |
maximum-product-of-two-elements-in-an-array | C++ | 2 liner | 0ms | c-2-liner-0ms-by-lraml-bpa5 | Sort nums\n2. multiply the last two nums \n (since they\'re the max and second max)\n\nc++\n int maxProduct(vector<int>& nums) {\n sort(nums.begin(),n | lRaml | NORMAL | 2022-12-28T16:57:26.832570+00:00 | 2022-12-28T16:59:45.269260+00:00 | 487 | false | 1. Sort nums\n2. multiply the last two nums \n (since they\'re the max and second max)\n\n```c++\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## Another Method\n```c++\nint maxProduct(vector<int>& nums) {\n ... | 3 | 0 | ['C++'] | 0 |
maximum-product-of-two-elements-in-an-array | c# Beats 89.56% | c-beats-8956-by-abettertomorrow-jcty | \n\npublic class Solution {\n public int MaxProduct(int[] nums) {\n int first = 0;\n int second = 0;\n List<int> list = nums.ToL | abettertomorrow | NORMAL | 2022-12-19T11:27:25.953586+00:00 | 2022-12-19T11:27:25.953622+00:00 | 380 | false | \n```\npublic class Solution {\n public int MaxProduct(int[] nums) {\n int first = 0;\n int second = 0;\n List<int> list = nums.ToList<int>();\n\n first = list.Max();\n list.Remove(first);\n second = list.Max();\n\n\n return (first-1)*(second-1... | 3 | 0 | ['C#'] | 0 |
maximum-product-of-two-elements-in-an-array | Java | java-by-niyazjava-rroo | Upvote if u like it\n\npublic int maxProduct(int[] nums) {\n int firstMax = 0;\n int secondMax = 0;\n\n for (int num : nums) {\n if (num > first | NiyazJava | NORMAL | 2022-11-22T08:08:59.130730+00:00 | 2022-11-22T08:08:59.130769+00:00 | 243 | false | Upvote if u like it\n```\npublic int maxProduct(int[] nums) {\n int firstMax = 0;\n int secondMax = 0;\n\n for (int num : nums) {\n if (num > firstMax) {\n secondMax = firstMax;\n firstMax = num;\n } else if (secondMax < num) {\n secondMax = num;\n }\n }... | 3 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | c++ one liner simple approach | c-one-liner-simple-approach-by-sailakshm-4w5j | PLEASE UPVOTE IF U FIND HELPFUL\n\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n sort(nums.rbegin(),nums.rend()); return (nums[0]- | sailakshmi1 | NORMAL | 2022-06-01T10:21:24.601274+00:00 | 2022-06-01T10:21:24.601300+00:00 | 158 | false | **PLEASE UPVOTE IF U FIND HELPFUL**\n```\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n sort(nums.rbegin(),nums.rend()); return (nums[0]-1)*(nums[1]-1);\n }\n};\n``` | 3 | 0 | ['C', 'Sorting'] | 1 |
maximum-product-of-two-elements-in-an-array | [swift] || Using Heap Sort || Easy explanation | swift-using-heap-sort-easy-explanation-b-qwea | The solution follows the idea of using a maxHeap, wherein the root object is the object with the highest value. \n\nA maxHeap also has a property that every chi | sadyojat | NORMAL | 2021-08-04T05:01:33.769750+00:00 | 2021-08-04T05:01:33.769810+00:00 | 271 | false | The solution follows the idea of using a maxHeap, wherein the root object is the object with the highest value. \n\nA maxHeap also has a property that every child of a node will have a lower weight than its parent, so while it does not deliver ordered sorting, the elements are in a semi sorted order. \n\nThe simplest f... | 3 | 0 | ['Swift', 'Heap (Priority Queue)'] | 2 |
maximum-product-of-two-elements-in-an-array | Rust O(N) solution | rust-on-solution-by-bigmih-twly | Sorting is unnecessary in such a task. \nWe only need to keep track of the 2 maximum values while traversing the vector.\n\nimpl Solution {\n pub fn max_prod | BigMih | NORMAL | 2021-06-05T21:08:50.005163+00:00 | 2021-06-05T21:28:12.872292+00:00 | 90 | false | Sorting is unnecessary in such a task. \nWe only need to keep track of the 2 maximum values while traversing the vector.\n```\nimpl Solution {\n pub fn max_product(nums: Vec<i32>) -> i32 {\n let (mut max1, mut max2) = (0, 0);\n nums.iter().for_each(|&n| {\n if n > max2 {\n max... | 3 | 0 | ['Rust'] | 0 |
maximum-product-of-two-elements-in-an-array | C++ 3 Solutions: Max_Heap, Linear Scan, Sort | c-3-solutions-max_heap-linear-scan-sort-9nmrt | FIRST\n\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<int, vector<int>, greater<int>> que;\n \n for( | paulariri | NORMAL | 2020-06-22T06:58:20.584584+00:00 | 2020-06-22T06:58:20.584615+00:00 | 225 | false | FIRST\n```\nclass Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n priority_queue<int, vector<int>, greater<int>> que;\n \n for(int i = 0; i < (int)nums.size(); ++i){\n if(que.size() < 2) { que.push(nums[i]); continue; }\n \n if(que.top() < nums[i]){\n... | 3 | 2 | ['C', 'Sorting', 'Heap (Priority Queue)', 'C++'] | 0 |
maximum-product-of-two-elements-in-an-array | [Rust] O(N) 1 pass 0ms simple solution | rust-on-1-pass-0ms-simple-solution-by-ru-icer | A naive way will be: \n1. sort the vector \n2. multiply (max - 1) * (second_max - 1)\n\nrust\nimpl Solution {\n pub fn max_product(ref mut nums: Vec<i32>) -> | rudy__ | NORMAL | 2020-06-01T01:56:54.289688+00:00 | 2020-06-01T02:19:21.485973+00:00 | 69 | false | A naive way will be: \n1. sort the vector \n2. multiply `(max - 1) * (second_max - 1)`\n\n```rust\nimpl Solution {\n pub fn max_product(ref mut nums: Vec<i32>) -> i32 {\n nums.sort();\n (nums.last().unwrap() - 1) * (nums[nums.len() - 2] - 1)\n }\n}\n```\n\nBut I guess the interviewer will not like i... | 3 | 0 | [] | 1 |
maximum-product-of-two-elements-in-an-array | Javascript sorting solution | javascript-sorting-solution-by-seriously-en0a | \nvar maxProduct = function(nums) {\n nums.sort(function(a,b){return b-a});\n return (nums[0]-1)*(nums[1]-1)\n};\n | seriously_ridhi | NORMAL | 2020-05-31T15:18:54.412522+00:00 | 2020-05-31T15:18:54.412569+00:00 | 214 | false | ```\nvar maxProduct = function(nums) {\n nums.sort(function(a,b){return b-a});\n return (nums[0]-1)*(nums[1]-1)\n};\n``` | 3 | 1 | [] | 1 |
maximum-product-of-two-elements-in-an-array | C++ simple solution | c-simple-solution-by-oleksam-f2s7 | \nint maxProduct(vector<int>& nums) {\n\tpriority_queue<int> pq(nums.begin(), nums.end());\n\tint first = pq.top();\n\tpq.pop();\n\tint second = pq.top();\n\tre | oleksam | NORMAL | 2020-05-31T10:25:12.547489+00:00 | 2020-05-31T10:25:12.547536+00:00 | 446 | false | ```\nint maxProduct(vector<int>& nums) {\n\tpriority_queue<int> pq(nums.begin(), nums.end());\n\tint first = pq.top();\n\tpq.pop();\n\tint second = pq.top();\n\treturn (first - 1) * (second - 1);\n}\n```\n | 3 | 0 | ['C', 'C++'] | 1 |
maximum-product-of-two-elements-in-an-array | Python 3 beats 100% | python-3-beats-100-by-alankrit_03-kpje | \nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n max_val = nums[0]\n second_max = 0\n \n for i in range(1,len( | alankrit_03 | NORMAL | 2020-05-31T08:25:04.443290+00:00 | 2020-05-31T08:25:04.443325+00:00 | 378 | false | ```\nclass Solution:\n def maxProduct(self, nums: List[int]) -> int:\n max_val = nums[0]\n second_max = 0\n \n for i in range(1,len(nums)):\n if nums[i]>=max_val:\n second_max = max_val\n max_val = nums[i]\n elif nums[i]>second_max:\n ... | 3 | 1 | ['Python3'] | 1 |
maximum-product-of-two-elements-in-an-array | simple cpp solution | simple-cpp-solution-by-_mrbing-du4q | Runtime: 24 ms, faster than 50.00% of C++ online submissions for Maximum Product of Two Elements in an Array.\nMemory Usage: 10 MB, less than 100.00% of C++ onl | _mrbing | NORMAL | 2020-05-31T04:13:11.798619+00:00 | 2020-05-31T04:13:11.798665+00:00 | 231 | false | Runtime: 24 ms, faster than 50.00% of C++ online submissions for Maximum Product of Two Elements in an Array.\nMemory Usage: 10 MB, less than 100.00% of C++ online submissions for Maximum Product of Two Elements in an Array.\n```\n class Solution {\npublic:\n int maxProduct(vector<int>& nums) {\n sort(nums... | 3 | 2 | ['C++'] | 0 |
maximum-product-of-two-elements-in-an-array | [Java] One Iteration Solution | java-one-iteration-solution-by-adityarew-lf4e | \n int m1 = 1; // second largest num\n int m2 = 1; // largest num\n for (int i=0; i<nums.length; i++){\n if (nums[i] >= m2) {\n m1 = m2;\n | adityarewari | NORMAL | 2020-05-31T04:01:46.219381+00:00 | 2020-05-31T04:03:19.267361+00:00 | 271 | false | ```\n int m1 = 1; // second largest num\n int m2 = 1; // largest num\n for (int i=0; i<nums.length; i++){\n if (nums[i] >= m2) {\n m1 = m2;\n m2 = nums[i];\n continue;\n } else if(nums[i] > m1) {\n m1 = nums[i];\n continue;\n }\n }\n return (m2-1)*(m1-1);... | 3 | 1 | [] | 0 |
maximum-product-of-two-elements-in-an-array | BEST SOLUTION FOR BEGINERS IN JAVA | best-solution-for-beginers-in-java-by-an-5rbo | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | antonypraveen004 | NORMAL | 2025-03-07T16:12:30.197793+00:00 | 2025-03-07T16:12:30.197793+00:00 | 143 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 2 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | C++ Optimized Solution | 4 line Code | beginner level | BEATS 100% | c-optimized-solution-4-line-code-beginne-zsup | IntuitionThe problem requires finding the two largest numbers in the array and computing their modified product (max1−1)×(max2−1).Instead of sorting, we can eff | Harisharen | NORMAL | 2025-01-31T16:31:41.880899+00:00 | 2025-01-31T16:31:41.880899+00:00 | 114 | false | # Intuition
The problem requires finding the two largest numbers in the array and computing their modified product ***(max1−1)×(max2−1)***.
Instead of sorting, we can efficiently track the two largest numbers while iterating through the array.
# Approach
1. Initialize max1 and max2 to the smallest possible integer va... | 2 | 0 | ['Array', 'Sorting', 'C++'] | 0 |
maximum-product-of-two-elements-in-an-array | Three different approaches || Beats 100% | three-different-approaches-beats-100-by-g4mw9 | Intuition\nThe problem requires finding the maximum product of two distinct elements in an array after reducing each by 1. The first thought is to find the two | mayanknayal5306069 | NORMAL | 2024-11-09T21:42:36.781443+00:00 | 2024-11-09T21:42:36.781482+00:00 | 46 | false | # Intuition\nThe problem requires finding the maximum product of two distinct elements in an array after reducing each by 1. The first thought is to find the two largest elements in the array, as they will maximize the product.\n\n# Approach\nFirst Approach (Bubble Sort):\n\nSort the array in non-decreasing order using... | 2 | 0 | ['Java'] | 0 |
maximum-product-of-two-elements-in-an-array | A simple two-line solution with a step-by-step process. | a-simple-two-line-solution-with-a-step-b-4z8s | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Sort the array from larger to small\n2. Took 0,1 index values from tha | shamnad_skr | NORMAL | 2024-10-11T12:05:22.783674+00:00 | 2024-10-11T12:05:22.783703+00:00 | 77 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Sort the array from larger to small\n2. Took 0,1 index values from that array ( since after sort they are the largest or same)\n3. calculate the anser and return ind\n\n# Complexity\n- Time complexity:O(n)\n<!-- Add your ... | 2 | 0 | ['TypeScript', 'JavaScript'] | 0 |
maximum-product-of-two-elements-in-an-array | Q.1463 | ☑️One line Python3 solution | EZ💯 | q1463-one-line-python3-solution-ez-by-ch-8dbq | \uD83D\uDCA1Intuition\nGrab the two biggest numbers using inbuilt functions, knock them down by one, and bring out the product.\n\n# \uD83E\uDDE0 Approach\n Des | charan1kh | NORMAL | 2024-09-18T18:12:54.473766+00:00 | 2024-09-18T18:12:54.473797+00:00 | 20 | false | # \uD83D\uDCA1Intuition\nGrab the two biggest numbers using inbuilt functions, knock them down by one, and bring out the product.\n\n# \uD83E\uDDE0 Approach\n<!-- Describe your approach to solving the problem. -->\nIn this solution, the input list nums is first sorted in descending order, and then the top two largest n... | 2 | 0 | ['Python3'] | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.