HackerRank Compare The Triplets solution

Hello Programmers, In this post, you will know how to solve the HackerRank Compare The Triplets solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Compare The Triplets solution
HackerRank Compare The Triplets solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Compare The Triplets Solution

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 is 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] .
  • For the equal elements a[1] and b[1], no points are earned.
  • Finally, for elements 2a[2] > b[2] so Alice receives a point.

The return array is [1, 1] with Alices 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

Sample Input 0

5 6 7
3 6 10

Sample Output 0

1 1

Explanation 0

In this example:

  • a = (a[0], a[1], a[2]) = (5, 6, 7)
  • b = (b[0], b[1], b[2]) = (3, 6, 10)

Now, let’s compare each individual score:

  • a[0] > b[0], so Alice receives 1 point.
  • a[1] = b[1], so nobody receives a point.
  • a[2] < b[2], so Bob receives 1 point.

Alice’s comparison score is 1, and Bob’s comparison score is 1. Thus, we return the array [1, 1].

Sample Input 1

17 28 30
99 16 8

Sample Output 1

2 1

Explanation 1

Comparing the 0th elements, 17 < 99 so Bob receives a point.
Comparing the 1st and 2nd elements, 28 > 16 and 30 > 8 so Alice receives two points.
The return array is [2, 1].

HackerRank Compare The Triplets Solution

Compare The Triplets solution in C

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
    int a0; 
    int a1; 
    int a2; 
    scanf("%d %d %d",&a0,&a1,&a2);
    int b0; 
    int b1; 
    int b2; 
    scanf("%d %d %d",&b0,&b1,&b2);
    int a,b;
    if(a0>b0)
        a++;
    else if(b0>a0)
        b++;
    if(a1>b1)
        a++;
    else if(b1>a1)
        b++;
    if(a2>b2)
        a++;
    else if(b2>a2)
        b++;    
        printf("%d %d",a,b);
        
    return 0;

Compare The Triplets solution in Cpp

#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <set>
#include <map>
#include <complex>
#include <iostream>
#include <time.h>
#include <stack>
#include <stdlib.h>
#include <memory.h>
#include <bitset>
#include <math.h>
#include <string>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;
const int MaxN = 1e5 + 10;
const int MOD = 1e9 + 7;
const int INF = 1e9;
int main() {
//  freopen("input.txt", "r", stdin);
  int a[3], b[3];
  for (int i = 0; i < 3; ++i) {
    cin >> a[i];
  }
  int c1 = 0, c2 = 0;
  for (int i = 0; i < 3; ++i) {
    cin >> b[i];
    if (a[i] > b[i]) c1++;
    else if (a[i] < b[i]) c2++;
  }
  cout << c1 << ' ' << c2 << endl;
  return 0;

Compare The Triplets solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a0 = in.nextInt();
        int a1 = in.nextInt();
        int a2 = in.nextInt();
        int b0 = in.nextInt();
        int b1 = in.nextInt();
        int b2 = in.nextInt();
        
        int a = 0;
        int b = 0;
        
        if (a0 > b0) a++;
        if (a0 < b0) b++;
        
        if (a1 > b1) a++;
        if (a1 < b1) b++;
        
        if (a2 > b2) a++;
        if (a2 < b2) b++;
        
        System.out.println(a + " " + b);
    }
}
Ezoicreport this ad

Compare The Triplets solution in Python

#!/bin/python
import sys
a0,a1,a2 = raw_input().strip().split(' ')
A = a0,a1,a2 = [int(a0),int(a1),int(a2)]
b0,b1,b2 = raw_input().strip().split(' ')
B = b0,b1,b2 = [int(b0),int(b1),int(b2)]
alice = bob = 0
for x,y in zip(A,B):
    if x>y: alice += 1
    if x<y: bob += 1
print alice,bob

Compare The Triplets solution using JavaScript

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 scoreA = 0;
    var scoreB = 0;
    if(a0 > b0) scoreA++;
    if(a0 < b0) scoreB++;
    if(a1 > b1) scoreA++;
    if(a1 < b1) scoreB++;
    if(a2 > b2) scoreA++;
    if(a2 < b2) scoreB++;
    console.log(scoreA + " " + scoreB);

Compare The Triplets solution in Scala

object Solution extends App {
  val lines = io.Source.stdin.getLines()
  val total = lines.next().split(" ").map(_.toInt).zip(lines.next().split(" ").map(_.toInt)).fold((0,0)){ case (acc, pair) =>
    if(pair._1 > pair._2)
      (acc._1+1, acc._2)
    else if(pair._1 < pair._2)
      (acc._1, acc._2+1)
    else
      acc
  }
  println(s"${total._1} ${total._2}")

Compare The Triplets solution in Pascal

ses math;
var  i,res,res1:longint; a,b:array[0..1000  ] of longint;
 begin
  for i:=1 to 3 do
   read(a[i]);
   for i:=1 to 3 do
   read(b[i]);
   for i:=1 to 3 do
   if a[i]>b[i] then inc(res) else
   if a[i]<b[i] then inc(res1);
   writeln(res,' ',res1);
 end.

Disclaimer: This problem (Compare The Triplets) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Simple Array Sum solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad