Header Ad

HackerRank Compare the Triplets problem solution

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


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}`);

}


Post a Comment

1 Comments

  1. #include

    int main(){
    int a[100],b[100];
    for(int i=0;i<3;i++){
    scanf("%d",&a[i]);
    scanf("%d",&b[i]);
    }
    int a_count=0,b_count=0;

    for(int i=0;i<3;i++){

    if(a[i]>b[i])
    {
    a_count++;

    }

    else if(a[i]<b[i]){
    b_count++;
    // printf("%d",b_count);
    }

    else if(a[i]==b[i]){
    b_count=b_count+0;
    a_count=a_count+0;
    }


    }

    printf("%d %d",a_count,b_count);
    return 0;
    }

    why is not working?

    ReplyDelete