Compare the Triplet HackerRank Solution in C, C++, Java, Python

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.

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].

Compare the Triplet HackerRank 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 Triplet HackerRank Solution in C++

#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 Triplet HackerRank 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);
    }
}

Compare the Triplet HackerRank 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 Triplet HackerRank Solution in C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution {

    static void Main(String[] args) {
        string[] tokens_a0 = Console.ReadLine().Split(' ');
        int a0 = Convert.ToInt32(tokens_a0[0]);
        int a1 = Convert.ToInt32(tokens_a0[1]);
        int a2 = Convert.ToInt32(tokens_a0[2]);
        string[] tokens_b0 = Console.ReadLine().Split(' ');
        int b0 = Convert.ToInt32(tokens_b0[0]);
        int b1 = Convert.ToInt32(tokens_b0[1]);
        int b2 = Convert.ToInt32(tokens_b0[2]);
        
        int alice = 0;
        int bob = 0;
        if(a0 > b0)
        {
            alice++;
        }
        
        if(a0 < b0)
        {
            bob++;
        }

        if(a1 > b1)
        {
            alice++;
        }
        
        if(a1 < b1)
        {
            bob++;
        }

        if(a2 > b2)
        {
            alice++;
        }
        
        if(a2 < b2)
        {
            bob++;
        }
        
        Console.WriteLine(alice + " " + bob);
    }
}

 

Attempt Compare the Triplet HackerRank Challenge

Link: https://www.hackerrank.com/challenges/compare-the-triplets

Next Challenge – A Very Big Sum HackerRank Solution

Link: https://exploringbits.com/a-very-big-sum-hackerrank-solution/

2 thoughts on “Compare the Triplet HackerRank Solution in C, C++, Java, Python”

  1. def convert_int(a,b):
    null=[]
    for i in a:
    null=null+[int(i)]
    g=[]
    for k in b:
    g=g+[int(k)]
    result=0
    result_=0

    for i in range(0,3):
    if null[i]>g[i]:
    result+=1
    if null[i]<g[i]:
    result_+=1

    print(result,result_)

    a=list(input().split())
    b=list(input().split())
    res=convert_int(a,b)

    Reply
  2. “solution in c”

    #include

    int main()
    {
    int a[3],b[3],scores[2];
    int alice_score=0;
    int bob_score=0;
    for(int i=0;i<3;i++)
    {
    scanf("%d",&a[i]);
    }
    for(int i=0;i<3;i++)
    {
    scanf("%d",&b[i]);
    }
    for(int i=0;i b[i]) {
    alice_score++;
    } else if (a[i] < b[i]) {
    bob_score++;
    }
    }
    scores[0] = alice_score;
    scores[1] = bob_score;
    printf("%d %d", scores[0], scores[1]);
    }

    Reply

Leave a Comment