0% found this document useful (0 votes)
8 views4 pages

Question 3

Q4

Uploaded by

naveenyadav0820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Question 3

Q4

Uploaded by

naveenyadav0820
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

HackerRank Questions 3 & Solution 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.

Example: arr = [1,1,0, -1, -1]

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.

Results are printed as:

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.

Example arr= [1,5,3,7,9]

The minimum sum is 1+3+5+7 = 16 and the maximum sum is 3+5+7+9 = 24.

The function prints 16 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]);
}

You might also like