Compare The Triplets Hackerrank Solution |Codeityweb
Compare The Triplets Hackerrank Solution in Java and python
Problem Statement:
Two Programmers Alice and Bob Created a problem for hackerrank and they got ratings after reviewing their problems .
Alice Rating are stored in suppose array A and Bob rating is stored in say array B.
Task:
- Know the Task is to check who got more awards and awards will be calculated by checking who got more number for rating in more number of review.
- That is if Alice got [3,2,4] rating and Bob got [2,2,1] rating , then we compare the rating at individual index like 3>2 so Alice got 1 Award , 2==2 So no award will be given and 4>1 so again Alice will get 1 Award.
- So total Alice has 2 Awards and Bob Has 0 award .
- Then we need to return the answer to the calling function.
- That is it, its a simple question .
Compare The Triplets Hackerrank Solution Python:
def compareTriplets(a, b):solution=[]
a_awarded=0
b_awarded=0
for i in range(len(a)):
if a[i]>b[i]:
a_awarded+=1
elif b[i]>a[i]:
b_awarded+=1
solution.append(a_awarded)
solution.append(b_awarded)
return solution
- As discussed above we need to compare the ratings of Alice and Bob so we run a loop to traverse through the list.
- We compare the rating of alice with bob and if it is greater we increment the award counter of alice by 1.
- If Bob has better rating we increment the bob's award counter by 1.
- We create a list called solution to store the results and then we append alice score and bob's score in the solution list.
- Finally we return the list to the calling function.
Compare The Triplets Hackerrank Solution Java:
static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
List<Integer> l=new ArrayList<>();
int av=0;
int bv=0;
for(int i=0;i<a.size();i++)
{
if(a.get(i)>b.get(i))
{
av++;
}
else if(a.get(i)<b.get(i))
{
bv++;
}
}
l.add(av);
l.add(bv);
return l;
}
Explanation:
- The explanation is same as the above ,just here we have provide solution in java .
- we have used Arraylist as a list to return in scores ,rest is same as the above explanation.
Output:
Related Posts:
Conclusion:
You can get this solutions with rest of other solutions in the github repository as well Here.We have hackerrank solutions on different topics and projects.
Do Explore the site to get more contents like this.
Thank you for reading!
0 Comments
Please Let me Know, If you have any doubts.