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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
reverse-degree-of-a-string | Scala solution with implicit, zip and map | scala-solution-with-implicit-zip-and-map-y8c6 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | iyIeO99AmH | NORMAL | 2025-04-03T21:10:20.377309+00:00 | 2025-04-03T21:10:20.377309+00:00 | 1 | 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
```scala []
object Solution {
def reverseDegree(s: String): Int = {
(1 to s.length).zip(s.toCharArray).map {
case (idx, value) => idx * value.asNum
}.sum
}
implicit class CharNum(c: Char) {
def asNum: Int = 26 - (c - 'a')
}
}
``` | 0 | 0 | ['Scala'] | 0 |
reverse-degree-of-a-string | Intuitive solution with array to map character to value in reversed alphabet | intuitive-solution-with-array-to-map-cha-sekc | IntuitionWe'll use a simple array to map each lowercase character to its value in the reversed alphabet.ApproachWe'll set up a simple array to map each lowercas | mnjk | NORMAL | 2025-04-03T20:05:21.275617+00:00 | 2025-04-03T20:05:21.275617+00:00 | 3 | false | # Intuition
We'll use a simple array to map each lowercase character to its value in the reversed alphabet.
# Approach
We'll set up a simple array to map each lowercase character to its value in the reversed alphabet:
```ts
const m = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
```
We then loop over over the string and map each character while multiplying it with the 1-indexed position in `s`:
```ts
let result = 0;
for (let i = 0; i < s.length; i++) {
result += (i + 1) * m[s[i].charCodeAt(0) - 97];
}
```
# Complexity
- Time complexity: $$O(n)$$
- Space complexity: $$O(n)$$
# Code
```typescript []
function reverseDegree(s: string): number {
const m = [26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
let result = 0;
for (let i = 0; i < s.length; i++) {
result += (i + 1) * m[s[i].charCodeAt(0) - 97];
}
return result;
};
``` | 0 | 0 | ['Array', 'String', 'TypeScript'] | 0 |
reverse-degree-of-a-string | python | python-by-binkswang-u2xp | Code | BinksWang | NORMAL | 2025-04-03T19:12:43.709857+00:00 | 2025-04-03T19:12:43.709857+00:00 | 2 | false | # Code
```python3 []
class Solution:
def reverseDegree(self, s: str) -> int:
arr = []
val = 26
rs = 0
while val > 0:
arr.append(val)
val -= 1
for i in range (0, len(s)):
rs += arr[ord(s[i]) - ord('a')] * (i + 1)
return rs
``` | 0 | 0 | ['Python3'] | 0 |
reverse-degree-of-a-string | c# | c-by-binkswang-89ua | Code | BinksWang | NORMAL | 2025-04-03T19:08:36.004979+00:00 | 2025-04-03T19:08:36.004979+00:00 | 5 | false | # Code
```csharp []
public class Solution {
public int ReverseDegree(string s) {
var arr = new int[26];
var val = 26;
var rs = 0;
for(var i = 0; i < arr.Length; i++){
arr[i] = val;
val--;
}
for(var i = 0; i < s.Length; i++){
rs += arr[s[i] - 'a'] * (i + 1);
}
return rs;
}
}
``` | 0 | 0 | ['C#'] | 0 |
reverse-degree-of-a-string | reverse-degree-of-a-string || very easy solution || JS | reverse-degree-of-a-string-very-easy-sol-rxn4 | Complexity
Time complexity:O(N)
Space complexity:O(1)
Code | shevykumar26 | NORMAL | 2025-04-03T17:56:38.958169+00:00 | 2025-04-03T17:56:38.958169+00:00 | 8 | false | # Complexity
- Time complexity:O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```javascript []
/**
* @param {string} s
* @return {number}
*/
var reverseDegree = function(s) {
let sum=0
for(let i=0;i<s.length;i++){
let itm=97-s.charCodeAt(i)+26
sum+=itm*(i+1)
}
return sum
};
``` | 0 | 0 | ['JavaScript'] | 0 |
reverse-degree-of-a-string | Java | java-by-soumya_699-x1hm | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Soumya_699 | NORMAL | 2025-04-03T04:29:35.207615+00:00 | 2025-04-03T04:29:35.207615+00:00 | 13 | 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
```java []
class Solution {
public int reverseDegree(String s) {
Map<Character,Integer> map = new HashMap<>();
char ch ='a';
for(int i =26;i>=1;i--){
map.put(ch,i);
ch++;
}
// System.out.println(map);
int sum =0;
int i =0;
for(char ch1 :s.toCharArray()){
i++;
sum+=map.get(ch1)*i;
}
System.out.println(sum);
return sum;
}
}
``` | 0 | 0 | ['Java'] | 0 |
reverse-degree-of-a-string | Java | java-by-soumya_699-lr6d | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Soumya_699 | NORMAL | 2025-04-03T04:29:31.499443+00:00 | 2025-04-03T04:29:31.499443+00:00 | 3 | 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
```java []
class Solution {
public int reverseDegree(String s) {
Map<Character,Integer> map = new HashMap<>();
char ch ='a';
for(int i =26;i>=1;i--){
map.put(ch,i);
ch++;
}
// System.out.println(map);
int sum =0;
int i =0;
for(char ch1 :s.toCharArray()){
i++;
sum+=map.get(ch1)*i;
}
System.out.println(sum);
return sum;
}
}
``` | 0 | 0 | ['Java'] | 0 |
reverse-degree-of-a-string | Reverse Degree of a String - 💯Beats 100.00% || C++✅✔️ | reverse-degree-of-a-string-beats-10000-c-izsb | Complexity
Time complexity:
Space complexity:
Code | itz_raju | NORMAL | 2025-04-02T20:06:15.581196+00:00 | 2025-04-02T20:06:15.581196+00:00 | 1 | false | 
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)
# Code
```cpp []
class Solution {
public:
int reverseDegree(string s) {
int sum(0);
for(int i=0; i<s.length(); i++) sum += (26 - (s[i]-'a')) * (i+1);
return sum;
}
};
``` | 0 | 0 | ['String', 'C++'] | 0 |
reverse-degree-of-a-string | Unique Approach | hashing | unique-approach-hashing-by-mr_ramzan-n77a | Code | mr_ramzan | NORMAL | 2025-04-02T17:17:14.427525+00:00 | 2025-04-02T17:17:14.427525+00:00 | 1 | false |
# Code
```cpp []
class Solution {
public:
int reverseDegree(string s) {
unordered_map<char,int>map;
for(char ch='a';ch<='z';ch++) map[ch]= 'z'-ch+1;
int sum=0;
for(int i=0;i<s.length();i++){
sum+=(map[s[i]]*(i+1));
}
return sum;
}
};
``` | 0 | 0 | ['Hash Table', 'String', 'C++'] | 0 |
reverse-degree-of-a-string | C++ Solution for Reverse Degree of a String | c-solution-for-reverse-degree-of-a-strin-mxqa | IntuitionThe problem likely involves transforming a string by calculating a "reverse degree." The goal is to map each character to its reverse position in the E | _tarunkr_chauhan | NORMAL | 2025-04-02T10:36:01.629945+00:00 | 2025-04-02T10:36:01.629945+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem likely involves transforming a string by calculating a "reverse degree." The goal is to map each character to its reverse position in the English alphabet ('a' becomes 26, 'b' becomes 25, etc.) and weight each character's reverse value by its position in the string. This creates an interesting metric for processing the input string.
# Approach
<!-- Describe your approach to solving the problem. -->
1. Traverse the given string character by character.
2. For each character `s[i]`:
- Calculate the reverse value as `26 - (s[i] - 'a')`.
- Multiply the reverse value by its 1-based position `i + 1` in the string.
3. Accumulate these weighted reverse values into a result variable (`ans`).
4. Return the accumulated result.
# Complexity
- **Time Complexity:** $$O(n)$$, where $$n$$ is the length of the input string. This is because we traverse the string once and perform constant-time calculations for each character.
- **Space Complexity:** $$O(1)$$, as we use only a fixed amount of extra space regardless of the input size.
# Code
```cpp []
class Solution {
public:
int reverseDegree(string s) {
int ans=0;
for(int i=0;i<s.size();i++){
int rev=26-(s[i]-'a');
ans+=rev*(i+1);
}
return ans;
}
};
``` | 0 | 0 | ['C++'] | 0 |
reverse-degree-of-a-string | esay java code of beginers!!!!!!!!!!!!!!!! | esay-java-code-of-beginers-by-churchill0-js0k | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | CHURCHILL04 | NORMAL | 2025-04-02T10:00:48.302676+00:00 | 2025-04-02T10:00:48.302676+00:00 | 4 | 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
```java []
class Solution {
public int reverseDegree(String s) {
int su=0,p=1;
for(int i=0;i<s.length();i++)
{
p=26-(s.charAt(i)-'a');
su+=p*(i+1);
}
return su;
}
}
``` | 0 | 0 | ['Java'] | 0 |
reverse-degree-of-a-string | Python fast one-line solution | python-fast-one-line-solution-by-redberr-l9oz | Code | redberry33 | NORMAL | 2025-04-02T07:08:52.987723+00:00 | 2025-04-02T07:08:52.987723+00:00 | 6 | false |
# Code
```python3 []
class Solution:
def reverseDegree(self, s: str) -> int:
return reduce(lambda acc, enum: acc + (enum[0] + 1) * (123 - ord(enum[1])), enumerate(s), 0)
``` | 0 | 0 | ['Python3'] | 0 |
reverse-degree-of-a-string | 3498 - Reverse Degree Of A String | 3498-reverse-degree-of-a-string-by-prati-chxn | Intuitionresult += (26 - (s.charAt(i) - 'a')) * (i + 1);Approachresult += (s.charAt(i) - 123) * -(i + 1);Complexity
Time complexity:
Space complexity:
Code | pratik_solanki | NORMAL | 2025-04-02T06:11:13.172980+00:00 | 2025-04-02T06:11:13.172980+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
result += (26 - (s.charAt(i) - 'a')) * (i + 1);
# Approach
<!-- Describe your approach to solving the problem. -->
result += (s.charAt(i) - 123) * -(i + 1);
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int reverseDegree(String s) {
int result = 0;
int len = s.length();
for (int i = 0; i < len; i++) {
result += (s.charAt(i) - 123) * -(i + 1);
}
return result;
}
}
``` | 0 | 0 | ['Java'] | 0 |
reverse-degree-of-a-string | Java Easy Solution | java-easy-solution-by-iamsd-szpc | Code | iamsd_ | NORMAL | 2025-04-02T05:31:25.396453+00:00 | 2025-04-02T05:31:25.396453+00:00 | 3 | false | # Code
```java []
class Solution {
public int reverseDegree(String s) {
int i = 26;
Map<String, Integer> map = new HashMap<>();
for (char c = 'a'; c <= 'z'; c++) {
map.put(String.valueOf(c), i);
i--;
}
int sum = 0;
String[] srr = s.split("");
int len = srr.length;
for (int ii = 0; ii < len; ii++) {
sum += map.get(srr[ii]) * (ii + 1);
}
return sum;
}
}
``` | 0 | 0 | ['Java'] | 0 |
reverse-degree-of-a-string | 🔄📖 Reverse Alphabet Math Madness! 🔢🤯Simple Logic Behind the Code 🤓➡️💡PYTHON | reverse-alphabet-math-madness-simple-log-hp51 | IntuitionThe problem requires calculating a weighted sum where each letter's reverse alphabet value is multiplied by its 1-based index in the string.
Since lett | Shahin1212 | NORMAL | 2025-04-02T04:41:08.554637+00:00 | 2025-04-02T04:41:08.554637+00:00 | 3 | false | # Intuition
The problem requires calculating a weighted sum where each letter's **reverse alphabet value** is multiplied by its **1-based index** in the string.
Since letter values are fixed, we can **precompute** them in a dictionary and efficiently compute the sum in a **single pass**.
# Approach
1. **Create a dictionary** that maps each lowercase letter to its **reverse alphabet value**:
```python
Letter = {chr(97 + i): 26 - i for i in range(26)}
# Code
```python3 []
class Solution:
def reverseDegree(self, s: str) -> int:
Letter = {chr(97 + i): 26 - i for i in range(26)}
res = 0 # Store the sum of values
for i, char in enumerate(s, start=1): # 1-based index
if char in Letter:
res += Letter[char] * i # Multiply by index
return res
```

