# 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 a, b; copy_n(istream_iterator(cin), 3, begin(a)); copy_n(istream_iterator(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++; } 

A little more efficient: else if

 1 2 3 4 5 6 7 8 9  void compare_triplets(int* a, int* b, int* points) { for (size_t i = 0; i < 3; i++) { if (a[i] > b[i]) { ++points; } else if (a[i] < b[i]) { ++points; } } } 

An alternative is to use the result of the comparison to select which point to update. Even though this solution doesn't use branches (if), on x86 with compiler optimizations on, this is about 7x slower than the if solution above. Loop is unrolled on both programs.

 1 2 3 4 5  void compare_triplets(int* a, int* b, int* points) { for (size_t i = 0; i < 3; i++) { points[a[i] < b[i]] += a[i] != b[i]; } } 

This, instead, is as fast as the else if above:

 1 2 3 4  void compare_triplets3(int* a, int* b, int* points) { points = (a > b) + (a > b) + (a > b); points = (a < b) + (a < b) + (a < b); } 

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

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

A Java alternative:

 1 2 3  pointsAlice = ((a0>b0)?1:0) + ((a1>b1)?1:0) + ((a2>b2)?1:0); pointsBob = ((a0
We've worked on this challenge in these gyms: modena  padua  milan