Write a code in Java programming: Suppose a robot is trying to cross a river. The system provides two suggestion lists like the following: Given: list1 = ["swim", "boat", "via bridge A", "airplane"] list2 = ["via bridge B", "swim", "give up"] The robot will pick "swim" [Explain] The robot will pick the solution based on the common suggestion from the two lists. If there is not a common suggestion, it should return null. Additionally, if there are multiple common suggestions in the list, the robot will pick the solution that has the highest sum of the list index. For example, Given: list1 = ["swim", "boat", "via bridge A", "airplane"] list2 = ["via bridge B", "airplane", "swim", "give up"] The robot will pick "airplane" [Explain] both lists have "swim" and "airplane." The index for "swim" in list1 is 0 and in list2 is 2, thus the sum of them is 2. On the other hand, the index for "airplane" in list1 is 3 and in list2 is 1, thus the sum of them is 4. Since the robot should pick the solution which has the highest sum of the list index, it will pick the airplane. If there is a choice tie, you can return any one of them. Finally, please check below for the method signature. public static String findRiverCrossingSolution(ArrayList list1, ArrayList list2)
Write a code in Java
Suppose a robot is trying to cross a river. The system provides two suggestion lists like the following:
Given:
list1 = ["swim", "boat", "via bridge A", "airplane"]
list2 = ["via bridge B", "swim", "give up"]
The robot will pick "swim"
[Explain] The robot will pick the solution based on the common suggestion from the two lists. If there is not a common suggestion, it should return null. Additionally, if there are multiple common suggestions in the list, the robot will pick the solution that has the highest sum of the list index. For example,
Given:
list1 = ["swim", "boat", "via bridge A", "airplane"]
list2 = ["via bridge B", "airplane", "swim", "give up"]
The robot will pick "airplane"
[Explain] both lists have "swim" and "airplane." The index for "swim" in list1 is 0 and in list2 is 2, thus the sum of them is 2. On the other hand, the index for "airplane" in list1 is 3 and in list2 is 1, thus the sum of them is 4.
Since the robot should pick the solution which has the highest sum of the list index, it will pick the airplane. If there is a choice tie, you can return any one of them.
Finally, please check below for the method signature.
public static String findRiverCrossingSolution(ArrayList<String> list1, ArrayList<String> list2)
Step by step
Solved in 6 steps with 4 images