Skip to main content
added 6 characters in body
Source Link
mdfst13
  • 22.4k
  • 6
  • 34
  • 70

This task is taken from www.interviewbit.com

Given an array of integers, return the highest product possible by multiplying 3 numbers from the array

Input:

array of integers e.g {1, 2, 3}

Example:

[0, -1, 3, 100, 70, 50]

=> 70*50*100 = 350000

NOTE: Solution will fit in a 32-bit signed integer

My approach: First sort the array, then return the maximum the following numbers: The product of the first two smallest numbers (because they could be negative) and the last number (largest) or the product of the three largest numbers.

My solution ashas a runtime of \$O(nlogn)\$\$O(n \log n)\$ due to the sort and a space complexity of \$O(1)\$. I wonder whether there is a faster solution than \$O(nlogn)\$\$O(n\log n)\$.

function highesProd(a) {
  a.sort((a,b) => a - b);
  return Math.max(a[0] * a[1] * a[a.length - 1], a[a.length - 3] * a[a.length - 2] * a[a.length - 1]);
}

This task is taken from www.interviewbit.com

Given an array of integers, return the highest product possible by multiplying 3 numbers from the array

Input:

array of integers e.g {1, 2, 3}

Example:

[0, -1, 3, 100, 70, 50]

=> 70*50*100 = 350000

NOTE: Solution will fit in a 32-bit signed integer

My approach: First sort the array, then return the maximum the following numbers: The product of the first two smallest numbers (because they could be negative) and the last number (largest) or the product of the three largest numbers.

My solution as a runtime of \$O(nlogn)\$ due to the sort and a space complexity of \$O(1)\$. I wonder whether there is a faster solution than \$O(nlogn)\$.

function highesProd(a) {
  a.sort((a,b) => a - b);
  return Math.max(a[0] * a[1] * a[a.length - 1], a[a.length - 3] * a[a.length - 2] * a[a.length - 1]);
}

This task is taken from www.interviewbit.com

Given an array of integers, return the highest product possible by multiplying 3 numbers from the array

Input:

array of integers e.g {1, 2, 3}

Example:

[0, -1, 3, 100, 70, 50]

=> 70*50*100 = 350000

NOTE: Solution will fit in a 32-bit signed integer

My approach: First sort the array, then return the maximum the following numbers: The product of the first two smallest numbers (because they could be negative) and the last number (largest) or the product of the three largest numbers.

My solution has a runtime of \$O(n \log n)\$ due to the sort and a space complexity of \$O(1)\$. I wonder whether there is a faster solution than \$O(n\log n)\$.

function highesProd(a) {
  a.sort((a,b) => a - b);
  return Math.max(a[0] * a[1] * a[a.length - 1], a[a.length - 3] * a[a.length - 2] * a[a.length - 1]);
}
added 33 characters in body
Source Link
thadeuszlay
  • 4k
  • 28
  • 53

This task is taken from www.interviewbit.com

Given an array of integers, return the highest product possible by multiplying 3 numbers from the array

Input:

array of integers e.g {1, 2, 3}

Example:

[0, -1, 3, 100, 70, 50]

=> 70*50*100 = 350000

NOTE: Solution will fit in a 32-bit signed integer

My approach: First sort the array, then return the maximum the following numbers: The product of the first two smallest numbers (because they could be negative) and the last number (largest) or the product of the three largest numbers.

My solution as a runtime of \$O(nlogn)\$ due to the sort and a space complexity of \$O(1)\$. I wonder whether there is a faster solution than \$O(nlogn)\$.

function highesProd(a) {
  a.sort((a,b) => a - b);
  return Math.max(a[0] * a[1] * a[a.length - 1], a[a.length - 3] * a[a.length - 2] * a[a.length - 1]);
}

This task is taken from www.interviewbit.com

Given an array of integers, return the highest product possible by multiplying 3 numbers from the array

Input:

array of integers e.g {1, 2, 3}

Example:

[0, -1, 3, 100, 70, 50]

=> 70*50*100 = 350000

NOTE: Solution will fit in a 32-bit signed integer

My approach: First sort the array, then return the maximum the following numbers: The product of the first two smallest numbers and the last number (largest) or the product of the three largest numbers.

My solution as a runtime of \$O(nlogn)\$ due to the sort and a space complexity of \$O(1)\$. I wonder whether there is a faster solution than \$O(nlogn)\$.

function highesProd(a) {
  a.sort((a,b) => a - b);
  return Math.max(a[0] * a[1] * a[a.length - 1], a[a.length - 3] * a[a.length - 2] * a[a.length - 1]);
}

This task is taken from www.interviewbit.com

Given an array of integers, return the highest product possible by multiplying 3 numbers from the array

Input:

array of integers e.g {1, 2, 3}

Example:

[0, -1, 3, 100, 70, 50]

=> 70*50*100 = 350000

NOTE: Solution will fit in a 32-bit signed integer

My approach: First sort the array, then return the maximum the following numbers: The product of the first two smallest numbers (because they could be negative) and the last number (largest) or the product of the three largest numbers.

My solution as a runtime of \$O(nlogn)\$ due to the sort and a space complexity of \$O(1)\$. I wonder whether there is a faster solution than \$O(nlogn)\$.

function highesProd(a) {
  a.sort((a,b) => a - b);
  return Math.max(a[0] * a[1] * a[a.length - 1], a[a.length - 3] * a[a.length - 2] * a[a.length - 1]);
}
Source Link
thadeuszlay
  • 4k
  • 28
  • 53

Find the largest product of an array

This task is taken from www.interviewbit.com

Given an array of integers, return the highest product possible by multiplying 3 numbers from the array

Input:

array of integers e.g {1, 2, 3}

Example:

[0, -1, 3, 100, 70, 50]

=> 70*50*100 = 350000

NOTE: Solution will fit in a 32-bit signed integer

My approach: First sort the array, then return the maximum the following numbers: The product of the first two smallest numbers and the last number (largest) or the product of the three largest numbers.

My solution as a runtime of \$O(nlogn)\$ due to the sort and a space complexity of \$O(1)\$. I wonder whether there is a faster solution than \$O(nlogn)\$.

function highesProd(a) {
  a.sort((a,b) => a - b);
  return Math.max(a[0] * a[1] * a[a.length - 1], a[a.length - 3] * a[a.length - 2] * a[a.length - 1]);
}