Seeking the optimal solution to resolve this issue
Issue: Develop a function called ArrayChallenge (Javascript) that takes a single argument "arr" representing an array of numbers. The function should return the string true if there exist two numbers in the array that can be multiplied to yield a product greater than double the sum of all elements in the array. If such numbers do not exist, the function should return the string false.
For instance: if the array "arr" is [2, 5, 6, -6, 16, 2, 3, 6, 5, 3], the sum of these elements is 42, and doubling it results in 84. In this case, the two elements 16 and 6 can be multiplied to get 96, which is greater than 84, so the function should return true. Conversely, an array like [1, 2, 4] should return false as the product of the two largest elements (4 * 2 = 8) is less than double the sum (14).
my approach was
function ArrayChallenge(arr) {
if (arr.length < 2) return 'false'
let maxNeg = 0
let neg = 0
let pos = 0
let maxPos = 0
const sum = arr.reduce((total, num) => {
if (num < 0) {
if (num < neg) maxNeg = num
else neg = num
} else {
if (num >= maxPos) {
pos = maxPos
maxPos = num
} else if (num > pos) pos = num
}
return total + num
}, 0)
if (maxPos * pos > sum * 2 || maxNeg * neg > sum * 2) return 'true'
else return 'false'
}