Untitled
unknown
java
2 years ago
1.5 kB
4
Indexable
public class Solution { public static void main(String[] args) { // Example input int parentPos = 1; // Initial position of parent int childPos = 10; // Initial position of child int velParent = 3; // Velocity of parent int steps = 10; // Number of steps parent takes int[] result = calculateCommonFootstepsAndVelocity(parentPos, childPos, velParent, steps); System.out.println(result[0] + " " + result[1]); // Output: F and V2 } private static int[] calculateCommonFootstepsAndVelocity(int parentPos, int childPos, int velParent, int steps) { int maxCommonSteps = 0; int bestVelocity = 0; // Assuming the velocity of the child can be at most twice the parent's for (int velChild = 1; velChild <= 2 * velParent; velChild++) { int commonSteps = 0; for (int step = 0; step < steps; step++) { int parentStepPos = parentPos + step * velParent; int childStepPos = childPos + step * velChild; if (parentStepPos == childStepPos) { commonSteps++; } } if (commonSteps >= maxCommonSteps) { maxCommonSteps = commonSteps; bestVelocity = velChild; } } return new int[]{maxCommonSteps, bestVelocity}; } }
Editor is loading...
Leave a Comment