08/03/2023

Compare The Triplets Hackerrank Solution C++ | Algorithms Warmup

Compare The Triplets Hackerrank Solution C++. 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. Triplets Hackerrank Solution. 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]).

Compare The Triplets problem similar to comparing marks of three subjects of two students in a way like if subject1, student1 got high marks than student2 then student1 got 1 point and the same repeat for subject2, and subject3 at the end print both student points. The maximum number of points can a student earn is 3 or the minimum can be 0 or there may be an equal point if subject1 both students got the same point.

Compare The Triplets Hackerrank Solution C++

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 elements *0*, Bob is awarded a point because of 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

Sample Input 0

5 6 7
3 6 10

Sample Output 0

1 1

Submit your solution here: Click here

Tip: No need to use a huge header file as you can see I use in the below problem to use a Master Header File #include<bits/stdc++.h>. This single header file is enough to perform all operations in C++ including strings operations. Before copying the solution I recommended please read this full article, this will help you to build your own logic.

Compare The Triplets Hackerrank Solution C++


#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

int main()
{
	int a0;
	int a1;
	int a2;
	cin >> a0 >> a1 >> a2;
	int b0;
	int b1;
	int b2;
	cin >> b0 >> b1 >> b2;
	int a = 0, 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++;
	cout << a << " " << b;
	return 0;
}

Let's take an example and compare it with our logic, suppose two students are Bob and Alice and Bob got 56, 75, 26 marks and Alice got 84, 45, 26 Now Compare The Triplets

Bob Marks===> 56, 75, 26
Alice Marks==> 84, 45, 26

Bob Marks Alice Marks Bob Point Alice Point

56 < 84 0 1
75 > 45 1 0
26 = 26 0 0

So we can see that Bob and Alice got 1, 1 point in the tale That is an answer printed on the screen.

The Output of Compare The Triplets Hackerrank Solution


The Output of Compare The Triplets Hackerrank Solution

Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

0 Comments: