In this post, We are going to solve HackerRank Compare the Triplets Problem. 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 element 0, Bob is awarded a point because a [0].
- For the equal elements a[1] and b[1], no points are earned.
- Finally, for elements 2, a[2] > b[2] so Alice receives a point.
The returned 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
Return
- int[2]: Alice’s score is in the first position, and Bob’s score is in the second.
Input Format
The first line contains 3 space-separated integers, a[0], a[1], and a[2], the respective values in triplet a.
The second line contains 3 space-separated integers, b[0], b[1], and b[2], the respective values in triplet b.
Constraints
1 ≤ a[i] ≤ 100
1 ≤ b[i] ≤ 100

Compare the Triplets C Solution
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){int i;
int a=0,t_a[3];
int a0;
int a1;
int a2;
scanf("%d %d %d",&a0,&a1,&a2);
t_a[0]=a0;
t_a[1]=a1;
t_a[2]=a2;
int b=0,t_b[3];
int b0;
int b1;
int b2;
scanf("%d %d %d",&b0,&b1,&b2);
t_b[0]=b0;
t_b[1]=b1;
t_b[2]=b2;
for (i=0;i<3;i++)
{
if (t_a[i]>t_b[i])
a++;
else if (t_b[i]>t_a[i])
b++;
}
printf("%d %d\n",a,b);
return 0;
}
Compare the Triplets C++ Solution
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>
//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else
//#endif
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define VV(T) vector<vector< T > >
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
v.assign(a, vector<T>(b, t));
}
template <class F, class T>
void convert(const F &f, T &t){
stringstream ss;
ss << f;
ss >> t;
}
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif
#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)
void mainmain(){
vint a(3);
vint b(3);
int aa = 0;
int bb = 0;
rep(i,3) cin>>a[i];
rep(i,3) cin>>b[i];
rep(i,3){
if(a[i]<b[i]) bb++;
if(a[i]>b[i]) aa++;
}
cout<<aa<<" "<<bb<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout<<fixed<<setprecision(20);
mainmain();
}
Compare the Triplets C Sharp Solution
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {
static void Main(String[] args) {
var a = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
var b = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
int aScore = 0, bScore = 0;
for(int i = 0; i < a.Length; i++){
if(a[i] > b[i])
aScore++;
else if(b[i] > a[i])
bScore++;
}
Console.WriteLine("{0} {1}", aScore, bScore);
}
}
Compare the Triplets Java Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'compareTriplets' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER_ARRAY a
* 2. INTEGER_ARRAY b
*/
public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
int AliceTotal = 0;
int BobTotal = 0;
for (int i = 0;i<3;i++){
int aliceScore = a.get(i);
int bobScore = b.get(i );
if (aliceScore != bobScore){
int temp = aliceScore > bobScore ? AliceTotal++ : BobTotal++;
}
}
List<Integer> result = new ArrayList<>();
result.add(AliceTotal);
result.add(BobTotal);
return result;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> b = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = Result.compareTriplets(a, b);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
Compare the Triplets JavaScript Solution
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var a0_temp = readLine().split(' ');
var a0 = parseInt(a0_temp[0]);
var a1 = parseInt(a0_temp[1]);
var a2 = parseInt(a0_temp[2]);
var b0_temp = readLine().split(' ');
var b0 = parseInt(b0_temp[0]);
var b1 = parseInt(b0_temp[1]);
var b2 = parseInt(b0_temp[2]);
var bob = 0;
var alice = 0;
for(var i in a0_temp) {
let a = parseInt(a0_temp[i]);
let b = parseInt(b0_temp[i]);
if(a > b)
alice += 1;
if(a < b)
bob += 1;
}
console.log("%d %d", alice, bob);
}
Compare the Triplets Python Solution
#!/bin/python3
import sys
a0,a1,a2 = input().strip().split(' ')
a0,a1,a2 = [int(a0),int(a1),int(a2)]
b0,b1,b2 = input().strip().split(' ')
b0,b1,b2 = [int(b0),int(b1),int(b2)]
a = 0
b = 0
if a0 > b0:
a+=1
elif a0 < b0:
b+=1
if a1 > b1:
a+=1
elif a1 < b1:
b+=1
if a2 > b2:
a+=1
elif a2 < b2:
b+=1
print("{} {}".format(a,b))
Other Solutions