We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Plus Minus
- Discussions
Plus Minus
Plus Minus
Sort by
recency
|
673 Discussions
|
Please Login in order to post a comment
This is my Java solution in video: https://www.youtube.com/watch?v=YRht_UcDIK4
Time complexity is
Space complexity is
Please let me know what you think.
JavaScript Solution:
function plusMinus(arr) { const n = arr.length;
if(n === 0){ console.log("0.000000"); console.log("0.000000"); console.log("0.000000"); return; }
//count positive, negative, and zero elements
const positiveCount = arr.filter(x => x > 0).length; const negativeCount = arr.filter(x => x < 0).length; const zeroCount = arr.filter(x => x === 0).length;
//calculate ratios const positiveRatio = positiveCount / n; const negativeRatio = negativeCount / n; const zeroRatio = zeroCount / n;
//print results with six decimal places
console.log(positiveRatio.toFixed(6)); console.log(negativeRatio.toFixed(6)); console.log(zeroRatio.toFixed(6));
}
Javascript solution:
my c++ solution showing the solution code only
My PHP Solution: