Compare the Triplets

See the original problem on HackerRank.

Solutions

This is clearly an easy problem we can experiment with.

The easiest way to write the solution in C++ is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
array<int, 3> a, b;
copy_n(istream_iterator<int>(cin), 3, begin(a));
copy_n(istream_iterator<int>(cin), 3, begin(b));

int alice_points = 0;
int bob_points = 0;

for(int i = 0; i < 3; i++) 
{
    if (a[i] > b[i])
        alice_points++;
    if (a[i] < b[i])
        bob_points++;
}

We can use inner_product to exploit the zip | map | reduce pattern:

1
2
3
4
5
6
array<int, 3> a, b;
copy_n(istream_iterator<int>(cin), 3, begin(a));
copy_n(istream_iterator<int>(cin), 3, begin(b));
cout << inner_product(begin(a), end(a), begin(b), 0, plus<int>(), greater<int>()) << " " 
     << inner_product(begin(b), end(b), begin(a), 0, plus<int>(), greater<int>());
}

A Java alternative:

1
2
3
pointsAlice = ((a0>b0)?1:0) + ((a1>b1)?1:0) + ((a2>b2)?1:0);
pointsBob = ((a0<b0)?1:0) + ((a1<b1)?1:0) + ((a2<b2)?1:0);
System.out.println(pointsAlice + " " + pointsBob);
We've worked on this challenge in these gyms: modena 
comments powered by Disqus