HackerRank

Simple Array Sum

reads in input like so:

6 // first # is represents how many #s there are
1 2 3 4 10 11 // your output should be 31

use Array’s reduce.

output:
0 1
1 2
2 3
3 4
4 5
15

answer

Compare the Triplets

Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty.

The rating for Alice’s challenge is the triplet a = (a[0], a[1], a[2]), and the rating for Bob’s challenge is the triplet b = (b[0], b[1], b[2]).

The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].

If a[i] > b[i], then Alice is awarded 1 point.
If a[i] < b[i], then Bob is awarded 1 point. If a[i] = b[i], then neither person receives a point. Comparison points is the total points a person earned. Given a and b, determine their respective comparison points.

Sample Input 1

17 28 30
99 16 8

bob wins on first compare 17 vs 99
alice wins on second compare 28 vs 16
alice wins on third compare 30 vs 8

Thus output should be alice’s score first, then bob’s score:
2 1

Diagonal Difference

Given a square matrix, calculate the absolute difference between the sums of its diagonals.

input:
3
11 2 4
4 5 6
10 8 -12

so diagonal starting from top left and going down right: 11 + 5 + -12 = 4
diagonal starting from bottom left and going up right: 10 + 5 + 4 = 19

output:
|19-4| = 15

Keep in mind that the matrix can vary in length.

Plus Minus

See how many
positives,
negatives, and
0s

there are in an array.
Calculate their ratio according to the array’s length.
Make sure the results is in 6 decimal places.

Sample Input

6
-4 3 -9 0 4 1

Sample Output

3, 4, 1, so three positives. 3/6 = 0.500000
-4, -9, so two negatives. 2/6 = 0.333333
0, so 1 zero. 1/6 = 0.166667

0.500000
0.333333
0.166667

For rounding to n-th place we use toFixed(n)
The rest, we simply keep counters and keep track of when one of them happens.

another solution:

eval(1 > 0) // 1
eval(-5 < 0) // 1 eval(2 > 5) // -1
eval(0===0) // 1

posCounter is 7
negCounter is 1
zeros is 1

Staircase

input:
4

output:

#
##
###
####

note the spaces in front. Make sure the # appears in the end.

Our solution is to use recursion.

approach is

print backwards

print forward

Min Max Sum

Get the sum of 4 of the 5 items in an array.
Then console out the min and max of it.

Sample Input

1 2 3 4 5
Sample Output

10 14

Workers and jobs

Given an array of orders [10, 20, 30], where each order takes one robot to complete, we have a starting # of robots, say 60.
Calculate how many orders can be completed.

We send 10 to do the first one. 20 to do the second one. We have 30 robots left. The last job takes 30 robots so works out perfectly.
In this case, the answer is (3) where all three orders can be completed.

Let’s say we have an array of orders [20, 20, 40, 10]. We only have 50 robots this time. The return answers should be 3. Because we can only afford to do 3 jobs. Job 1, 2, and 4.