<?php
/*
- Complete the 'batonPass' function below. *
- The function is expected to return an INTEGER_ARRAY.
- The function accepts following parameters:
- 1. INTEGER friends
- 2. LONG_INTEGER time */
function batonPass($friends, $time) {
$current = 1;
$direction = 1; // 1, -1
$last_passer = null;
$last_received = null;
for ($i = 0; $i < $time; $i++) {
$next = $current + $direction;
if ($next > $friends) {
$direction = -1;
$next = $current - 1;
} elseif ($next < 1) {
$direction = 1;
$next = $current + 1;
}
$last_passer = $current;
$last_received = $next;
$current = $next;
}
return [$last_passer, $last_received];
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$friends = intval(trim(fgets(STDIN)));
$time = intval(trim(fgets(STDIN)));
$result = batonPass($friends, $time);
fwrite($fptr, implode("\n", $result) . "\n");
fclose($fptr);
Top comments (0)