Question 3
Question 3
Question 1:
Given an array of integers, calculate the ratios of its elements that are positive, negative,
and zero. Print the decimal value of each fraction on a new line with 6 places after the
decimal.
Note: This challenge introduces precision problems. The test cases are scaled to six
decimal places, though answers with absolute error of up to 10^6 are acceptable.
There is n=5 elements, two positive, two negative and one zero. Their ratios are 2/5 =
0.400000, 2/5 = 0.400000 and 1/5 = 0.200000.
0.400000
0.400000
0.200000
Solution (JavaScript):
function printOut(count,length){
console.log((count/length).toPrecision(6));
}
function plusMinus(arr) {
let arrLength = arr.length;
let posCount = 0;
let negCount = 0;
let zeroCount = 0;
for(let a of arr){
if(a<0){
negCount++;
} else if(a>0) {
posCount++;
} else{
zeroCount++;
}
}
printOut(posCount,arrLength);
printOut(negCount,arrLength);
printOut(zeroCount,arrLength);
}
Question 1:
Given five positive integers, find the minimum and maximum values that can be calculated
by summing exactly four of the five integers. Then print the respective minimum and
maximum values as a single line of two space-separated long integers.
The minimum sum is 1+3+5+7 = 16 and the maximum sum is 3+5+7+9 = 24.
Solution (JavaScript):
function miniMaxSum(arr) {
arr = arr.sort();
console.log(arr[0]+arr[1]+arr[2]+arr[3],arr[1]+arr[2]+arr[3]+arr[4]);
}