| 0 | 0 | ['Python3'] | 0 |
reverse-degree-of-a-string | Java 100% Faster Solution | java-100-faster-solution-by-tbekpro-p8vb | Complexity
Time complexity: O(N)
Space complexity: O(1)
Code | tbekpro | NORMAL | 2025-04-01T19:11:57.318045+00:00 | 2025-04-01T19:11:57.318045+00:00 | 4 | false | # Complexity
- Time complexity: O(N)
- Space complexity: O(1)
# Code
```java []
class Solution {
public int reverseDegree(String s) {
int result = 0;
for (int i = 0; i < s.length(); i++) {
int score = (26 - (s.charAt(i) - 'a')) * (i + 1);
result += score;
}
return result;
}
}
``` | 0 | 0 | ['Java'] | 0 |
reverse-degree-of-a-string | reverseIndexString | reverseindexstring-by-aditya_211-gpo4 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Aditya_211 | NORMAL | 2025-04-01T17:35:43.710724+00:00 | 2025-04-01T17:35:43.710724+00:00 | 1 | 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
```java []
class Solution {
public int reverseDegree(String s) {
int sum = 0;
String S = s.toUpperCase();
int i=1;
for(char x : S.toCharArray()){
int idxRev = 26 - (x-'A');
sum = sum + idxRev*i;
i++;
}
return sum;
}
}
``` | 0 | 0 | ['Java'] | 0 |
maximum-product-of-three-numbers | Java O(1) space O(n) time solution beat 100% | java-o1-space-on-time-solution-beat-100-wung3 | Simply find out the three largest numbers and the two smallest numbers using one pass.\n\n public int maximumProduct(int[] nums) {\n int max1 = Intege | dreamchase | NORMAL | 2017-06-25T17:10:41.283000+00:00 | 2018-10-16T15:01:25.567757+00:00 | 53,928 | false | Simply find out the three largest numbers and the two smallest numbers using one pass.\n```\n public int maximumProduct(int[] nums) {\n int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE, min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;\n for (int n : nums) {\n if (n > max1) {\n max3 = max2;\n max2 = max1;\n max1 = n;\n } else if (n > max2) {\n max3 = max2;\n max2 = n;\n } else if (n > max3) {\n max3 = n;\n }\n\n if (n < min1) {\n min2 = min1;\n min1 = n;\n } else if (n < min2) {\n min2 = n;\n }\n }\n return Math.max(max1*max2*max3, max1*min1*min2);\n }\n``` | 315 | 2 | [] | 47 |
maximum-product-of-three-numbers | [Python] O(N) and 1 line | python-on-and-1-line-by-lee215-g80f | My first solution using sort\n\ndef maximumProduct(self, nums):\n nums.sort()\n return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * num | lee215 | NORMAL | 2017-06-28T19:15:59.247000+00:00 | 2018-09-02T20:50:07.670914+00:00 | 29,058 | false | My first solution using ```sort```\n````\ndef maximumProduct(self, nums):\n nums.sort()\n return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1])\n`````\nI found a exactly same solution in discuss. Anyway, O(NlogN) is not adorable and O(N) is possible.\n````\ndef maximumProduct(self, nums):\n a, b = heapq.nlargest(3, nums), heapq.nsmallest(2, nums)\n return max(a[0] * a[1] * a[2], b[0] * b[1] * a[0])\n`````\nMake it 1 line if you like:\n````\n def maximumProduct(self, nums):\n return max(nums) * max(a * b for a, b in [heapq.nsmallest(2, nums), heapq.nlargest(3, nums)[1:]])\n````\n\n**Update:**\n**Q:** Time complexity?\n**A:** Let me make it clear.\nMy solution is `O(NlogK)`, where K = 3.\nSo I said it\'s `O(N)`\nAnd the best solution can be `O(N)`.\n\n\n@macheret help explained the following:\n\nFor heapq.nlargest / heapq.nsmallest, looking at the (source code)[https://github.com/python/cpython/blob/e22072fb11246f125aa9ff7629c832b9e2407ef0/Lib/heapq.py#L545-L559]\n\nFirst, k items are heapified - that\'s an O(k*log(k)) operation.\nThen, n-k items are added into the heap with heapreplace - that\'s n-k O(log(k)) operations, or O((n-k)*log(k)).\nAdd those up, you get O(n*log(k)).\nIn this case k is constant and doesn\'t scale with n, so this usage is O(n). | 148 | 8 | [] | 21 |
maximum-product-of-three-numbers | General solution for any K | general-solution-for-any-k-by-kale_kale_-gyt1 | This is the first solution that can be used to solve the problem, Maximum Prodoct of K Numbers.\nHope it can help others when encountering a follow up question. | kale_kale_kale | NORMAL | 2018-05-05T02:49:19.018294+00:00 | 2018-09-25T06:07:12.515578+00:00 | 6,183 | false | This is the first solution that can be used to solve the problem, Maximum Prodoct of K Numbers.\nHope it can help others when encountering a follow up question.\n```\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n // if signs of the minimum and maximum number are the same, simply return this.\n // Assume no overflow problem will occur.\n if(nums[0]*nums[nums.length-1] >= 0) return nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3];\n int l = 0, r = nums.length-1;\n int product = 1;\n // k = 3 in this case. But it should be extended to any number.\n // if count is odd, we should take the rightmost number. Otherwise, we should take the maximum product by comparing\n // the product of two leftmost elements and that of two rightmost elements.\n int count = 3;\n while(count > 0) {\n if(count % 2 == 1) {\n product *= nums[r--];\n }\n else {\n if(nums[r]*nums[r-1] > nums[l]*nums[l+1])\n product *= nums[r--] * nums[r--];\n else\n product *= nums[l++] * nums[l++];\n count--;\n }\n count--;\n }\n return product;\n }\n``` | 74 | 0 | [] | 11 |
maximum-product-of-three-numbers | [C++] Two Solutions | c-two-solutions-by-pankajgupta20-6wyf | Sorting Solution\n\tclass Solution {\n\tpublic:\n\t\tint maximumProduct(vector& nums) {\n\t\t\tint n = nums.size();\n\t\t\tsort(nums.begin(), nums.end());\n\t\t | pankajgupta20 | NORMAL | 2021-05-13T12:42:16.670192+00:00 | 2021-05-13T12:43:19.470746+00:00 | 11,171 | false | ##### Sorting Solution\n\tclass Solution {\n\tpublic:\n\t\tint maximumProduct(vector<int>& nums) {\n\t\t\tint n = nums.size();\n\t\t\tsort(nums.begin(), nums.end());\n\t\t\treturn max(nums[n - 1] * nums[n - 2] * nums[n - 3], nums[0] * nums[1] * nums[n - 1]);\n\t\t}\n\t};\n##### Without Sorting Solution\n\tclass Solution {\n\tpublic:\n\t\tint maximumProduct(vector<int>& nums) {\n\t\t\tint max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN;\n\t\t\tint min1 = INT_MAX, min2 = INT_MAX, min3 = INT_MAX;\n\t\t\tfor(int i = 0; i < nums.size(); i++){\n\t\t\t\tif(nums[i] <= min1){\n\t\t\t\t\tmin2 = min1;\n\t\t\t\t\tmin1 = nums[i];\n\t\t\t\t} \n\t\t\t\telse if(nums[i] <= min2){\n\t\t\t\t\tmin2 = nums[i];\n\t\t\t\t}\n\t\t\t\tif(nums[i] >= max1){ \n\t\t\t\t\tmax3 = max2;\n\t\t\t\t\tmax2 = max1;\n\t\t\t\t\tmax1 = nums[i];\n\t\t\t\t} \n\t\t\t\telse if(nums[i] >= max2){\n\t\t\t\t\tmax3 = max2;\n\t\t\t\t\tmax2 = nums[i];\n\t\t\t\t} \n\t\t\t\telse if(nums[i] >= max3){\n\t\t\t\t\tmax3 = nums[i];\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn max(min1 * min2 * max1, max1 * max2 * max3);\n\t\t}\n\t}; | 60 | 3 | ['C', 'Sorting', 'C++'] | 4 |
maximum-product-of-three-numbers | Java Easy AC... | java-easy-ac-by-leiyu-j91a | \n public int maximumProduct(int[] nums) {\n \n Arrays.sort(nums);\n //One of the Three Numbers is the maximum value in the array.\n\n | leiyu | NORMAL | 2017-06-25T03:00:34.488000+00:00 | 2018-10-16T04:15:09.312973+00:00 | 17,561 | false | ```\n public int maximumProduct(int[] nums) {\n \n Arrays.sort(nums);\n //One of the Three Numbers is the maximum value in the array.\n\n int a = nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3];\n int b = nums[0] * nums[1] * nums[nums.length - 1];\n return a > b ? a : b;\n }\n```\npython3\n```\n def maximumProduct(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n nums.sort()\n a = nums[-1] * nums[-2] * nums[-3]\n b = nums[0] * nums[1] * nums[-1]\n return max(a,b)\n``` | 59 | 3 | [] | 11 |
maximum-product-of-three-numbers | easiest two lines of code#python3 | easiest-two-lines-of-codepython3-by-moha-m2dp | 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 | Mohammad_tanveer | NORMAL | 2023-03-18T05:04:19.034291+00:00 | 2023-03-18T05:04:19.034336+00:00 | 5,698 | 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)$$ -->\n\n# Code\n```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n return max(nums[-1]*nums[-2]*nums[-3],nums[0]*nums[1]*nums[-1])\n #please do upvote as it makes me to code more.\n```\n# consider upvoting\n\n | 50 | 0 | ['Python3'] | 4 |
maximum-product-of-three-numbers | Share my python solution: one pass, O(n) time O(1) space | share-my-python-solution-one-pass-on-tim-sl33 | \n max1,max2,max3,min1,min2 = float('-Inf'),float('-Inf'),float('-Inf'),float('Inf'),float('Inf')\n for num in nums:\n if num >= max1:\ | kiiiid | NORMAL | 2017-07-16T05:06:19.580000+00:00 | 2018-10-25T06:15:52.574047+00:00 | 5,208 | false | ```\n max1,max2,max3,min1,min2 = float('-Inf'),float('-Inf'),float('-Inf'),float('Inf'),float('Inf')\n for num in nums:\n if num >= max1:\n max3,max2,max1 = max2,max1,num\n elif num >= max2:\n max3,max2 = max2,num\n elif num > max3:\n max3 = num\n if num <= min1:\n min2,min1 = min1,num\n elif num < min2:\n min2 = num\n return max(max1*max2*max3,min1*min2*max1)\n```\nNo need to sort but more typing~ | 44 | 1 | [] | 5 |
maximum-product-of-three-numbers | C++ DP solution | c-dp-solution-by-wofainta-i4q2 | O(n) time, O(1) space\n\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int f[2][4], i, j;\n f[0][0] = f[1][0] = 1;\n | wofainta | NORMAL | 2018-09-24T21:59:08.737154+00:00 | 2018-10-14T18:49:25.282190+00:00 | 4,733 | false | O(n) time, O(1) space\n```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int f[2][4], i, j;\n f[0][0] = f[1][0] = 1;\n for (j = 3; j > 0; --j) {\n f[0][j] = INT_MIN;\n f[1][j] = INT_MAX;\n } \n \n for (i = 0; i < nums.size(); ++i) {\n for (j = 3; j > 0; --j) {\n if (f[0][j - 1] == INT_MIN) {\n continue;\n }\n f[0][j] = max(f[0][j], max(f[0][j - 1] * nums[i], f[1][j - 1] * nums[i]));\n f[1][j] = min(f[1][j], min(f[0][j - 1] * nums[i], f[1][j - 1] * nums[i]));\n }\n } \n return f[0][3];\n }\n};\n``` | 39 | 1 | [] | 6 |
maximum-product-of-three-numbers | Time: O(n), Space: O(1) | time-on-space-o1-by-riyakushwaha-iso1 | Base: Maximum product of three numbers is possible only when all three numbers are positive or two numbers are negative and one is positive.\n\nApproach1: \n1. | riyakushwaha | NORMAL | 2021-12-03T10:53:25.994318+00:00 | 2021-12-03T10:53:25.994347+00:00 | 3,360 | false | Base: Maximum product of three numbers is possible only when all three numbers are positive or two numbers are negative and one is positive.\n\nApproach1: \n1. Sort the array, so three max numbers are in the end of the array, two negative numbers whose product will be max will be on the start of the array.\n2. return max of (nums[n-1]*nums[i-2]*nums[i-3], nums[0]*nums[1]*nums[n-1])\n\nApproach2:\nFind max1, max2, max3, min1 and min2 using a for loop\n```\n public int maximumProduct(int[] nums) {\n int min1, min2, max1, max2, max3;\n min1 = min2 = Integer.MAX_VALUE;\n max1 = max2 = max3 = Integer.MIN_VALUE;\n for(int i = 0; i < nums.length; i++){\n //Updating maximums\n if(max1 < nums[i]){\n max3 = max2;\n max2 = max1;\n max1 = nums[i];\n }else if(max2 < nums[i]){\n max3 = max2;\n max2 = nums[i];\n }else if(max3 < nums[i]){\n max3 = nums[i];\n }\n \n //Updating minimums\n if(min1 > nums[i]){\n min2 = min1;\n min1 = nums[i];\n }else if(min2 > nums[i]){\n min2 = nums[i];\n }\n \n }\n \n return Math.max(max1 * max2 * max3 , min1 * min2 * max1);\n }\n\n```\nThis solution beats 99.72 % of java submissions. Kindly **UPVOTE**, if this helps.\n | 33 | 1 | ['Java'] | 2 |
maximum-product-of-three-numbers | C++ Solution with explanation | c-solution-with-explanation-by-princeerj-881v | Either product of 3 biggest positive values will be maxProduct or if there are negative values then pick the 2 biggest negative values and multiply with biggest | princeerjain | NORMAL | 2017-06-25T03:03:49.183000+00:00 | 2018-08-17T04:37:24.140252+00:00 | 9,395 | false | Either product of 3 biggest positive values will be maxProduct or if there are negative values then pick the 2 biggest negative values and multiply with biggest positive value\n\nSort the Array and compare above mentioned products\n\n\n int maximumProduct(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n int temp1 = nums[n-1]*nums[n-2]*nums[n-3];\n int temp2 = nums[0]*nums[1]*nums[n-1];\n return temp1>temp2?temp1:temp2;\n } | 31 | 2 | [] | 7 |
maximum-product-of-three-numbers | Begineer Friendly || O(nlogn) -> O(n) | begineer-friendly-onlogn-on-by-himanshuc-06t1 | IDEA : Product is going to be maximum if we take product of top 3 maximum number. and also negative*negative became Positive so we will also consider a combinat | himanshuchhikara | NORMAL | 2021-03-19T15:06:26.591642+00:00 | 2021-03-19T15:29:07.187379+00:00 | 1,215 | false | IDEA : Product is going to be maximum if we take product of top 3 maximum number. and also **negative*negative became Positive** so we will also consider a combination of 2 most negative with maximum .\nanswer = Math.max(max1*max2*max3,max1*min1*min2)\n**min1:** minimum element of array\n**min2:** 2nd minimum element of array \n**max1**: maximum element of array\n**max2:** 2nd maximum element of array\n**max3:** 3rd maximum of array \n\n**Solution 1: Sorting**\n```\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int min1=nums[0];\n int min2=nums[1];\n \n int max1=nums[nums.length-1];\n int max2=nums[nums.length-2];\n int max3=nums[nums.length-3];\n \n int c1=max1*(max2*max3);\n int c2=min1*(min2*max1);\n \n return Math.max(c1,c2);\n }\n```\n**Time:O(nlogn) and Space:O(1)**\n\n**Solution 2: Effective Solution** \n```\npublic int maximumProduct(int[] nums) {\n int max1=Integer.MIN_VALUE;\n int max2=Integer.MIN_VALUE;\n int max3=Integer.MIN_VALUE;\n \n int min1=Integer.MAX_VALUE;\n int min2=Integer.MAX_VALUE;\n \n for(int val:nums){\n \n if(val>max1){\n max3=max2;\n max2=max1;\n max1=val;\n }else if(val>max2){\n max3=max2;\n max2=val;\n }else if(val>max3){\n max3=val;\n }\n \n if(val<min1){\n min2=min1;\n min1=val;\n }else if(val<min2){\n min2=val;\n }\n }\n \n return Math.max(max1*max2*max3,min1*min2*max1); \n }\n```\t\n**Time:O(N) && Space:O(1)**\n\n##### Advice for you : Before proceeding to solution confirm your interviewer that array is containing negative values or not?.. If you didn\'t ask these clarification question then even after solving it in most optmised way you may get rejection..\n##### Don\'t Assume anything by yourself in interview\n\nSimilar Question : [Largest number atleast twice of others](https://leetcode.com/problems/largest-number-at-least-twice-of-others/)\n\nPlease **Upvote** if found it helpful:)\n\t | 30 | 0 | [] | 0 |
maximum-product-of-three-numbers | ✅✅Two Java Solution || Easy Explanation 🔥 🔥 | two-java-solution-easy-explanation-by-ri-ydyb | Approach 1\nThe code starts by sorting the input array, "nums," in ascending order using the Arrays.sort() method. Sorting the array allows us to consider diffe | rirefat | NORMAL | 2023-05-30T16:52:09.456002+00:00 | 2023-05-30T16:52:09.456047+00:00 | 6,349 | false | # Approach 1\nThe code starts by sorting the input array, "nums," in ascending order using the **Arrays.sort()** method. Sorting the array allows us to consider different cases and calculate the maximum product.\n\nThe next step involves calculating two potential maximum products:\n\n1. "case1" represents the product of the two smallest numbers (nums[0] and nums[1]) in the sorted array, multiplied by the largest number (nums[nums.length-1]). You may think why do I use the first two number. Just think, if there are negative numbers and if I multiply two negative numbers then the resultant will be positive. \n\n2. "case2" represents the product of the three largest numbers in the sorted array (nums[nums.length-1], nums[nums.length-2], and nums[nums.length-3]).\n\nAfter calculating both cases, the code determines the maximum value between "case1" and "case2" using the Integer.max() method. The maximum value is then stored in the variable "maxProduct."\n\nFinally, the "maxProduct" value is returned as the result of the method.\n\nIn summary, this code finds the maximum product of three numbers in an input array by sorting the array, considering two different cases, and selecting the maximum value between them.\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int case1 = nums[0]*nums[1]*nums[nums.length-1];\n int case2 = nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3];\n\n int maxProduct = Integer.max(case1, case2);\n return maxProduct;\n }\n}\n```\n\n---\n\n# Approach 2\nLet\'s break down the second approach step by step:\n\n1. We have a class named `Solution`, which contains a method `maximumProduct` that takes an integer array `nums` as input and returns an integer.\n\n2. Inside the method, we declare several variables: `min1`, `min2`, `max1`, `max2`, and `max3`. These variables are used to keep track of the minimum and maximum values encountered in the array.\n\n3. We initialize `min1`, `min2`, `max1`, `max2`, and `max3` with the maximum and minimum possible integer values, respectively. This ensures that the initial values will be overwritten by the actual values in the array.\n\n4. We start a loop that iterates over each element in the `nums` array.\n\n5. Inside the loop, we compare the current element with the current maximum values (`max1`, `max2`, and `max3`) to determine if any of them should be updated. If the current element is greater than `max1`, we update `max3`, `max2`, and `max1` accordingly. If it is greater than `max2` but not greater than `max1`, we update `max3` and `max2`. If it is greater than `max3` but not greater than `max2` or `max1`, we update only `max3`.\n\n6. Similarly, we compare the current element with the current minimum values (`min1` and `min2`) to determine if any of them should be updated. If the current element is smaller than `min1`, we update `min2` and `min1` accordingly. If it is smaller than `min2` but not smaller than `min1`, we update only `min2`.\n\n7. After the loop completes, we calculate the maximum product by taking the maximum of two possible products: `max1 * max2 * max3` and `min1 * min2 * max1`. This is done using the `Math.max` function.\n\n8. Finally, we return the maximum product as the result.\n\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n int min1, min2, max1, max2, max3;\n min1 = min2 = Integer.MAX_VALUE;\n max1 = max2 = max3 = Integer.MIN_VALUE;\n for(int i = 0; i < nums.length; i++){\n if(max1 < nums[i]){\n max3 = max2;\n max2 = max1;\n max1 = nums[i];\n }else if(max2 < nums[i]){\n max3 = max2;\n max2 = nums[i];\n }else if(max3 < nums[i]){\n max3 = nums[i];\n }\n \n\n if(min1 > nums[i]){\n min2 = min1;\n min1 = nums[i];\n }else if(min2 > nums[i]){\n min2 = nums[i];\n }\n \n }\n \n return Math.max(max1 * max2 * max3 , min1 * min2 * max1);\n }\n}\n```\n\n**Please upvote if you like the solution.\nHappy Coding!** \uD83D\uDE0A | 29 | 0 | ['Array', 'Math', 'Sorting', 'Java'] | 2 |
maximum-product-of-three-numbers | Beats 100% of users with C++ || Step By Step Explain || Easy to Understand || Best Approach || | beats-100-of-users-with-c-step-by-step-e-duu1 | Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it...\n\n---\n\n# Intuition\n Describe your first thoughts on how to solve this problem. | abhirajpratapsingh | NORMAL | 2024-02-16T18:41:02.047144+00:00 | 2024-02-16T18:41:02.047173+00:00 | 3,627 | false | # Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it...\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem asks for the maximum product that can be obtained by multiplying three integers from the given array. To achieve this, we can observe that the maximum product would be the product of the three largest numbers in the array or the product of the two smallest numbers (if they are negative) and the largest number (to handle negative numbers).\n\n---\n\n\n\n\n\n---\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initialize variables to keep track of the three maximum and two minimum numbers encountered.\n- Iterate through the array:\n - Update the maximum and minimum values accordingly.\n- After iterating through the array, return the maximum of the product of the three maximum numbers and the product of the two minimum numbers and the maximum number.\n\n\n---\n\n# Complexity\n\n- **Time complexity:** O(n), where n is the size of the input array. We iterate through the array once.\n- **Space complexity:** O(1), as we use only a constant amount of extra space regardless of the size of the input array.\n\n---\n# Code\n```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) \n {\n int first_max = INT_MIN ;\n int second_max = INT_MIN ;\n int third_max = INT_MIN ;\n\n int first_min = INT_MAX ;\n int second_min = INT_MAX ;\n\n for( int i = 0 ; i < nums.size() ; i++ )\n {\n if( nums[i] < first_min )\n {\n second_min = first_min ;\n first_min = nums[i] ;\n }\n else if( nums[i] < second_min )\n second_min = nums[i] ;\n if( nums[i] > first_max )\n {\n third_max = second_max ;\n second_max = first_max ;\n first_max = nums[i] ;\n }\n else if( nums[i] > second_max )\n {\n third_max = second_max ;\n second_max = nums[i] ;\n }\n else if( nums[i] > third_max )\n third_max = nums[i] ;\n }\n return max ( first_max * second_max * third_max , first_min * second_min * first_max );\n }\n};\n```\n\n---\n---\n# if you like the solution please UPVOTE it...\n\n\n\n\n\n\n---\n---\n--- | 28 | 0 | ['Array', 'Math', 'Sorting', 'C++'] | 3 |
maximum-product-of-three-numbers | Python O(n) time O(1) space | python-on-time-o1-space-by-leeteatsleep-0nx7 | \nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n smallestTwo = [float(\'inf\')]*2\n largestThree = [float(\'-inf\')]*3\n | leeteatsleep | NORMAL | 2020-07-13T20:21:05.997910+00:00 | 2020-07-13T20:21:05.997946+00:00 | 4,851 | false | ```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n smallestTwo = [float(\'inf\')]*2\n largestThree = [float(\'-inf\')]*3\n for num in nums:\n if num <= smallestTwo[0]:\n smallestTwo[0] = num\n smallestTwo.sort(reverse=True)\n if num >= largestThree[0]:\n largestThree[0] = num\n largestThree.sort()\n return max(smallestTwo[0]*smallestTwo[1]*largestThree[2], \n largestThree[0]*largestThree[1]*largestThree[2])\n```\n\nNote that the sorts take constant time since the sizes of `smallestTwo` and `largestThree` are constant. | 25 | 1 | ['Python', 'Python3'] | 2 |
maximum-product-of-three-numbers | Java solution with sorting | java-solution-with-sorting-by-2020-pkkg | In a sorted array, the maximum product will be either the last three largest elements or the first two (negative elements) multiplied by the last (largest) elem | 2020 | NORMAL | 2020-05-27T00:52:42.079298+00:00 | 2020-05-27T00:52:42.079345+00:00 | 2,020 | false | In a sorted array, the maximum product will be either the last three largest elements or the first two (negative elements) multiplied by the last (largest) element.\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int len = nums.length;\n \n return Math.max(nums[0]*nums[1]*nums[len-1], nums[len-1] * nums[len-2] * nums[len-3]);\n }\n}\n``` | 23 | 0 | ['Java'] | 3 |
maximum-product-of-three-numbers | 2-liner Python solution with explanations | 2-liner-python-solution-with-explanation-nxgw | There are two possible ways to get the largest number:\n1. biggest number * 2nd biggest * 3rd biggest\n2. biggest number * smallest number * 2nd smallest number | yangshun | NORMAL | 2017-06-25T04:41:36.936000+00:00 | 2018-10-14T18:51:34.465828+00:00 | 2,667 | false | There are two possible ways to get the largest number:\n1. biggest number * 2nd biggest * 3rd biggest\n2. biggest number * smallest number * 2nd smallest number (if the two smallest numbers are negative)\n\nThis formula will also work in the case there are all negative numbers, in which the smallest negative number will be the result (based on condition 1).\n\nWe can simply sort the numbers and retrieve the biggest/smallest numbers at the two ends of the array.\n\n```\ndef maximumProduct(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n nums = sorted(nums)\n return max(nums[0]*nums[1]*nums[-1], nums[-3]*nums[-2]*nums[-1])\n``` | 22 | 0 | [] | 1 |
maximum-product-of-three-numbers | JavaScript O(n) time (1) space | javascript-on-time-1-space-by-shengdade-zrdr | javascript\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n let max1 = -Infinity;\n let max2 = -Infinity;\n | shengdade | NORMAL | 2020-02-06T21:59:07.898885+00:00 | 2020-02-06T21:59:07.898923+00:00 | 2,547 | false | ```javascript\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n let max1 = -Infinity;\n let max2 = -Infinity;\n let max3 = -Infinity;\n let min2 = Infinity;\n let min1 = Infinity;\n for (let i = 0; i < nums.length; i++) {\n if (nums[i] > max1) {\n [max1, max2, max3] = [nums[i], max1, max2];\n } else if (nums[i] > max2) {\n [max2, max3] = [nums[i], max2];\n } else if (nums[i] > max3) {\n max3 = nums[i];\n }\n if (nums[i] < min1) {\n [min2, min1] = [min1, nums[i]];\n } else if (nums[i] < min2) {\n min2 = nums[i];\n }\n }\n return Math.max(max1 * max2 * max3, max1 * min1 * min2);\n};\n``` | 20 | 0 | ['JavaScript'] | 2 |
maximum-product-of-three-numbers | ✅ EASIEST || 💯 BEAST || 🧑💻 3 LINES CODE || ⭐ JAVA | easiest-beast-3-lines-code-java-by-mukun-93wb | \n\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe goal is to find the maximum product of three numbers in the array. There are | mukund_rakholiya | NORMAL | 2024-09-27T11:28:49.418833+00:00 | 2024-09-27T11:28:49.418870+00:00 | 4,341 | false | ```\n```\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**The goal is to find the maximum product of three numbers in the array. There are two potential scenarios:**\n1. The three largest numbers yield the maximum product.\n2. In cases where there are negative numbers, the product of the two smallest (negative) numbers and the largest positive number may yield the maximum product.\n\n```\n```\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Sort the Array**: Sorting the array allows us to easily identify the largest numbers as well as the smallest (potentially negative) numbers.\n2. **Calculate Two Possible Products**:\n - The product of the three largest numbers.\n - The product of the two smallest numbers (possibly negative) and the largest number.\n3. **Return the Maximum**: Return the larger of the two products.\n```\n```\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n**`O(n log n)`, where `n` is the length of the array, due to sorting.**\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n**O(1) if sorting is done in place.**\n```\n```\n# Code\n```java []\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n \n int n = nums.length;\n\n return Math.max(nums[n - 1] * nums[n - 2] * nums[n - 3],\n nums[0] * nums[1] * nums[n - 1]);\n }\n}\n```\n\n\n | 19 | 0 | ['Array', 'Math', 'Sorting', 'Java'] | 2 |
maximum-product-of-three-numbers | c++ solution | with proper explaination | O(n) solution | c-solution-with-proper-explaination-on-s-uo5e | The basic approach that we all will first click is that just find the largest three numbers from the given vector nums and calculate the product-> and this wor | kritika_12 | NORMAL | 2021-07-03T15:31:53.497150+00:00 | 2021-07-03T15:59:01.277368+00:00 | 1,536 | false | The **basic approach** that we all will first click is that just find the largest three numbers from the given vector nums and calculate the product-> and this works to some extent only if the given vector nums is having positive elements\nbut what if there are negative elements too this basic approach will fail hence we need to think a better approach from this.\n\n**Better Approach** is to think about the negative elements too. So we know if there are two most negative elements -> it will give us a positive number. \n\nTake 5 variables \nmin1 , min2 -> initialized to INT_MAX\nmax1, max2, max3 ->initialized to INT_MIN\nfind the maximum of (min1* min2* max1 ), max1* max2* max3\nthe max we will get from above is our result.\n**Time complexity of the solution is O(N)**\n**Space Complexity - O(N)**\n\n```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n // find out the min1 , min2, max1, max2, max3\n int min1 = INT_MAX;\n int min2= min1;\n \n int max1 = INT_MIN;\n int max2 = max1; \n int max3 = max1;\n for(int i =0; i< nums.size(); i++){\n \n int val = nums[i];\n // check for maximum \n if(val >= max1){\n max3 = max2; \n max2 = max1;\n max1 = val;\n }\n else if( val >= max2){ // there is a possibilty where val can be smaller than max1 and greater than or equal to max2 \n max3 = max2;\n max2 = val;\n }\n else if(val >= max3){ // there is a possibilty that the val can be smaller than max1 & max2 but greater than or equal to max3\n max3 = val;\n }\n \n // check for minimum \n if(val <= min1){\n min2 = min1;\n min1 = val;\n }else if(val <= min2) {\n min2 = val;\n\n }\n }\n int max_product = max((min1* min2* max1),(max1* max2* max3));\n return max_product;\n }\n};\n```\n**Do upvote if you like the solution and comment if have doubts** | 16 | 0 | ['C', 'C++'] | 1 |
maximum-product-of-three-numbers | 👏Beats 98.57% of users with Java || ✅Simple & Easy Well Explained Solution without Sorting 🔥💥 | beats-9857-of-users-with-java-simple-eas-qls0 | Intuition\nFind First 3 Maximum number and 2 minimum numbers and return maximum of 3 max numbers or 2 min number multiply max number.\n\n# I Think This Can Help | Rutvik_Jasani | NORMAL | 2024-04-04T07:48:55.134122+00:00 | 2024-04-04T07:48:55.134147+00:00 | 2,287 | false | # Intuition\nFind First 3 Maximum number and 2 minimum numbers and return maximum of 3 max numbers or 2 min number multiply max number.\n\n# I Think This Can Help You(For Proof Click on the Image)\n[](https://leetcode.com/problems/maximum-product-of-three-numbers/submissions/1222855261/)\n\n# Approach\n1. Initilize 3 max pointers as min value to store first 3 max numbers.\n ```\n int maxp1 = Integer.MIN_VALUE, maxp2 = Integer.MIN_VALUE, maxp3 = Integer.MIN_VALUE;\n ```\n2. Initilize 2 min pointers as max value to store first 2 min numbers.\n ```\n int minp1 = Integer.MAX_VALUE, minp2 = Integer.MAX_VALUE;\n ```\n3. Iterate over the nums.(Below Code in the loop)\n - Check if maxp1 is smaller than ele then replaced maxp3 to maxp2,maxp2 to maxp1 or maxp1 to ele.\n ```\n if(maxp1<=ele){\n maxp3=maxp2;\n maxp2=maxp1;\n maxp1=ele;\n }\n ```\n - Check if maxp2 is smaller than ele or ele smaller than maxp1 then replace maxp3 to maxp2 or maxp2 to ele.\n ```\n if(maxp2<=ele && ele<maxp1){\n maxp3=maxp2;\n maxp2=ele;\n }\n ```\n - Check if maxp3 is smaller than ele or ele smaller than maxp2 then replace maxp3 to ele.\n ```\n if(maxp3<=ele && ele<maxp2){\n maxp3=ele;\n }\n ```\n - Check if minp1 is greater than ele then replace minp2 to minp1 or minp1 to ele.\n ```\n if(minp1>=ele){\n minp2=minp1;\n minp1=ele;\n }\n ```\n - Check if minp2 is greater than ele or ele greater than minp1 then replace minp2 to ele.\n ```\n if(minp2>=ele && ele>minp1){\n minp2=ele;\n }\n ```\n- Whole loop look like this\n ```\n for (int ele : nums) {\n if(maxp1<=ele){\n maxp3=maxp2;\n maxp2=maxp1;\n maxp1=ele;\n }\n if(maxp2<=ele && ele<maxp1){\n maxp3=maxp2;\n maxp2=ele;\n }\n if(maxp3<=ele && ele<maxp2){\n maxp3=ele;\n }\n if(minp1>=ele){\n minp2=minp1;\n minp1=ele;\n }\n if(minp2>=ele && ele>minp1){\n minp2=ele;\n }\n }\n ```\n4. Return the max of 3 max pointers or 2 min pointers multiply max number.\n```\nreturn Math.max(maxp1 * maxp2 * maxp3, minp1 * minp2 * maxp1);\n```\n\n# Complexity\n- Time complexity:\nO(n) --> We are going linear.\n\n- Space complexity:\nO(1) --> We use Constant Space.\n\n# Code\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n int maxp1 = Integer.MIN_VALUE, maxp2 = Integer.MIN_VALUE, maxp3 = Integer.MIN_VALUE;\n int minp1 = Integer.MAX_VALUE, minp2 = Integer.MAX_VALUE;\n for (int ele : nums) {\n if(maxp1<=ele){\n maxp3=maxp2;\n maxp2=maxp1;\n maxp1=ele;\n }\n if(maxp2<=ele && ele<maxp1){\n maxp3=maxp2;\n maxp2=ele;\n }\n if(maxp3<=ele && ele<maxp2){\n maxp3=ele;\n }\n if(minp1>=ele){\n minp2=minp1;\n minp1=ele;\n }\n if(minp2>=ele && ele>minp1){\n minp2=ele;\n }\n }\n return Math.max(maxp1 * maxp2 * maxp3, minp1 * minp2 * maxp1);\n }\n}\n```\n\n\n | 15 | 0 | ['Array', 'Math', 'Sorting', 'Java'] | 1 |
maximum-product-of-three-numbers | JAVA | USED JUST LOGIC✔EXPLAINATION | java-used-just-logicexplaination-by-rj07-ozym | \nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int n = nums.length;\n int ans = Integer.MIN_VALUE;\ | rj0711 | NORMAL | 2022-09-08T18:00:21.166314+00:00 | 2022-09-12T10:02:33.125057+00:00 | 3,769 | false | ```\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int n = nums.length;\n int ans = Integer.MIN_VALUE;\n \n ans = Math.max(nums[0]*nums[1]*nums[n-1], nums[n-1]*nums[n-2]*nums[n-3]);\n \n return ans;\n }\n}\n``` | 14 | 0 | ['Math', 'Java'] | 2 |
maximum-product-of-three-numbers | Python O( n log n ) sol. based on sorting. run-time 80%+ | python-o-n-log-n-sol-based-on-sorting-ru-lrqr | Python O( n log n ) sol. based on sorting\n\n\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n\n # sort the nums with ascending | brianchiang_tw | NORMAL | 2020-01-06T06:11:22.197875+00:00 | 2020-01-06T06:11:22.197921+00:00 | 1,536 | false | Python O( n log n ) sol. based on sorting\n\n```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n\n # sort the nums with ascending order \n nums.sort()\n \n # for possible solution_#1: First three largest positive numbers\n candidate_1 = nums[-1] * nums[-2] * nums[-3]\n\n # for possible solution_#2: First two smallest negative numbers, whit one largest positive number.\n candidate_2 = nums[-1]*nums[0]*nums[1]\n \n max_product = max( candidate_1, candidate_2)\n \n return max_product\n\n``` | 13 | 0 | ['Sorting', 'Python'] | 1 |
maximum-product-of-three-numbers | Easy solution for js | easy-solution-for-js-by-ahmedrithas48-1gvj | Approach\n Describe your approach to solving the problem. \n- First we have to sort it out by descending.\n- We have two case to get max number with three numbe | ahmedrithas48 | NORMAL | 2024-04-05T12:38:39.582654+00:00 | 2024-04-05T12:38:39.582679+00:00 | 700 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\n- First we have to sort it out by descending.\n- We have two case to get max number with three numbers.\n- we have two cases -\n### If array contains only postive/negative numbers \n- take first 3 numbers .\n- for example if we sort this [4,2,15,6] descending we wil get [15,6,4,2]. \n- so take the first three indices values 15*6*4 to get max product\n### If array contains both postive & negative numbers \n- we take first number and last two numbers.\n- for example if we sort this [-100,-98,-1,2,3,4] desc we will get [4,3,2,1,-1,-98,-100] \n- if we take 4 , -100 , -98 and product it then we will get max product.\n\nAt last we compare both approach and check which approach value is greater , then return that value\n\n# Code\n```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n nums.sort((a,b)=> b-a);\n let l = nums.length;\n let value1 = nums[0] * nums[1] * nums[2];\n let value2 = nums[0] * nums[l-1] * nums[l-2];\n return value1 > value2 ? value1 : value2;\n};\n````please upvote me if this code helped you```` | 12 | 0 | ['JavaScript'] | 0 |
maximum-product-of-three-numbers | Simple Java || C++ Code ☠️ | simple-java-c-code-by-abhinandannaik1717-9wy6 | java []\nclass Solution {\n public int maximumProduct(int[] nums) {\n int n = nums.length;\n Arrays.sort(nums);\n int m1 = nums[n-1]*num | abhinandannaik1717 | NORMAL | 2024-06-25T16:57:33.077575+00:00 | 2024-06-25T16:57:33.077601+00:00 | 3,059 | false | ```java []\nclass Solution {\n public int maximumProduct(int[] nums) {\n int n = nums.length;\n Arrays.sort(nums);\n int m1 = nums[n-1]*nums[n-2]*nums[n-3];\n int m2 = nums[0]*nums[1]*nums[n-1];\n if(m1>m2){\n return m1;\n }\n else{\n return m2;\n }\n }\n}\n```\n```C++ []\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int n = nums.size();\n sort(nums.begin(),nums.end());\n int m1 = nums[n-1]*nums[n-2]*nums[n-3];\n int m2 = nums[0]*nums[1]*nums[n-1];\n if(m1>m2){\n return m1;\n }\n else{\n return m2;\n }\n }\n};\n```\n | 10 | 0 | ['Java'] | 4 |
maximum-product-of-three-numbers | 2 Lines of Code-->Powerful Heap Concept | 2-lines-of-code-powerful-heap-concept-by-uk9p | 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 | GANJINAVEEN | NORMAL | 2023-03-18T05:36:17.220860+00:00 | 2023-03-18T05:36:17.220895+00:00 | 2,056 | 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)$$ -->\n\n# Code\n```\n #Math Approach\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n return max(nums[0]*nums[1]*nums[-1],nums[-1]*nums[-2]*nums[-3])\n\n #please upvote me it would encourage me alot\n\n```\n\n```\n #Heap Approach\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n pos=heapq.nlargest(3,nums)\n neg=heapq.nsmallest(2,nums)\n return max(neg[0]*neg[1]*pos[0],pos[0]*pos[1]*pos[2])\n #please upvote me it would encourage me alot\n\n\n | 10 | 0 | ['Python3'] | 0 |
maximum-product-of-three-numbers | [Java] 3-pointer Method {99.73%} faster than orther solutions | java-3-pointer-method-9973-faster-than-o-2r7o | If you do like solution please upvote it , so it could be helpful to orthers\nSorting Method :\n\nclass Solution {\n public int maximumProduct(int[] nums) {\ | salmanshaikssk007 | NORMAL | 2021-12-09T06:44:30.211296+00:00 | 2021-12-09T07:04:42.539924+00:00 | 945 | false | **If you do like solution please upvote it , so it could be helpful to orthers**\nSorting Method :\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n \n Arrays.sort(nums);\n int n = nums.length;\n return Math.max(nums[n-1]*nums[n-2]*nums[n-3] , nums[0]*nums[1]*nums[n-1]);\n }\n}\n```\n3 Pointer Method\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n\n int min1 = Integer.MAX_VALUE,min2 = Integer.MAX_VALUE;\n int max1 = Integer.MIN_VALUE , max2 = Integer.MIN_VALUE , max3 = Integer.MIN_VALUE ;\n \n for(int i = 0 ; i < nums.length ; i++){\n// to find max1 , max2 , max3 in the array\n if(nums[i]>max1){\n max3 = max2 ;\n max2 = max1 ;\n max1 = nums[i];\n \n }else if (nums[i]>max2){\n max3=max2;\n max2=nums[i];\n }else if (nums[i]>max3){\n max3=nums[i];\n }\n// To find min1 , min2 in the array\n if(nums[i]<min1){\n min2 = min1;\n min1 = nums[i];\n }else if(nums[i]<min2) {\n min2 = nums[i];\n }\n }\n return Math.max(max1*max2*max3 , min1*min2*max1);\n }\n\n}\n``` | 10 | 0 | ['Sorting', 'Java'] | 0 |
maximum-product-of-three-numbers | 🏆💢💯 Faster✅💯 Lesser🧠 🎯 C++✅Python3🐍✅Java✅C✅Python🐍✅💥🔥💫Explained☠💥🔥 Beats 💯 | faster-lesser-cpython3javacpythonexplain-69nu | Intuition\n\n\n\n\n\nC++ []\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nu | Edwards310 | NORMAL | 2024-05-29T03:01:36.719656+00:00 | 2024-05-29T03:01:36.719673+00:00 | 3,337 | false | # Intuition\n\n\n\n\n\n```C++ []\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n return max(nums[n - 1] * nums[n - 2] * nums[n - 3],\n nums[0] * nums[1] * nums[n - 1]);\n }\n};\n```\n```python3 []\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n first_max = float(\'-inf\')\n second_max = first_max\n third_max = first_max\n\n first_min = float(\'inf\')\n second_min = first_min\n\n for i in range(len(nums)):\n if first_max <= nums[i]:\n third_max = second_max\n second_max = first_max\n first_max = nums[i]\n elif second_max <= nums[i]:\n third_max = second_max\n second_max = nums[i]\n elif third_max <= nums[i]:\n third_max = nums[i]\n\n if(first_min >= nums[i]):\n second_min = first_min\n first_min = nums[i]\n elif second_min >= nums[i]:\n second_min = nums[i]\n \n return max(first_max * second_max * third_max, first_min * second_min * first_max)\n```\n```C []\nint maximumProduct(int* nums, int numsSize) {\n int first_max = INT_MIN;\n int second_max = first_max;\n int third_max = first_max;\n\n int first_min = INT_MAX;\n int second_min = first_min;\n\n for (int i = 0; i < numsSize; i++) {\n // Calculating first_max, second_max, and third_max\n if (first_max <= nums[i]) {\n third_max = second_max;\n second_max = first_max;\n first_max = nums[i];\n } else if (second_max <= nums[i]) {\n third_max = second_max;\n second_max = nums[i];\n } else if (third_max <= nums[i])\n third_max = nums[i];\n\n\n // Calculating the first_min, second_min\n if (first_min >= nums[i]) {\n second_min = first_min;\n first_min = nums[i];\n } else if (second_min >= nums[i])\n second_min = nums[i];\n \n }\n int maxi = fmax(first_max * second_max * third_max, first_min * second_min * first_max);\n\n return maxi;\n}\n```\n```Java []\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int n = nums.length;\n return Math.max(nums[n - 1] * nums[n - 2] * nums[n - 3], nums[0] * nums[1] * nums[n - 1]);\n }\n}\n```\n```python []\nclass Solution(object):\n def maximumProduct(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n min1 = float(\'inf\')\n min2 = float(\'inf\')\n max1 = float(\'-inf\')\n max2 = float(\'-inf\')\n max3 = float(\'-inf\')\n if len(nums) < 3:\n return 0\n \n for num in nums:\n if num < 0:\n if num < min2:\n if num <= min1:\n min2 = min1\n min1 = num\n else:\n min2 = num\n if num > max3:\n if num > max2:\n if num >= max1:\n max3 = max2\n max2 = max1\n max1 = num\n else:\n max3 = max2\n max2 = num\n else:\n max3 = num\n\n if min1 <= 0 and min2 <= 0 and max1 >= 0 and min1 * min2 > max2*max3:\n return min1 * min2 * max1\n return max1 * max2 * max3\n```\n\n\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 O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n O(1)\n# Code\n```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n sort(nums.begin(), nums.end());\n int n = nums.size();\n return max(nums[n - 1] * nums[n - 2] * nums[n - 3],\n nums[0] * nums[1] * nums[n - 1]);\n }\n};\n```\n# ***Please upvote if it\'s useful for you***\n\n | 9 | 0 | ['Array', 'Math', 'C', 'Sorting', 'Python', 'C++', 'Java', 'Python3'] | 5 |
maximum-product-of-three-numbers | JavaScript solution | javascript-solution-by-jagdish7908-j7mw | \n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n nums = nums.sort((a,b) => b-a)\n return Math.max(nums[ | jagdish7908 | NORMAL | 2021-10-24T08:25:10.839421+00:00 | 2021-10-24T08:26:54.493698+00:00 | 1,491 | false | ```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n nums = nums.sort((a,b) => b-a)\n return Math.max(nums[0]*nums[1]*nums[2], nums[nums.length-1]*nums[nums.length-2]*nums[0])\n};\n``` | 9 | 0 | ['Sorting', 'JavaScript'] | 2 |
maximum-product-of-three-numbers | C++ {Speed,Mem} = {O(n), O(1)} simple | c-speedmem-on-o1-simple-by-crab_10legs-c2c1 | \nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n \n \n int min_c = INT_MAX;\n int min_n = INT_MAX;\n | crab_10legs | NORMAL | 2020-07-19T05:31:17.445005+00:00 | 2020-07-19T05:35:34.442758+00:00 | 1,529 | false | ```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n \n \n int min_c = INT_MAX;\n int min_n = INT_MAX;\n int max_c = INT_MIN;\n int max_n = INT_MIN;\n int max_n_n = INT_MIN;\n \n for(int i=0; i < nums.size(); i++){\n // 2 smallets elements\n if(min_c > nums[i]){\n min_n = min_c;\n min_c = nums[i];\n \n }\n else if (min_n > nums[i])\n min_n = nums[i];\n \n // 3 largest elements\n if(max_c < nums[i]){\n max_n_n = max_n;\n max_n = max_c;\n max_c = nums[i];\n }\n else if(max_n < nums[i]){\n max_n_n = max_n;\n max_n = nums[i];\n }\n else if(max_n_n < nums[i])\n max_n_n = nums[i];\n }\n \n return max(min_c*min_n*max_c, max_n_n*max_n*max_c);\n }\n};\n``` | 9 | 1 | ['C', 'C++'] | 3 |
maximum-product-of-three-numbers | Best Solution in C++ for Trees 🚀 || 100% working | best-solution-in-c-for-trees-100-working-8ykh | IntuitionTo find the maximum product, we should consider the largest numbers. But what if the array has negative numbers? 🤔 Negative numbers can flip the result | BladeRunner150 | NORMAL | 2024-12-31T06:05:32.222778+00:00 | 2024-12-31T06:05:32.222778+00:00 | 1,554 | false | # Intuition
To find the maximum product, we should consider the largest numbers. But what if the array has negative numbers? 🤔 Negative numbers can flip the result! So, we also check the smallest two negatives multiplied with the largest positive number. 🚀
# Approach
1. Sort the array. 🧹
2. Compare:
- The product of the three largest numbers.
- The product of the largest number and the two smallest numbers.
3. Return the maximum of the two. 🌟
# Complexity
- **Time complexity:** $$O(n \log n)$$ (because of sorting).
- **Space complexity:** $$O(1)$$ (in-place sorting).
# Code
```cpp
class Solution {
public:
int maximumProduct(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
return max(nums[n-1] * nums[n-2] * nums[n-3], nums[n-1] * nums[0] * nums[1]);
}
};
```
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">
# Connect with me on LinkedIn for more insights! 🌟 Link in bio | 8 | 0 | ['Array', 'Math', 'Sorting', 'C++'] | 0 |
maximum-product-of-three-numbers | [Python3] O(N) and O(NlogN) solutions | python3-on-and-onlogn-solutions-by-ye15-yoif | The maximum can be one of below \n1) multiplying the three largest number;\n2) multiplying the largest number with two negative numbers largest in magnitude. \n | ye15 | NORMAL | 2019-08-12T03:02:50.896669+00:00 | 2020-06-29T19:49:20.721427+00:00 | 1,134 | false | The maximum can be one of below \n1) multiplying the three largest number;\n2) multiplying the largest number with two negative numbers largest in magnitude. \n\nSo to loop through the list and keep track of the three largest and two smallest numbers is enough to compute the maximum. \n\n```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n max1 = max2 = max3 = float("-inf")\n min1 = min2 = float("inf")\n \n for num in nums: \n if num > max1:\n max1, max2, max3 = num, max1, max2\n elif num > max2:\n max2, max3 = num, max2\n elif num > max3:\n max3 = num\n \n if num < min1:\n min1, min2 = num, min1\n elif num < min2:\n min2 = num\n \n return max(max2*max3, min1*min2) * max1 \n```\n\nAlternatively, one could sort `nums`, and pick the larger one from `nums[0]*nums[1]*nums[-1]` and `nums[-3]*nums[-2]*nums[-1]`. \n```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n return max(nums[0]*nums[1]*nums[-1], nums[-3]*nums[-2]*nums[-1])\n```\n\nPS: The test cases don\'t include all negative numbers as of June 29, 2020. | 8 | 0 | ['Python3'] | 1 |
maximum-product-of-three-numbers | Python, Straightforward with Explanation | python-straightforward-with-explanation-g0i4c | Sort the array. Any "middle" numbers not in the first 3 or last 3 cannot be used in the final answer. If we are using a middle number, it must have both a lef | awice | NORMAL | 2017-06-25T03:06:12.043000+00:00 | 2017-06-25T03:06:12.043000+00:00 | 4,850 | false | Sort the array. Any "middle" numbers not in the first 3 or last 3 cannot be used in the final answer. If we are using a middle number, it must have both a left-neighbor and a right-neighbor, and switching to one of these neighbors will increase the product. \n\n```\ndef maximumProduct(self, A):\n A.sort()\n if len(A) > 6:\n A = A[:3] + A[-3:]\n \n return max(A[i] * A[j] * A[k]\n \t for i in xrange(len(A))\n \t for j in xrange(i+1, len(A))\n \t for k in xrange(j+1, len(A)))\n``` | 8 | 0 | [] | 2 |
maximum-product-of-three-numbers | 🎯SUPER EASY FOR BEGINNERS 👏 | super-easy-for-beginners-by-vigneshvaran-32qf | Code | vigneshvaran0101 | NORMAL | 2025-02-03T17:30:37.394114+00:00 | 2025-02-03T17:30:37.394114+00:00 | 1,115 | false |
# Code
```python3 []
class Solution:
def maximumProduct(self, nums: List[int]) -> int:
a = sorted(nums)
return max(a[-1]*a[-2]*a[-3], a[0]*a[1]*a[-1])
# nums[-1]*nums[-2]*nums[-3] gives the product of 3 maximum number (positive)
# nums[0]*nums[1]*nums[-1] gives the product of 2 biggest negative number not considering the negative sign and the biggest positive number.
```

| 7 | 0 | ['Python3'] | 0 |
maximum-product-of-three-numbers | Python O(NlogN) Solution Beat 100% | python-onlogn-solution-beat-100-by-tliu7-64os | Sort the array first, then check the product of (3 largest positive numbers) and (2 largest negative numbers and largest positive numbers).\n\n\nclass Solution: | tliu77 | NORMAL | 2019-11-29T02:55:45.130846+00:00 | 2019-11-29T02:56:02.189072+00:00 | 1,307 | false | Sort the array first, then check the product of (3 largest positive numbers) and (2 largest negative numbers and largest positive numbers).\n\n```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n return max(nums[0]*nums[1]*nums[-1], nums[-1]*nums[-2]*nums[-3])\n``` | 7 | 0 | ['Python', 'Python3'] | 0 |
maximum-product-of-three-numbers | Java sort, 3 lines | java-sort-3-lines-by-cubicon-otm8 | \npublic int maximumProduct(int[] a) {\n Arrays.sort(a);\n int len = a.length;\n return Math.max(a[0] * a[1] * a[len-1], a[len-1] * a[len-2 | Cubicon | NORMAL | 2017-06-25T03:07:39.140000+00:00 | 2017-06-25T03:07:39.140000+00:00 | 1,499 | false | ```\npublic int maximumProduct(int[] a) {\n Arrays.sort(a);\n int len = a.length;\n return Math.max(a[0] * a[1] * a[len-1], a[len-1] * a[len-2] * a[len-3]);\n\n }\n``` | 7 | 2 | [] | 3 |
maximum-product-of-three-numbers | Easy To Understand | 3 Lines 🚀🚀 | easy-to-understand-3-lines-by-sanket_des-07uh | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Use this test case for better understanding --> [-100,-98,-1,2,3,4]\n\n# | The_Eternal_Soul | NORMAL | 2023-10-17T13:03:42.620490+00:00 | 2023-10-17T13:03:42.620509+00:00 | 2,952 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n Use this test case for better understanding --> [-100,-98,-1,2,3,4]\n\n# Complexity\n- Time complexity: O(NlogN)\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 maximumProduct(vector<int>& nums) {\n ios_base::sync_with_stdio(false);\n cin.tie(0);\n cout.tie(0);\n \n sort(nums.begin(),nums.end());\n int n = nums.size();\n\n return max(nums[n-1] * nums[n-2] * nums[n-3], nums[0] * nums[1] * nums[n-1]);\n }\n};\n``` | 6 | 0 | ['Array', 'Math', 'C', 'Sorting', 'C++', 'Java', 'Python3'] | 0 |
maximum-product-of-three-numbers | Maximum Product of Three Numbers Solution in C++ | maximum-product-of-three-numbers-solutio-vrju | 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_Kunal_Singh | NORMAL | 2023-05-02T16:52:49.674136+00:00 | 2023-05-02T16:52:49.674179+00:00 | 1,450 | 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)$$ -->\nO(nlogn)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int prod1, prod2;\n sort(nums.begin(), nums.end());\n prod1 = nums[nums.size()-1]*nums[nums.size()-2]*nums[nums.size()-3];\n prod2 = nums[nums.size()-1]*nums[0]*nums[1];\n return max(prod1, prod2);\n }\n};\n```\n\n | 6 | 0 | ['C++'] | 0 |
maximum-product-of-three-numbers | Python O(n) solution || NO fancy one liner | python-on-solution-no-fancy-one-liner-by-gasn | \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. O(n) \n | Augus7 | NORMAL | 2023-01-31T16:06:52.400746+00:00 | 2023-01-31T16:09:57.733218+00:00 | 2,325 | false | \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. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def maximumProduct(self, vec: List[int]) -> int:\n if len(vec) == 3:\n return vec[0] * vec[1] * vec[2]\n max1 = max2 = max3 = -1000 \n min1 = min2 = 1000\n for i in vec:\n if i > max1:\n max3 = max2\n max2 = max1\n max1 = i\n elif i > max2:\n max3 = max2\n max2 = i\n elif i > max3:\n max3 = i\n if i < min1:\n min2 = min1\n min1 = i\n elif i < min2:\n min2 = i\n return max(max1 * max2 * max3, min1 * min2 * max1)\n``` | 6 | 0 | ['Array', 'Math', 'Python3'] | 3 |
maximum-product-of-three-numbers | Most Efficient and Optimized || Fully Explained || O(n) || Java😊 | most-efficient-and-optimized-fully-expla-hsz6 | \n# Approach :\n\n1. Scan the array and compute Maximum, second maximum and third maximum element present in the array.\n2. Scan the array and compute Minimum a | N7_BLACKHAT | NORMAL | 2022-12-05T12:46:43.214826+00:00 | 2022-12-05T12:46:43.214867+00:00 | 1,850 | false | \n# Approach :\n\n1. Scan the array and compute Maximum, second maximum and third maximum element present in the array.\n2. Scan the array and compute Minimum and second minimum element present in the array.\n3. Return the maximum of product of Maximum, second maximum and third maximum and product of Minimum, second minimum and Maximum element.\n\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. $$O(n)$$ -->\n\n# Note:\n```\nStep 1 and Step 2 can be done in a single traversal \nof the array.\n```\n\n# Code (Explained in Comments)\n```\nclass Solution \n{\n public int maximumProduct(int[] nums) \n {\n // Initialize Maximum, second maximum\n // and third maximum element\n int maxA=Integer.MIN_VALUE;\n int maxB=Integer.MIN_VALUE;\n int maxC=Integer.MIN_VALUE;\n\n // Initialize Minimum and\n // second minimum element\n int minA=Integer.MAX_VALUE;\n int minB=Integer.MAX_VALUE;\n \n for(int i=0;i<nums.length;i++)\n {\n // Update Maximum, second maximum\n // and third maximum element\n if(nums[i]>maxA)\n {\n maxC=maxB;\n maxB=maxA;\n maxA=nums[i];\n }\n\n // Update second maximum and\n // third maximum element\n else if(nums[i]>maxB)\n {\n maxC=maxB;\n maxB=nums[i];\n }\n\n // Update third maximum element\n else if(nums[i]>maxC)\n {\n maxC=nums[i];\n }\n\n // Update Minimum and second\n // minimum element\n if(nums[i]<minA)\n {\n minB=minA;\n minA=nums[i];\n }\n\n // Update second minimum element\n else if(nums[i]<minB)\n {\n minB=nums[i];\n }\n }\n return Math.max(maxA*maxB*maxC,minA*minB*maxA);\n }\n}\n```\n# *If you find this solution easy to understand and helpful, then please Upvote\uD83D\uDC4D\uD83D\uDC4D* | 6 | 0 | ['Array', 'Math', 'Java'] | 2 |
maximum-product-of-three-numbers | c++ | Two approaches | Fast solution | Asked in interview | c-two-approaches-fast-solution-asked-in-8ie8y | \n\n\n////Sorting method\n\nclass Solution {\npublic:\n int maximumProduct(vector<int>& a) {\n sort(a.begin(),a.end());\n int n=a.size();\n | srv-er | NORMAL | 2021-09-28T01:27:07.099334+00:00 | 2021-09-28T01:27:07.099460+00:00 | 916 | false | \n\n```\n////Sorting method\n\nclass Solution {\npublic:\n int maximumProduct(vector<int>& a) {\n sort(a.begin(),a.end());\n int n=a.size();\n return max(a[n-1]*a[n-2]*a[n-3],max(a[0]*a[1]*a[2],a[0]*a[1]*a[n-1]));\n \n }\n};\n\n////linear solution \n\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int mn1=10001,mn2=10001,mx1=-10001,mx2=-10001,mx3=-10001;\n for(auto& i:nums){\n if(i>mx1){\n mx3=mx2;\n mx2=mx1;\n mx1=i;\n }\n else if(i>mx2){ \n mx3=mx2;\n mx2=i;\n }\n else if(i>mx3)mx3=i;\n if(i<mn1){\n mn2=mn1;\n mn1=i;\n }else if(i<mn2)mn2=i;\n }\n return max(mn1*mn2*mx1,mx1*mx2*mx3); \n }\n};\n``` | 6 | 0 | ['C', 'Sorting'] | 0 |
maximum-product-of-three-numbers | Java || Easy To Understand || nlog(n) | java-easy-to-understand-nlogn-by-vishal_-2u90 | Code\n\nclass Solution {\n public int maximumProduct(int[] nums) {\n int n=nums.length;\n Arrays.sort(nums);\n return Math.max(nums[0]*n | vishal_niet | NORMAL | 2023-03-21T05:27:20.946538+00:00 | 2023-03-21T05:27:20.946585+00:00 | 1,741 | false | # Code\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n int n=nums.length;\n Arrays.sort(nums);\n return Math.max(nums[0]*nums[1]*nums[n-1],nums[n-1]*nums[n-2]*nums[n-3]);\n // return max_product;\n \n }\n}\n``` | 5 | 0 | ['Sorting', 'Java'] | 0 |
maximum-product-of-three-numbers | 2 Approach 🔥 || Easy C++ ✅ Solution || Brute Force [Sorting] and Optimized Approach ⚡ | 2-approach-easy-c-solution-brute-force-s-q0lm | Hint\n\nHint 1: Scan the array and compute Maximum, second maximum, third maximum element, and minimum and second minimum element present in the array.\n\nHint | Vishal_Singh7 | NORMAL | 2023-03-10T09:21:38.183202+00:00 | 2023-03-10T09:21:38.183254+00:00 | 721 | false | # Hint\n\n**Hint 1:** Scan the array and compute Maximum, second maximum, third maximum element, and minimum and second minimum element present in the array.\n\n**Hint 2:** The answer will always be maximum of product of Maximum, second maximum and third maximum and product of Minimum, second minimum and Maximum element.\n\n\n\n\n# Approach 1: Brute Force Approach [Sorting]\n\n# Complexity\n- Time complexity: O(nlogn)\n\n- Space complexity: O(1)\n\n\n# Code\n```\n// Brute Force Approach\n// Time complexity -> O(nlogn) and Space -> O(1)\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n \n int n=nums.size();\n if(n<3)\n {\n return -1;\n }\n sort(nums.begin(),nums.end());\n\n int maxi=max(nums[n-3]*nums[n-2]*nums[n-1],nums[0]*nums[1]*nums[n-1]);\n\n return maxi;\n }\n};\n```\n\n# Approach 2: Optimized Approach\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(1)\n\n**Steps**\n\n1. First calcualte all of this below value:\n\n - first_max means Largest number in an array\n - second_max means second largest number in an array\n - third_max means third largest number in an array\n - first_min means smallest number in an array\n - second_min means second smallest number in an array\n\n2. Then the answer will always be maximum of product of first_max, second_max and third_max and product of first_min, second_min and first_max.\n\n\n# Code\n```\n// Optimized Approach\n// Time complexity -> O(n) and Space -> O(1)\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n\n int first_max=INT_MIN;\n int second_max=first_max;\n int third_max=first_max;\n\n int first_min=INT_MAX;\n int second_min=first_min;\n\n for(int i=0;i<nums.size();i++)\n {\n // Calculating first_max, second_max, and third_max\n if(first_max<=nums[i])\n {\n third_max=second_max;\n second_max=first_max;\n first_max=nums[i];\n }\n else if(second_max<=nums[i])\n {\n third_max=second_max;\n second_max=nums[i];\n }\n else if(third_max<=nums[i])\n {\n third_max=nums[i];\n }\n\n // Calculating the first_min, second_min\n if(first_min>=nums[i])\n {\n second_min=first_min;\n first_min=nums[i];\n }\n else if(second_min>=nums[i])\n {\n second_min=nums[i];\n }\n }\n int maxi=max(first_max*second_max*third_max,first_min*second_min*first_max);\n\n return maxi;\n }\n};\n``` | 5 | 0 | ['Sorting', 'C++'] | 1 |
maximum-product-of-three-numbers | Easiest C++ Solution | easiest-c-solution-by-harsh_1818-kvfk | Intuition\nwe will sort the vector.\n\n# Approach\nAfter sorting answer will always be product of first 2numbers and last number or last 3numbers.\n# Complexity | harsh_1818 | NORMAL | 2023-02-02T09:26:42.901282+00:00 | 2023-02-02T09:26:42.901335+00:00 | 2,132 | false | # Intuition\nwe will sort the vector.\n\n# Approach\nAfter sorting answer will always be product of first 2numbers and last number or last 3numbers.\n# Complexity\n- Time complexity:\nnlogn\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n\n sort(nums.begin(),nums.end());\n\n int e=nums.size()-1;\n int ans1=nums[e]*nums[e-1]*nums[e-2];\n int ans2=nums[e]*nums[0]*nums[1];\n\n int final=max(ans1,ans2);\n return final;\n \n }\n};\n``` | 5 | 0 | ['C++'] | 1 |
maximum-product-of-three-numbers | 4 Lines of Python code | 4-lines-of-python-code-by-prernaarora221-132a | ```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n l1 = nums[-1]nums[-2]nums[-3]\n l2 = nums[0]num | prernaarora221 | NORMAL | 2022-06-06T18:23:04.847104+00:00 | 2022-06-06T18:23:04.847176+00:00 | 2,165 | false | ```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n l1 = nums[-1]*nums[-2]*nums[-3]\n l2 = nums[0]*nums[1]*nums[-1]\n return max(l1,l2) | 5 | 0 | ['Python', 'Python3'] | 1 |
maximum-product-of-three-numbers | C++ Simple and clean solution for Beginners || Brute force | c-simple-and-clean-solution-for-beginner-87kd | Please Upvote if it helped you !!!\nHappy Coding :) \n \n \n\nint maximumProduct(vector<int>& nums) \n {\n sort(nums.begin(),nums.end());\n int | rahulrajguru | NORMAL | 2022-01-13T10:48:25.632376+00:00 | 2022-07-13T12:35:50.318519+00:00 | 459 | false | Please Upvote if it helped you !!!\nHappy Coding :) \n \n \n```\nint maximumProduct(vector<int>& nums) \n {\n sort(nums.begin(),nums.end());\n int n=nums.size();\n int max= nums[n-1]*nums[n-2]*nums[n-3];\n if(max > nums[0]*nums[1]*nums[n-1])\n return max;\n return nums[0]*nums[1]*nums[n-1];\n }\n```\t | 5 | 0 | ['C'] | 1 |
maximum-product-of-three-numbers | C++ code with Explanation | c-code-with-explanation-by-vishal_shady2-2f87 | Explanation:\nFirst we sort the array. Since the largest 3 numbers will make the largest product, we can look for the last 3 digits and multiply them. if the nu | Vishal_Shady23 | NORMAL | 2021-07-07T07:01:53.015764+00:00 | 2021-07-07T07:01:53.015792+00:00 | 612 | false | **Explanation:**\n*First we sort the array. Since the largest 3 numbers will make the largest product, we can look for the last 3 digits and multiply them. if the number contains more than 2 negative numbers with maximum magnitide. Then their product will yield maximum value along with the largest positive number. Example -100,-90,1,2,3,4 will have max val of (-100)(-90)(4). We return the maximum of the two as result.* \n```\n int maximumProduct(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n if(nums.size()<3) return 0;\n int n = nums.size();\n int m = nums[n-1]*nums[n-2]*nums[n-3];\n int l = nums[n-1]*nums[1]*nums[0];\n return max(m,l);\n }\n``` | 5 | 0 | ['C'] | 0 |
maximum-product-of-three-numbers | Java | 2ms | 99.33% faster | java-2ms-9933-faster-by-aish9-lerd | \n\nclass Solution {\n public int maximumProduct(int[] nums) {\n if(nums.length==3)\n return nums[0]*nums[1]*nums[2];\n int max1st=I | aish9 | NORMAL | 2021-06-09T15:14:51.174975+00:00 | 2021-06-09T15:14:51.175025+00:00 | 660 | false | \n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n if(nums.length==3)\n return nums[0]*nums[1]*nums[2];\n int max1st=Integer.MIN_VALUE,max2nd=Integer.MIN_VALUE,max3rd=Integer.MIN_VALUE,min1 = Integer.MAX_VALUE,min2 = Integer.MAX_VALUE;\n for(int num:nums){\n if(max1st<=num){\n max3rd=max2nd;\n max2nd=max1st;\n max1st=num; \n }\n else if(max2nd<=num)\n {\n max3rd=max2nd;\n max2nd=num; \n }\n else if(max3rd<=num) \n max3rd=num; \n if (num<min1) {\n min2 = min1;\n min1 = num;\n }\n else if (num<min2) {\n min2 = num;\n }\n }\n return Math.max(min1*min2*max1st,max1st*max2nd*max3rd);\n }\n}\n\n\n\n``` | 5 | 0 | ['Java'] | 1 |
maximum-product-of-three-numbers | python 3 with only 2 lines(easy to understand) | python-3-with-only-2-lineseasy-to-unders-ozq0 | \nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n a = sorted(nums)\n return(max(a[-1] *a[-2]*a[-3],a[0]*a[1]*a[-1]))\n | zhuanhuan | NORMAL | 2019-08-14T02:40:19.062308+00:00 | 2019-08-14T02:40:19.065222+00:00 | 1,520 | false | ```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n a = sorted(nums)\n return(max(a[-1] *a[-2]*a[-3],a[0]*a[1]*a[-1]))\n``` | 5 | 0 | ['Python', 'Python3'] | 1 |
maximum-product-of-three-numbers | JavaScript solution beat 100% | javascript-solution-beat-100-by-zhenximi-una9 | \n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n let max1 = max2 = max3 = Number.NEGATIVE_INFINITY;\n l | zhenximi | NORMAL | 2018-06-13T02:29:38.697194+00:00 | 2018-06-13T02:29:38.697194+00:00 | 747 | false | ```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n let max1 = max2 = max3 = Number.NEGATIVE_INFINITY;\n let min1 = min2 = Number.POSITIVE_INFINITY;\n for (var i = 0; i < nums.length; i++) {\n var n = nums[i];\n if(n >= max1) {\n max3 = max2;\n max2 = max1;\n max1 = n;\n } else if (n >= max2) {\n max3 = max2;\n max2 = n;\n } else if (n >= max3) {\n max3 = n;\n }\n if(n <= min1) {\n min2 = min1;\n min1 = n;\n } else if (n <= min2) {\n min2 = n;\n }\n };\n const product1 = max1 * max2 * max3;\n const product2 = max1 * min2 * min1;\n return product1 > product2 ? product1 : product2;\n}\n``` | 5 | 0 | [] | 1 |
maximum-product-of-three-numbers | Max Product Trio: Navigating Negatives for Maximum Gain Using C++ | max-product-trio-navigating-negatives-fo-lmkb | Intuition\nThe problem requires finding the maximum product of three numbers from the list. The key observation is that the maximum product could either come fr | Krishnaa2004 | NORMAL | 2024-09-07T13:33:39.189013+00:00 | 2024-09-07T13:33:39.189043+00:00 | 742 | false | # Intuition\nThe problem requires finding the maximum product of three numbers from the list. The key observation is that the maximum product could either come from the three largest numbers or from two smallest (negative) numbers and the largest number.\n\n# Approach\n1. Sort the array.\n2. Calculate the product of the two smallest numbers (which could be negative) and the largest number.\n3. Calculate the product of the three largest numbers.\n4. Return the maximum of these two products.\n\n# Complexity\n- Time complexity: $$O(n \\log n)$$, where $$n$$ is the size of the input array. The sorting step dominates the time complexity.\n- Space complexity: $$O(1)$$, as we are using constant extra space.\n\n# Code\n```cpp\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int l = nums.size(); \n sort(nums.begin(), nums.end()); \n int withNeg = nums[0] * nums[1] * nums[l-1]; \n int withoutNeg = nums[l-1] * nums[l-2] * nums[l-3]; \n return max(withNeg, withoutNeg); \n }\n};\n```\n | 4 | 0 | ['C++'] | 0 |
maximum-product-of-three-numbers | Superb Logic in java and Python(Heap concept) | superb-logic-in-java-and-pythonheap-conc-t9ng | \n\n# Superb Solution in JAVA\n\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int n=nums.length;\n return | GPraveen | NORMAL | 2023-05-08T03:52:36.531127+00:00 | 2023-05-08T03:52:36.531169+00:00 | 1,161 | false | \n\n# Superb Solution in JAVA\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int n=nums.length;\n return Math.max(nums[0]*nums[1]*nums[n-1],nums[n-1]*nums[n-2]*nums[n-3]); \n }\n}\n```\n# Excellent Solution in Python Using Heap Concept\n```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n pos=heapq.nlargest(3,nums)\n neg=heapq.nsmallest(2,nums)\n return max(neg[0]*neg[1]*pos[0],pos[-1]*pos[-2]*pos[-3])\n``` | 4 | 0 | ['Heap (Priority Queue)', 'Java', 'Python3'] | 0 |
maximum-product-of-three-numbers | Simple C++ Solution | simple-c-solution-by-divyanshu_singh_cs-zbf3 | Intuition\n Describe your first thoughts on how to solve this problem. \nFirst we sort the vector then we return the product of first 2 element and last element | Divyanshu_singh_cs | NORMAL | 2023-03-10T04:19:15.737575+00:00 | 2023-03-10T04:19:15.737607+00:00 | 3,187 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirst we sort the vector then we return the product of first 2 element and last element or product of last three element\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)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& arr) {\n int n=arr.size();\n \tsort(arr.begin(),arr.end());\n \n \treturn max(arr[0]*arr[1]*arr[n-1],arr[n-1]*arr[n-2]*arr[n-3]);\n \n }\n};\n``` | 4 | 0 | ['C++'] | 1 |
maximum-product-of-three-numbers | Java Easy using sorting | java-easy-using-sorting-by-shailendranir-uppq | 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 | shailendraniranjan771 | NORMAL | 2023-02-20T15:17:10.420247+00:00 | 2023-02-20T15:17:10.420295+00:00 | 398 | 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)$$ -->\n\n# Code\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n //One of the Three Numbers is the maximum value in the array.\n\n int a = nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3];\n int b = nums[0] * nums[1] * nums[nums.length - 1];\n return a > b ? a : b;\n }\n}\n``` | 4 | 0 | ['Java'] | 1 |
maximum-product-of-three-numbers | Maximum Product of 3 Numbers - O(n) Time Complexity | maximum-product-of-3-numbers-on-time-com-mjff | Intuition\nTo reduce the time complexity\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n- There are two approaches Brute Force App | Vanshika78 | NORMAL | 2023-02-02T20:44:22.105674+00:00 | 2023-02-02T20:44:22.105715+00:00 | 1,536 | false | # Intuition\nTo reduce the time complexity\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n- There are two approaches Brute Force Approach and the other one is the refined one that is using sorting.\n- I have used sorting approach to reduce the time complexity. \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. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) \n{\n int n = nums.size();\n int mx[3] = {-1001,-1001,-1001};\n int mn[2] = {1001,1001};\n int first, sec;\n for (int i = 0;i<n;i++)\n {\n if (nums[i] >= mx[0])\n {\n first = mx[0];\n sec = mx[1];\n mx[0]= nums[i];\n mx[1]=first;\n mx[2]=sec;\n }\n else if (nums[i] >= mx[1])\n {\n first = mx[1];\n mx[1]= nums[i];\n mx[2]=first;\n \n }\n else if (nums[i]> mx[2])\n {\n mx[2] = nums[i];\n }\n if (nums[i]<=mn[0])\n {\n first = mn[0];\n mn[0]=nums[i];\n mn[1]=first;\n }\n else if(nums[i]<=mn[1])\n {\n mn[1]=nums[i];\n }\n }\n return max(mn[0]*mn[1]*mx[0],mx[0]*mx[1]*mx[2]);\n\n\n}\nint main(){\n return 0;\n}\n\n \n \n};\n\n\n \n``` | 4 | 0 | ['Array', 'Dynamic Programming', 'Sorting', 'C++'] | 0 |
maximum-product-of-three-numbers | Java Simple Solution, using Sorting | java-simple-solution-using-sorting-by-sh-01qh | Intuition\n Describe your first thoughts on how to solve this problem. \nIntuition was to sort the array and get the product\n\n# Approach\n Describe your appro | ShivaniSinghh | NORMAL | 2022-12-01T23:04:28.302165+00:00 | 2022-12-01T23:04:28.302201+00:00 | 977 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIntuition was to sort the array and get the product\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nI used sorting because we know that after sorting largest 3 elements will have maximum product or 2 leftmost elements when multiplied with rightmost element will give maximum product. After finding left and right product, we use Math.max function to get maximum of these 2.\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)$$ -->\n\n# Code\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n int n = nums.length;\n Arrays.sort(nums);\n int r = nums[n-1] * nums[n-2] * nums[n-3]; //to check right subarray\n int l = nums[0] * nums[1] * nums[n-1]; //to check left subarray\n return Math.max(l,r);\n }\n}\n``` | 4 | 0 | ['Java'] | 0 |
maximum-product-of-three-numbers | easiest c++ soution | easiest-c-soution-by-anish25-d08e | class Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n sort(nums.rbegin(),nums.rend());\n int l=nums.size();\n if(nums[0]*n | Anish25 | NORMAL | 2022-11-21T07:52:10.187334+00:00 | 2022-11-21T07:52:10.187363+00:00 | 720 | false | ```class Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n sort(nums.rbegin(),nums.rend());\n int l=nums.size();\n if(nums[0]*nums[1]*nums[2]>nums[0]*nums[l-1]*nums[l-2]){\n return nums[0]*nums[1]*nums[2];\n }\n else{\n return nums[0]*nums[l-1]*nums[l-2];\n }\n }\n};``` | 4 | 0 | [] | 1 |
maximum-product-of-three-numbers | python||233ms||simplest approach | python233mssimplest-approach-by-2stellon-4ocz | \t\tclass Solution:\n\t\t\tdef maximumProduct(self, a: List[int]) -> int:\n\t\t\t\ta.sort(reverse=True)\n\t\t\t\tv1=a[0]a[1]a[2]\n\t\t\t\tv2=a[0]a[-1]a[-2]\n\t\ | 2stellon7 | NORMAL | 2022-10-01T05:14:22.348948+00:00 | 2022-10-01T05:14:22.348982+00:00 | 1,316 | false | \t\tclass Solution:\n\t\t\tdef maximumProduct(self, a: List[int]) -> int:\n\t\t\t\ta.sort(reverse=True)\n\t\t\t\tv1=a[0]*a[1]*a[2]\n\t\t\t\tv2=a[0]*a[-1]*a[-2]\n\t\t\t\treturn max(v1,v2)\n | 4 | 0 | ['Python'] | 2 |
maximum-product-of-three-numbers | 100% easy || Maximum Product of Three Numbers || C++ Solution | 100-easy-maximum-product-of-three-number-13x3 | \nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int a=nums[nums.size()-1]*nums[nums.siz | StArK19 | NORMAL | 2022-08-25T21:16:52.734466+00:00 | 2022-08-25T21:16:52.734514+00:00 | 958 | false | ```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n sort(nums.begin(),nums.end());\n int a=nums[nums.size()-1]*nums[nums.size()-2]*nums[nums.size()-3];\n int b=nums[0]*nums[1]*nums[nums.size()-1];\n return max(a,b);\n }\n \n};\n``` | 4 | 0 | ['C', 'C++'] | 0 |
maximum-product-of-three-numbers | C++ simplest, O(N), easy understand, no sorting | c-simplest-on-easy-understand-no-sorting-8hz4 | \n//We should keep 3 biggest and 2 smallest elements of the array\n//max product is either product of 3 bigger or 2 smallest(negatives) and the biggest\nclass S | Moldoteck | NORMAL | 2022-05-12T08:46:14.916492+00:00 | 2022-05-12T08:46:14.916518+00:00 | 268 | false | ```\n//We should keep 3 biggest and 2 smallest elements of the array\n//max product is either product of 3 bigger or 2 smallest(negatives) and the biggest\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int max1=INT_MIN, max2=INT_MIN, max3=INT_MIN;\n int min1=INT_MAX, min2=INT_MAX;\n \n for(int i=0;i<nums.size();++i){\n if(nums[i]>=max3){\n\t\t\t //max3 should always be the biggest\n\t\t\t\t// >= used because max2 can be equal to max3\n max1=max2;\n max2=max3;\n max3=nums[i];\n } else if(nums[i]>=max2){\n\t\t\t //max2 should be second biggest\n\t\t\t\t//if current number is smaller compared to max3 but bigger than max2\n max1=max2;\n max2=nums[i];\n } else if(nums[i]>max1){\n max1=nums[i];\n }\n \n if(nums[i]<=min1){\n\t\t\t //same ideas, applied for minimum\n\t\t\t\t//min1 is the smallest, min2 - second smallest\n min2=min1;\n min1=nums[i];\n } else if(nums[i]<min2){\n min2=nums[i];\n }\n }\n return max(max1*max2*max3, min1*min2*max3);\n }\n};\n``` | 4 | 0 | [] | 0 |
maximum-product-of-three-numbers | C++ || 100% faster O(n) without sorting || 2 solutions ✔ | c-100-faster-on-without-sorting-2-soluti-8bbi | Logic :\nMax product of three no. come from\n\t\t1. product of 3 maximum positive no.\n\t\t2. product of 2 minimum negative no. and 1 maximum positive no.\n\nSo | AJAY_MAKVANA | NORMAL | 2021-07-30T09:30:30.233970+00:00 | 2021-08-12T15:29:42.824781+00:00 | 243 | false | ### **Logic :**\n**Max product of three no. come from**\n\t\t**1. product of `3 maximum positive no.`**\n\t\t**2. product of `2 minimum negative no.` and `1 maximum positive no.`**\n\n*So bellow **two** solution is based on this logic :)*\n\n### **1. Sort based solution**\n**TC = O(nlogn)\nSC = O(1)**\n```\nclass Solution {\npublic:\n\tint maximumProduct(vector<int>&a) {\n\t\tsort(a.begin(), a.end());\n\t\tint n = a.size();\n\t\treturn max((a[n - 1] * a[n - 2] * a[n - 3]), (a[0] * a[1] * a[n - 1]));\n\t}\n};\n```\n\n**But Wait Can we do it in better than `O(nlogn)` ? `YES` \uD83D\uDE03**\n\n****\n### **2. Iterative Solution**\nIn this solution I store all required value in variable because don\'t want full sorted array for find answer but we only need 5 value `max1`, `max2`, `max3`, `min1` ans `min2` so we calculate that value by iteration\n**TC = O(n)\nSC = O(1)**\n```\nclass Solution {\npublic:\n\tint maximumProduct(vector<int>&a) {\n\t\tint max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN;\n\t\tint min1 = INT_MAX, min2 = INT_MAX;\n\t\tint n = a.size();\n\t\tfor (int i = 0; i < n; i++)\n\t\t{\n\t\t\tif (a[i] > max1)\n\t\t\t{\n\t\t\t\tmax3 = max2;\n\t\t\t\tmax2 = max1;\n\t\t\t\tmax1 = a[i];\n\t\t\t}\n\t\t\telse if (a[i] > max2)\n\t\t\t{\n\t\t\t\tmax3 = max2;\n\t\t\t\tmax2 = a[i];\n\t\t\t}\n\t\t\telse if (a[i] > max3)\n\t\t\t{\n\t\t\t\tmax3 = a[i];\n\t\t\t}\n\n\t\t\tif (a[i] < min1)\n\t\t\t{\n\t\t\t\tmin2 = min1;\n\t\t\t\tmin1 = a[i];\n\t\t\t}\n\t\t\telse if (a[i] < min2)\n\t\t\t{\n\t\t\t\tmin2 = a[i];\n\t\t\t}\n\t\t}\n\t\treturn max((max1 * max2 * max3), (min1 * min2 * max1));\n\t}\n};\n```\n**If find helpful please upvote it \u2714** | 4 | 0 | [] | 1 |
maximum-product-of-three-numbers | Javascript Solution With Explanation | javascript-solution-with-explanation-by-pi24l | Explanation :-\nExample : - [-4 ,-3, -2, -1, 60]\nWe first sort the array in increasing order\nIf the above example had all positive integers, we would have sim | gaurang9 | NORMAL | 2020-09-25T17:54:37.471172+00:00 | 2020-09-25T17:54:37.471218+00:00 | 451 | false | Explanation :-\nExample : - [-4 ,-3, -2, -1, 60]\nWe first sort the array in increasing order\nIf the above example had all positive integers, we would have simply done the product of 3 largest numbers.\nBut since it also has negative numbers , and we know negative multiplied by negative is positive , we can consider the first 2 negative numbers and the last number(which is the greatest)\n\nIf we choose 3 largest number --> 60 x -1 x-2 = 120\nIf we choose first 2 numbers and the last(largest) --> -4 x -3 x 60 = 720\nMath.max(120,720) ---->720\n\n```\nvar maximumProduct = function(nums) {\n let max = nums.sort((a, b) => a - b)\n let product1 = max[0] * max[1] * max[nums.length - 1]\n let product2 = max[nums.length - 1] * max[nums.length - 2] * max[nums.length - 3]\n return Math.max(product1, product2)\n};\n\n | 4 | 0 | ['JavaScript'] | 2 |
maximum-product-of-three-numbers | Easy JS Solution | easy-js-solution-by-hbjorbj-d8hq | \n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n let sorted = nums.sort((a,b) => a-b), len = nums.length;\ | hbjorbj | NORMAL | 2020-06-22T09:46:40.807051+00:00 | 2020-06-22T09:46:40.807084+00:00 | 555 | false | ```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n let sorted = nums.sort((a,b) => a-b), len = nums.length;\n let res1 = sorted[0]*sorted[1]*sorted[len-1],\n res2 = sorted[len-1]*sorted[len-2]*sorted[len-3]\n return Math.max(res1,res2);\n}; \n``` | 4 | 0 | ['JavaScript'] | 0 |
maximum-product-of-three-numbers | C# solutions w/ explaination | c-solutions-w-explaination-by-newbiecode-sno3 | There are 3 cases of the input array. And the conclusion is the maximun product is generated by either the 3 largest number or the largest number and 2 smallest | newbiecoder1 | NORMAL | 2020-03-27T20:10:42.685673+00:00 | 2020-04-23T04:17:12.596244+00:00 | 254 | false | There are 3 cases of the input array. And the conclusion is **the maximun product is generated by either the 3 largest number or the largest number and 2 smallest numbers.**\n1. All numbers are negative.\n\tmax product is generated by **the 3 largest negative numbers**.\n\te.g. [-4,-3,-2,-1]. max product = -3 * -2 * -1.\n2. All the numbers are positive.\n max product is generated by the **3 largest positive numbers**.\n\te.g., [1,2,3,4,5]. max product = 3 * 4 * 5\n3. Array has mixed positive numbers and negative numbers\n (a) Array has 1 positive number.\n\t\tmax product is generated by **the largest positive number and 2 smallest negative numbers**.\n\t\te.g. [-4, -3, -2, 1]. max product = 1 * -4 * -3\n\t(b) Array has 2 positive numbers.\n\t\tmax product is generated by **the largest positive number and 2 smallest negative numbers**.\n\t\te.g., [-4, -3, -2, 1, 2]. max product = 2 * -4 * -3\n\t(c) Array has 3 or more than 3 positive numbers.\n\t\tmax product is generated by the **3 largest positive numbers**, or by **the largest postive number and 2 smallest negative numbers**.\n\t\te.g., [1,2,3,-4]. max product = 1 * 2 * 3\n\t\te.g., [1,2,3,4,-4]. max product = 2 * 3 * 4\n\t\te.g., [1,2,3,-4,-5]. max product = 3 * -4 * -5\n\t\n**One pass solution O(n)**\n```\npublic class Solution {\n public int MaximumProduct(int[] nums) {\n \n int max1 = Int32.MinValue, max2 = Int32.MinValue, max3 = Int32.MinValue, min1 = Int32.MaxValue, min2 = Int32.MaxValue;\n \n foreach(int num in nums)\n {\n // find max1, max2 and max3\n if(num >= max1)\n {\n max3 = max2;\n max2 = max1;\n max1 = num;\n }\n else if(num >= max2)\n {\n max3 = max2;\n max2 = num;\n }\n else if(num > max3)\n {\n max3 = num;\n }\n \n // find min1 and min2\n if(num <= min1)\n {\n min2 = min1;\n min1 = num;\n }\n else if(num < min2)\n {\n min2 = num;\n }\n }\n \n return Math.Max(max1 * max2 * max3, min1 * min2 * max1);\n }\n}\n```\n\n**Sort solution O(nlogn)**\n```\n public int MaximumProduct(int[] nums) {\n \n if(nums == null || nums.Length < 3)\n return 0;\n \n Array.Sort(nums);\n \n return Math.Max(nums[nums.Length - 1] * nums[nums.Length - 2] * nums[nums.Length - 3], nums[nums.Length - 1] * nums[0] * nums[1]);\n }\n``` | 4 | 0 | [] | 0 |
maximum-product-of-three-numbers | Javascript - O(n) time & O(1) space | javascript-on-time-o1-space-by-achandel-cz9d | \nvar maximumProduct = function(nums) {\n // get top 3 max and top 2 min in one pass\n let max1 = -Infinity, \n max2 = -Infinity,\n max3 = - | achandel | NORMAL | 2019-11-28T14:00:30.094690+00:00 | 2019-11-28T14:00:30.094741+00:00 | 806 | false | ```\nvar maximumProduct = function(nums) {\n // get top 3 max and top 2 min in one pass\n let max1 = -Infinity, \n max2 = -Infinity,\n max3 = -Infinity,\n min1 = Infinity,\n min2 = Infinity;\n \n for(let num of nums){\n // fetch max\n if(num > max1){\n max3 = max2;\n max2 = max1;\n max1 = num;\n } else if(num > max2){\n max3 = max2;\n max2 = num;\n } else if(num > max3){\n max3 = num;\n }\n \n // fetch min\n if(num < min1){\n min2 = min1;\n min1 = num\n } else if(num < min2){\n min2 = num;\n }\n }\n // return max of top 3, or top1 and last 2\n return Math.max(max1 * max2 * max3, max1 * min1 * min2);\n \n};\n``` | 4 | 0 | ['JavaScript'] | 0 |
maximum-product-of-three-numbers | Share my Python solution | share-my-python-solution-by-wangruinju-68tv | Sort the array, the maximum product happens in two situations:\nthe last three or the first two with the last one.\nHere is the Python code!\n\nclass Solution(o | wangruinju | NORMAL | 2017-06-25T18:49:38.269000+00:00 | 2017-06-25T18:49:38.269000+00:00 | 1,259 | false | Sort the array, the maximum product happens in two situations:\nthe last three or the first two with the last one.\nHere is the Python code!\n```\nclass Solution(object):\n def maximumProduct(self, nums):\n """\n :type nums: List[int]\n :rtype: int\n """\n nums.sort()\n return max(nums[-1]*nums[-2]*nums[-3], nums[0]*nums[1]*nums[-1])\n``` | 4 | 0 | [] | 2 |
maximum-product-of-three-numbers | Very simple Javascript and typescript solution. Do check it out👍 | very-simple-javascript-and-typescript-so-qjqf | Intuition\nThe maximum product can either come from the three largest numbers or from two smallest negative numbers and the largest positive number.\n\n# Approa | meathirarosejohn | NORMAL | 2024-10-20T05:40:06.575578+00:00 | 2024-10-20T05:41:36.507311+00:00 | 281 | false | # Intuition\nThe maximum product can either come from the three largest numbers or from two smallest negative numbers and the largest positive number.\n\n# Approach\nTrack the three largest and two smallest numbers while iterating through the array. The result is the maximum product of either the three largest numbers or two smallest with the largest.\n\n# Complexity\n- Time complexity:\nO(n) - Single pass through the array\n\n- Space complexity:\nO(1) - Only constant space is used.\n\n# Code\n```typescript []\nfunction maximumProduct(nums: number[]): number {\n let largest: number = -Infinity, second: number = -Infinity, third: number = -Infinity;\n let smallest: number = Infinity, secondSmallest: number = Infinity;\n \n for (let i = 0; i < nums.length; i++) {\n if (nums[i] > largest) {\n third = second;\n second = largest;\n largest = nums[i];\n } else if (nums[i] > second) {\n third = second;\n second = nums[i];\n } else if (nums[i] > third) {\n third = nums[i];\n }\n\n if (nums[i] < smallest) {\n secondSmallest = smallest;\n smallest = nums[i];\n } else if (nums[i] < secondSmallest) {\n secondSmallest = nums[i];\n }\n }\n\n return Math.max(largest * second * third, largest * smallest * secondSmallest);\n};\n```\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maximumProduct = function(nums) {\n let largest = -Infinity, second = -Infinity, third = -Infinity;\n let smallest = Infinity, secondSmallest = Infinity;\n \n for(let i = 0; i < nums.length; i++) {\n if (nums[i] > largest) {\n third = second;\n second = largest;\n largest = nums[i];\n } else if (nums[i] > second) {\n third = second;\n second = nums[i];\n } else if (nums[i] > third) {\n third = nums[i];\n }\n\n if (nums[i] < smallest) {\n secondSmallest = smallest;\n smallest = nums[i];\n } else if (nums[i] < secondSmallest) {\n secondSmallest = nums[i];\n }\n }\n\n return Math.max(largest * second * third, largest * smallest * secondSmallest);\n};\n```\n | 3 | 0 | ['Array', 'Math', 'TypeScript', 'JavaScript'] | 1 |
maximum-product-of-three-numbers | 🔍Very Easy, Simple and Short Solution🚀 | very-easy-simple-and-short-solution-by-s-4l1k | Intuition\nThe key intuition is that the maximum product can arise from:\n\nThree largest positive numbers.\nTwo smallest negative numbers and the largest posit | shoaibkhalid65 | NORMAL | 2024-09-10T00:03:15.839947+00:00 | 2024-09-10T00:03:15.839975+00:00 | 731 | false | # Intuition\nThe key intuition is that the maximum product can arise from:\n\nThree largest positive numbers.\nTwo smallest negative numbers and the largest positive number.\n\n# Approach\nSort the array: Sort the array in ascending order.\n\nCalculate products:\n\nCalculate the product of the three largest elements.\nCalculate the product of the two smallest elements and the largest element.\nReturn the maximum: Return the larger of these two products.\n\n# Complexity\n- Time complexity:\nThe time complexity is dominated by the sorting operation, which is O(n log n) using efficient sorting algorithms like Merge Sort or Quick Sort.\n\n- Space complexity:\nThe space complexity is O(1) if the sorting algorithm used is in-place (like Heap Sort).\nThe space complexity is O(n) if the sorting algorithm uses additional memory (like Merge Sort).\n\n# Code\n```java []\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int n=nums.length;\n int a=nums[n-1]*nums[n-2]*nums[n-3];\n int b=nums[0]*nums[1]*nums[n-1];\n return Math.max(a,b);\n }\n}\n``` | 3 | 0 | ['Java'] | 2 |
maximum-product-of-three-numbers | Python Easy Solution || Sorting | python-easy-solution-sorting-by-sumedh07-ojge | Code\n\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n n=len(nums)\n return max(nums[n-1]*nums[n-2] | Sumedh0706 | NORMAL | 2023-07-17T05:45:05.030156+00:00 | 2023-07-17T05:45:05.030182+00:00 | 858 | false | # Code\n```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n n=len(nums)\n return max(nums[n-1]*nums[n-2]*nums[n-3],nums[0]*nums[1]*nums[n-1])\n \n``` | 3 | 0 | ['Sorting', 'Python3'] | 0 |
maximum-product-of-three-numbers | Super Logical Solution Using Java | super-logical-solution-using-java-by-gpr-333c | \n\n# Superb Solution using JAVA\n\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int n=nums.length;\n ret | GPraveen | NORMAL | 2023-05-08T03:07:48.487576+00:00 | 2023-05-08T03:07:48.487621+00:00 | 1,834 | false | \n\n# Superb Solution using JAVA\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n int n=nums.length;\n return Math.max(nums[0]*nums[1]*nums[n-1],nums[n-1]*nums[n-2]*nums[n-3]); \n }\n}\n``` | 3 | 0 | ['Java'] | 3 |
maximum-product-of-three-numbers | C++ Solution || Easy || Brute Force ✔✔ | c-solution-easy-brute-force-by-akanksha9-fi4f | Complexity\n- Time complexity: O(nlogn)\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 | akanksha984 | NORMAL | 2023-05-06T11:27:53.014835+00:00 | 2023-05-13T05:21:43.459184+00:00 | 2,274 | false | # Complexity\n- Time complexity: $$O(nlogn)$$\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 maximumProduct(vector<int>& nums) {\n int n= nums.size();\n sort(nums.begin(),nums.end());\n int ans1= nums[n-1]*nums[n-2]*nums[n-3];\n int ans2= nums[0]*nums[1]*nums[n-1];\n return max(ans1,ans2);\n }\n};\n``` | 3 | 0 | ['Array', 'Math', 'Sorting', 'C++'] | 2 |
maximum-product-of-three-numbers | Solution | solution-by-deleted_user-jqed | C++ []\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int max1 = INT32_MIN, max2 = INT32_MIN, max3 = INT32_MIN;\n int m | deleted_user | NORMAL | 2023-04-14T13:16:33.554001+00:00 | 2023-04-14T14:34:13.241539+00:00 | 4,005 | false | ```C++ []\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int max1 = INT32_MIN, max2 = INT32_MIN, max3 = INT32_MIN;\n int min1 = INT32_MAX, min2 = INT32_MAX;\n for(int& num : nums) {\n if(num > max1) {\n max3 = max2, max2 = max1, max1 = num;\n }\n else if(num > max2) {\n max3 = max2, max2 = num;\n }\n else if(num > max3) {\n max3 = num;\n }\n if(num < min1) {\n min2 = min1, min1 = num;\n }\n else if(num < min2) {\n min2 = num;\n }\n }\n return std::max(max1*max2*max3, max1*min1*min2);\n }\n};\n```\n\n```Python3 []\nclass Solution:\n def maximumProduct(self, nums):\n a, b = heapq.nlargest(3, nums), heapq.nsmallest(2, nums)\n return max(a[0] * a[1] * a[2], b[0] * b[1] * a[0])\n```\n\n```Java []\nclass Solution {\n int maxi(int[] a,int n)\n {\n int max=(int)-9999999;\n int f=-1;\n for(int i=0;i<n;i++)\n {\n if(max<=a[i])\n {\n max=a[i];\n f=i;\n }\n }\n a[f]=(int)-9999999;\n return max;\n }\n int min(int[] a,int n)\n {\n int max=(int)999999;\n int f=-1;\n for(int i=0;i<n;i++)\n {\n if(a[i]!=-9999999)\n {\n if(max>=a[i])\n {\n max=a[i];\n f=i;\n }\n }\n }\n a[f]=(int)999999;\n return max;\n }\n public int maximumProduct(int[] a) {\n int n=a.length;\n int aa=maxi(a,n);\n int b=maxi(a,n);\n int c=maxi(a,n);\n if(n==3)\n {\n return aa*b*c;\n }\n int x=min(a,n);\n int y=min(a,n);\n if(n==4)\n {\n y=c;\n }\n return Math.max(aa*b*c,aa*x*y);\n }\n}\n```\n | 3 | 0 | ['C++', 'Java', 'Python3'] | 0 |
maximum-product-of-three-numbers | Simple solution in python using sort | simple-solution-in-python-using-sort-by-bgfzt | \n\n# Code\n\nclass Solution:\n def maximumProduct(self, A: List[int]) -> int:\n A.sort()\n return max(A[0]*A[1]*A[-1], A[-1]*A[-2]*A[-3])\n | tryingall | NORMAL | 2023-03-30T07:23:34.055965+00:00 | 2023-03-30T07:23:34.055997+00:00 | 1,082 | false | \n\n# Code\n```\nclass Solution:\n def maximumProduct(self, A: List[int]) -> int:\n A.sort()\n return max(A[0]*A[1]*A[-1], A[-1]*A[-2]*A[-3])\n``` | 3 | 0 | ['Python3'] | 1 |
maximum-product-of-three-numbers | Brute Solution | brute-solution-by-anurag_rathore-rfpp | 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 | Anurag_Rathore | NORMAL | 2023-03-01T11:52:47.228429+00:00 | 2023-03-01T11:52:47.228470+00:00 | 1,287 | 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)$$ -->\n\n# Code\n```\nclass Solution {\n public int maximumProduct(int[] nums) {\n Arrays.sort(nums);\n return Math.max(nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3],nums[nums.length-1]*nums[0]*nums[1]);\n }\n}\n``` | 3 | 0 | ['Java'] | 0 |
maximum-product-of-three-numbers | python 2 liner code faster | python-2-liner-code-faster-by-raghunath_-c3yc | \n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n return max((nums[-1]*nums[-2]*nums[-3]),(nums[0]*nums[1]*nums[-1]))\n | Raghunath_Reddy | NORMAL | 2022-09-27T11:39:26.624394+00:00 | 2022-09-27T11:39:26.624436+00:00 | 1,421 | false | ```\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n return max((nums[-1]*nums[-2]*nums[-3]),(nums[0]*nums[1]*nums[-1]))\n``` | 3 | 0 | ['Python', 'Python3'] | 1 |
maximum-product-of-three-numbers | JS simple solution with sorting | js-simple-solution-with-sorting-by-kunkk-lexv | \nvar maximumProduct = function(nums) {\n nums = nums.sort((a,b) => a - b);\n \n return Math.max(nums[0] * nums[1] * nums[nums.length - 1], nums[nums.l | kunkka1996 | NORMAL | 2022-08-26T10:05:57.751759+00:00 | 2022-08-26T10:05:57.751794+00:00 | 624 | false | ```\nvar maximumProduct = function(nums) {\n nums = nums.sort((a,b) => a - b);\n \n return Math.max(nums[0] * nums[1] * nums[nums.length - 1], nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3]);\n};\n``` | 3 | 0 | ['Sorting', 'JavaScript'] | 1 |
maximum-product-of-three-numbers | Python - Easy to understand on the basis of testcases!! | python-easy-to-understand-on-the-basis-o-w2u1 | \ndef maximumProduct(self, nums):\n n = len(nums)\n nums.sort()\n return max(nums[0] * nums[1] * nums[n - 1], nums[n-3] * nums[n - 2] * num | kartikkulshreshtha | NORMAL | 2022-08-23T06:33:49.712253+00:00 | 2022-08-23T06:33:49.712299+00:00 | 1,084 | false | ```\ndef maximumProduct(self, nums):\n n = len(nums)\n nums.sort()\n return max(nums[0] * nums[1] * nums[n - 1], nums[n-3] * nums[n - 2] * nums[n-1])\n``` | 3 | 0 | ['Python'] | 0 |
maximum-product-of-three-numbers | my python solution with clear explanation | my-python-solution-with-clear-explanatio-nznm | \nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n max1 = nums[-1]*nums[-2]*nums[-3]\n max2 = nums[0] | CodeHunter_Sankar | NORMAL | 2022-07-08T12:36:06.992255+00:00 | 2022-07-08T12:36:06.992302+00:00 | 278 | false | ```\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n max1 = nums[-1]*nums[-2]*nums[-3]\n max2 = nums[0]*nums[1]*nums[-1]\n if max1 >= max2:\n return max1\n else:\n return max2\n""""\nexplanation: example \n list1=[1,2,3,4,5]\n list2=[-100,-98,-1,2,3,4]\n \n >first sort the list so highest values goes right and then multiplay last 3 values this works for list1. but this leads wrong in some cases see list2\n \n > so generally if you multiplay 3 negative number we get negative value but in case of two values we get positive so here we used same approach.\n \n >we need to calculate for 2 maximum values max1 and max2 \n \n >max1 case is used for all positive numbers like list1\n \n >to calculate max1 we just multiply the last 3 elements in the list list1=[3*4*5] =60, list2=[2*3*4]=24\n \n >now we calculate max2 .this is used for some negative values in the list or all negative values in the list\n \n >while calculate max2 we multiplay first 2 values because multiplying 2 values will get positive value list2=[-100*-98] = 9800\n \n >now we need to multiplay the 9800 with highest value in the list2 so we will get highest value that 9800*4 =39200\n \n >now we have 2 maximum values max1 and max2\n \n >compare both values and return maximum value """\n \n \n \n \n``` | 3 | 0 | ['Python'] | 0 |
maximum-product-of-three-numbers | Maximum product of three Numbers || simple || efficient || sorting | maximum-product-of-three-numbers-simple-cfm1n | \nclass Solution {\npublic:\n \n static int cmp(int a,int b) // This cmp function to sort in descending order\n { // To wri | babasaheb256 | NORMAL | 2022-06-10T03:52:28.894480+00:00 | 2022-06-10T03:54:41.336600+00:00 | 891 | false | ```\nclass Solution {\npublic:\n \n static int cmp(int a,int b) // This cmp function to sort in descending order\n { // To write this type of function concept is simple\n \n\t\treturn a>b; // Try to think what you want like you basically \n // want a>b i.e first no > second no. (both no are integer) though given \n\t\t\t\t\t\t\t\t // input was vector\n }\n \n int maximumProduct(vector<int>& nums) {\n \n sort(nums.begin(),nums.end(),cmp);\n \n int positive= nums[0]*nums[1]*nums[2], \n negative= nums[nums.size()-1]*nums[nums.size()-2]*nums[0]; // if array contents negative no as well.\n \n return positive>negative? positive : negative; \n }\n};\n```\n\n\nPlease upvote !!!!!\n(Top-left button)\n | 3 | 0 | ['C', 'Sorting', 'C++'] | 2 |
maximum-product-of-three-numbers | Two lines of Code || Java || C++ | two-lines-of-code-java-c-by-chiragvohra-p8kn | class Solution {\n public int maximumProduct(int[] nums) {\n\t\n\t//As we know there is only two possibilties \n\t//1st possibilty: All numbers are positive | chiragvohra | NORMAL | 2022-05-13T21:39:35.001248+00:00 | 2022-05-13T21:39:35.001274+00:00 | 183 | false | class Solution {\n public int maximumProduct(int[] nums) {\n\t\n\t//As we know there is only two possibilties \n\t//1st possibilty: All numbers are positive in that case we have return product of last three number of sorted array.\n \t// 2nd possibilty : There may be negative numbers also in that case we have to return product of two most negative number and one maximum possitive number\n\t\n Arrays.sort(nums);\n \n return Math.max( (nums[0]*nums[1]*nums[nums.length-1]),(nums[nums.length-1]*nums[nums.length-2]*nums[nums.length-3] ));\n }\n } \n # Press the above button if you like it\n | 3 | 0 | ['Array', 'Sorting'] | 0 |
maximum-product-of-three-numbers | c++ || max product of 3 no.|| Basic || n* logn | c-max-product-of-3-no-basic-n-logn-by-sh-uw08 | Time complexity= n*log(n)\nn=size of given vector\n\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int ans=INT_MIN,n=nums.size | sheetaljoshi | NORMAL | 2021-12-17T11:09:44.143630+00:00 | 2021-12-17T11:16:43.525334+00:00 | 352 | false | Time complexity= n*log(n)\nn=size of given vector\n```\nclass Solution {\npublic:\n int maximumProduct(vector<int>& nums) {\n int ans=INT_MIN,n=nums.size();\n sort(nums.begin(),nums.end());\n \n int x=nums[n-3];\n int y=nums[n-2];\n int z=nums[n-1];\n int a=nums[0];\n int b=nums[1];\n \n \n ans=max(ans,x*y*z); // when whole array is -ve or +ve\n ans=max(ans,a*b*z);\n \n return ans;\n \n }\n};\n``` | 3 | 1 | ['C', 'C++'] | 1 |
maximum-product-of-three-numbers | python3 | python3-by-saurabht462-971z | ```\nimport math\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n if len(nums)==3:return math.prod(nums)\n nums.sort(reve | saurabht462 | NORMAL | 2021-04-16T14:17:45.878546+00:00 | 2021-04-16T14:17:45.878583+00:00 | 159 | false | ```\nimport math\nclass Solution:\n def maximumProduct(self, nums: List[int]) -> int:\n if len(nums)==3:return math.prod(nums)\n nums.sort(reverse=True)\n print(nums)\n return max(math.prod(nums[:3]),math.prod([nums[-1],nums[-2],nums[0]])) | 3 | 0 | [] | 0 |
maximum-product-of-three-numbers | python || very simple solution | python-very-simple-solution-by-risky_cod-5qm0 | \n# Written by : Dhruv Vavliya\nnums = [1,2,3,4]\n\ndef max_product(nums):\n nums.sort(reverse=True)\n a = nums[0]*nums[1]*nums[2]\n b = nums[0]*nums[- | risky_coder | NORMAL | 2020-12-29T11:36:07.300888+00:00 | 2020-12-29T11:36:07.300930+00:00 | 727 | false | ```\n# Written by : Dhruv Vavliya\nnums = [1,2,3,4]\n\ndef max_product(nums):\n nums.sort(reverse=True)\n a = nums[0]*nums[1]*nums[2]\n b = nums[0]*nums[-1]*nums[-2]\n return max(a,b)\n\nprint(max_product(nums))\n``` | 3 | 0 | ['Math', 'Python', 'Python3'] | 1 |
maximum-product-of-three-numbers | 628-Maximum Product of Three Numbers-Py All-in-One By Talse | 628-maximum-product-of-three-numbers-py-a00z2 | Get it Done, Make it Better, Share the Best -- Talse\nI). Naive Sort\n| O(T): O(nlgn) | O(S): O(1) | Rt: 284ms | \npython\n def maximumProduct(self, nums: Li | talse | NORMAL | 2019-12-03T18:59:47.272360+00:00 | 2019-12-03T19:00:11.348926+00:00 | 177 | false | **Get it Done, Make it Better, Share the Best -- Talse**\n**I). Naive Sort**\n| O(T): O(nlgn) | O(S): O(1) | Rt: 284ms | \n```python\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1])\n```\n\n\n**II). Limit State**\n| O(T): O(n) | O(S): O(1) | Rt: 284ms | \n```python\n def maximumProduct(self, nums: List[int]) -> int:\n max1 = max2 = max3 = -float(\'inf\')\n min1 = min2 = float(\'inf\')\n for i in nums:\n if i > max1: max1, max2, max3 = i, max1, max2\n elif i > max2: max2, max3 = i, max2\n elif i > max3: max3 = i\n if i < min1: min1, min2 = i, min1\n elif i < min2: min2 = i\n return max(min1 * min2 * max1, max1 * max2 * max3)\n```\n\n\n**III). Combination**\n| O(T): O(nlgn) | O(S): O(1) | Rt: 276ms | \n```python\n def maximumProduct(self, nums: List[int]) -> int:\n nums.sort()\n del nums[2:-3]\n return max(a * b * c for a, b, c in itertools.combinations(nums, 3))\n```\nReferrence: https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/104779/Python-Straightforward-with-Explanation\n\n\n**IV). Min Heap**\n| O(T): O(nlgn) | O(S): O(1) | Rt: 372ms | \n```python\n def maximumProduct(self, nums: List[int]) -> int:\n ncopy = copy.deepcopy(nums)\n heapq.heapify(ncopy)\n min1, min2 = heapq.heappop(ncopy), heapq.heappop(ncopy)\n \n neg = [-i for i in nums]\n heapq.heapify(neg)\n max1, max2, max3 = -heapq.heappop(neg), -heapq.heappop(neg), -heapq.heappop(neg)\n \n return max(min1 * min2 * max1, max1 * max2 * max3) \n```\nComment: this heap solution heapify all nums therefore is of time complexity O(nlgn). In real case, we don\'t need to heapify all elements. The refined version is provided as follows.\nAlternative: heapq nlargest and nsmallest. | O(T): O(nlgk) | O(S): O(1) | Rt: 276ms | \n```python\n def maximumProduct(self, nums: List[int]) -> int:\n hpl, hps = heapq.nlargest(3, nums), heapq.nsmallest(2, nums)\n return max(hpl[0] * hpl[1] * hpl[2], hps[-1] * hps[-2] * hpl[0])\n```\nReferrence: https://leetcode.com/problems/maximum-product-of-three-numbers/discuss/104739/Python-O(N)-and-1-line\nComment: heap solution is compatible for maximum product of k numbers. \n\n | 3 | 0 | [] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.