Objective:
This problem “compare the triplets” involves basic comparison logic and helps in understanding how to iterate through elements of two arrays and compare corresponding elements. Compare two sets of three integers and determine their relative scores.
Problem Statement:
You are given two arrays, each containing three integers. These arrays represent the scores of two participants, Alice and Bob, in three different categories. Your task is to complete the compare the triplets function by comparing their scores in each category and calculating their respective points.
- If Alice’s score in a category is greater than Bob’s, Alice gets 1 point.
- If Bob’s score in a category is greater than Alice’s, Bob gets 1 point.
- If their scores are equal, neither gets a point.
Input Format:
- The first line contains three space-separated integers representing Alice’s scores.
- The second line contains three space-separated integers representing Bob’s scores.
Output Format:
- Print two space-separated integers: the first representing Alice’s total points, and the second representing Bob’s total points.
Example 1:
- Input:
alice = [5, 6, 7]
bob = [3, 6, 10]
- Output:
1 1
- Explanation: Alice wins in the first category (5 vs 3), Bob wins in the third category (10 vs 7), and they tie in the second category (6 vs 6). So, Alice’s points = 1, and Bob’s points = 1.
Example 2:
- Input:
alice = [17, 28, 30]
bob = [99, 16, 8]
- Output:
2 1
- Explanation: Alice wins in the second (28 vs 16) and third (30 vs 8) categories and Bob wins in the first category (99 vs 17). So, Alice’s points = 2, Bob’s points = 1.
Example 3:
- Input:
alice = [1, 2, 3]
bob = [3, 2, 1]
- Output:
1 1
- Explanation: Alice wins in the second category (2 vs 1), Bob wins in the first category (3 vs 1), and they tie in the second category (2 vs 2). So, Alice’s points = 1, and Bob’s points = 1.
Steps to Solve Compare the Triplets:
- Initialize Points: Start with Alice’s and Bob’s points set to 0.
- Compare Scores: For each of the three categories, compare Alice’s and Bob’s scores.
- If Alice’s score is greater, increment Alice’s points.
- If Bob’s score is greater, increment Bob’s points.
- Output Points: Print the final points of Alice and Bob.
Compare The Triplets Problem Snippet
Compare The Triplets Solution:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'compareTriplets' function below.
*
* The function should return an INTEGER_ARRAY as the result.
* The function accepts following parameters:
* 1. INTEGER_ARRAY a
* 2. INTEGER_ARRAY b
*/
public static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
List<Integer> list = new ArrayList<>();
int alice = 0, bob = 0;
for(int i=0; i<3; i++){
if(a.get(i) != b.get(i)){
if(a.get(i) > b.get(i))
alice++;
else
bob++;
}
}
// if(alice > 0)
list.add(alice);
// if(bob > 0)
list.add(bob);
return list;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> b = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = Result.compareTriplets(a, b);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}