CodeSignal Arcade – areEquallyStrong

Problem

Call two arms equally strong if the heaviest weights they each are able to lift are equal.

Call two people equally strong if their strongest arms are equally strong (the strongest arm can be both the right and the left), and so are their weakest arms.

Given your and your friend’s arms’ lifting capabilities find out if you two are equally strong.

Example

  • For yourLeft = 10yourRight = 15friendsLeft = 15, and friendsRight = 10, the output should be
    solution(yourLeft, yourRight, friendsLeft, friendsRight) = true;
  • For yourLeft = 15yourRight = 10friendsLeft = 15, and friendsRight = 10, the output should be
    solution(yourLeft, yourRight, friendsLeft, friendsRight) = true;
  • For yourLeft = 15yourRight = 10friendsLeft = 15, and friendsRight = 9, the output should be
    solution(yourLeft, yourRight, friendsLeft, friendsRight) = false.

Solution

Idea

This problem seems to be pretty obvious. Just a bunch of if and else and it will be solved. However, it will be much more complicated if we add one more parameter (for example: left leg) to the comparison. Therefore, we need to see the big picture and find the pattern of this problem.

My strengths or friend’s strengths here are just a bunch of numbers, so they’re array of integers. The two array should be equals regarding the order of the elements. Therefore, firstly we will sort the arrays by the element then comparing it. The easiest way to compare is joining them to string. That’s it.

Code

function solution(yourLeft, yourRight, friendsLeft, friendsRight) {
    const myArms = [yourLeft, yourRight];
    const friendArms = [friendsLeft, friendsRight];
    
    return myArms.sort().join(",") === friendArms.sort().join(",");
}
Leave a Reply 0

Your email address will not be published. Required fields are marked *