Skip to content
Programming101
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

Learn everything about programming

HackerRank Compare the Triplets problem solution

YASH PAL, 31 July 20243 May 2025

In this HackerRank Compare the Triplets problem solution 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 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

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.

Hackerrank compare the triplets problem solution
Compare the triplets solution

Topics we are covering

Toggle
  • Problem solution in Python programming.
  • Problem solution in Java Programming.
    • Problem solution in C++ programming.
    • Problem solution in C programming.
    • Problem solution in JavaScript programming.

Problem solution in Python programming.

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the compareTriplets function below.
def compareTriplets(a, b):
    nas = []
    x=0
    y=0
    for i in range(0,len(a)):

        if a[i]>b[i]:
            x=x+1
        elif b[i]>a[i]:
            y=y+1
        else:
            continue
    nas.append(x)
    nas.append(y)
    return nas

    

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    a = list(map(int, input().rstrip().split()))

    b = list(map(int, input().rstrip().split()))

    result = compareTriplets(a, b)

    fptr.write(' '.join(map(str, result)))
    fptr.write('n')

    fptr.close()

Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   
    	Scanner scan = new Scanner(System.in);
//    	int N = scan.nextInt();
    	int[] alice = new int[3];
    	int[] bob = new int[3];
    	int a=0, b=0;
    	for(int i=0;i<3;i++)
    		alice[i]=scan.nextInt();
    	for(int i=0;i<3;i++)
    		bob[i]=scan.nextInt();
    	for(int i=0;i<3;i++)
    		if(alice[i]>bob[i])
    			a++;
    		else if(alice[i]<bob[i])
    			b++;
    	System.out.println(a+" "+b);
    	scan.close();
    }
}

 

Problem solution in C++ programming.

#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

#ifdef ONLINE_JUDGE
#define debug(args...)
#else
#define debug(args...) fprintf(stderr,args)
#endif

typedef long long Int;
typedef unsigned long long uInt;
typedef unsigned uint;

int A[5], B[5];

int main(void) {
    int ia = 0;
    int ib = 0;
    
    for (int i = 0; i < 3; i++) {
        cin >> A[i];
    }
    for (int i = 0; i < 3; i++) {
        cin >> B[i];

        if (A[i] < B[i]) {
            ib += 1;
        } else if (A[i] > B[i]) {
            ia += 1;
        }
    }

    cout << ia << " " << ib << "n";
    return 0;
}

 

Problem solution in C programming.

#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;
    
    int a_score = 0;
    int b_score = 0;
    scanf("%d %d %d",&b0,&b1,&b2);
    if (a0 > b0)
        a_score++;
    else if (a0 < b0)
        b_score++;
    else{}
        //no op
        
    if (a1 > b1)
        a_score++;
    else if (a1 < b1)
        b_score++;
    else {}
        //no op

    if (a2 > b2)
        a_score++;
    else if (a2 < b2)
        b_score++;
    else {}
        //no op
    
    printf("%d %d",a_score, b_score);            
    return 0;
}

 

Problem solution in JavaScript programming.

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]);
    
    let bScore = 0;
    let aScore = 0;
    
    for(let i = 0; i < a0_temp.length; i++){
        var aProblem = parseInt(a0_temp[i]);
        var bProblem = parseInt(b0_temp[i]);
        if (aProblem > bProblem) {
            aScore++;
        }
        else if (bProblem > aProblem) {
            bScore++;
        }
    }
    
    console.log(`${aScore} ${bScore}`);

}

Other problems solutions

  • HackerRank A very big sum problem solution
  • HackerRank Diagonal difference problem solution
  • HackerRank Plus minus problem solution
  • HackerRank Mini-Max sum problem solution
  • HackerRank Birthday cake candles problem solution
Algorithms coding problems solutions

Post navigation

Previous post
Next post
  • Automating Image Format Conversion with Python: A Complete Guide
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes