Problem Statement
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 are the total points a person earned.
Given a and b, determine their respective comparison points.
Example
a = [1, 2, 3]
b = [3, 2, 1]
For elements [0], Bob is awarded a point because
a[0]>b[0]
.For the element [1] ,
a[1]
andb[1]
are equal ,so no points are earned .Finally, for elements [2],
a[2] > b[2]
so Alice receives a point.The return array is
[1, 1]
with Alice's score first and Bob's second.
Function Description
Complete the function compareTriplets in the editor below.
compareTriplets has the following parameter(s):
- int a[3]: Alice's challenge rating
- int b[3]: Bob's challenge rating
Sample Input:
17 28 30
99 16 8
Sample Output
2 1
Explanation
The function compareTriplets()
has already declared alongside its parameters $a
and $b
which are the challenge ratings for Alice and Bob respectively. All you need to do now is to write instructions in that function to award them points when their ratings are compared with the conditions above and return the comparison array with Alice score coming first.
Let's use our sample input as an example,
-
b[0] > a[0]
so Bob is awarded one point. -
a[0] > b[0]
so Alice is awarded one point. -
a[0] > b[0]
so Alice is awarded one point.
This gives Alice a total of 2 points and Bob, 1 point. Now we return an array with the comparison results.
- The comparison array is
[2,1]
.
Solution
After reading the problem, we can observe 2 things:
- The comparison array must come with Alice score first.
- If both ratings are equal, nobody receives a point.
Psuedocodes
Pseudocode is a plain language description of the steps in an algorithm.
With the above observations, our pseudocode can be executed as follows:
- Initialize the score for Alice and Bob to 0, respectively.
- Loop through both arrays for their ratings.
- For every loop, compare both ratings based on the array index and assign one point to the score of the person with the greater rating.
- After the loop, return the comparison array with the cumulative points of Alice and Bob.
Now let's get coding!
Here is a solution to this problem in
Javascript
andPHP
. I hope it gives you an idea to solve it in any preferred language.
Now, we can write the Javascript
code as follows:
function compareTriplets(a, b) {
let aScore=0;
let bScore=0;
for(let i=0; i<3; i++){
if( a[ i]> b[ i])
{
aScore+=1;
}
else{
if( a[ i]< b[ i])
{
bScore+=1;
}
}
}
let comparison_array = [ aScore, bScore];
return comparison_array;
}
Now, we can write the PHP
code as follows:
function compareTriplets($a, $b) {
$aScore=0;
$bScore=0;
for($i=0; $i<3;$i++){
if($a[$i]>$b[$i])
{
$aScore+=1;
}
elseif($a[$i]<$b[$i])
{
$bScore+=1;
}
}
return array($aScore, $bScore);
}
Quite an easy one right? Well done😎.
Conclusion
This is a very good example of an easy question in HackerRank. Although it's very tricky because it looks very complicated at the first read but the first step in solving it is understanding the problem statement. Then you'll be able to solve it easily. If you have any questions or suggestions, do well to drop them in the comment section. Thank you.
The HackerRank series for Algorithm Warmup Challenges continues, so stay tuned!🥰
Top comments (2)
Hi, thanks for your javascript solution i was struggling on the comprehension and logic and it really opened my mind !
Hello, though I know it’s simple, it’s very difficult to me, thanks for your help