Created
January 22, 2021 18:55
-
-
Save mathew-fleisch/c1ff0a663aad34e661dff4e83bdb9417 to your computer and use it in GitHub Desktop.
Get maximum multiple of all possible values in array of integers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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