Skip to content

Instantly share code, notes, and snippets.

@mathew-fleisch
Created January 22, 2021 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathew-fleisch/c1ff0a663aad34e661dff4e83bdb9417 to your computer and use it in GitHub Desktop.
Save mathew-fleisch/c1ff0a663aad34e661dff4e83bdb9417 to your computer and use it in GitHub Desktop.
Get maximum multiple of all possible values in array of integers
// get maximum multiple of all possible values in array of integers
// [1, 2 , 5 , 0, 100] 500
let arr = [-300, -10, 1, 2 , 5 , 0, 100, 99]
// let arr = [-3000, -10, 1, 2 , 5 , 0, 100, 99]
// let arr = [-3000, 10, 1, 2 , 5 , 0, 100, 99]
function getHighestMultiple(arr) {
console.log(arr)
let higha = 0
let highb = 0
let lowa = 0
let lowb = 0
for (let i in arr) {
if (arr[i] > higha) {
highb = higha
higha = arr[i]
}
if (arr[i] > highb && arr[i] != higha) {
highb = arr[i]
}
if (arr[i] < lowa) {
lowb = lowa
lowa = arr[i]
}
if (arr[i] < lowb && arr[i] != lowa) {
lowb = arr[i]
}
// console.log("Higha: " + higha)
// console.log("Highb: " + highb)
// console.log("Lowa: " + lowa)
// console.log("Lowb: " + lowb)
}
let pos = (higha * highb)
let neg = (lowa * lowb)
if (pos > neg) {
return "Max: " + pos
} else {
return "Max: " + neg
}
}
console.log(getHighestMultiple(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment