Untitled

mail@pastecode.io avatar
unknown
plain_text
16 days ago
1.3 kB
1
Indexable
Never
<?php

/*
 * Complete the 'circularArrayRotation' function below.
 *
 * The function is expected to return an INTEGER_ARRAY.
 * The function accepts following parameters:
 *  1. INTEGER_ARRAY a
 *  2. INTEGER k
 *  3. INTEGER_ARRAY queries
 */

function circularArrayRotation($a, $k, $queries) {
    $n = count($a);
    $k = $k % $n;

    // Perform the rotation by calculating the new indices directly
    $rotatedArray = [];
    for ($i = 0; $i < $n; $i++) {
        $rotatedArray[($i + $k) % $n] = $a[$i];
    }

    // Fetch the results for each query index
    $result = [];
    foreach ($queries as $q) {
        $result[] = $rotatedArray[$q];
    }

    return $result;
}

$fptr = fopen(getenv("OUTPUT_PATH"), "w");

$first_multiple_input = explode(' ', rtrim(fgets(STDIN)));

$n = intval($first_multiple_input[0]);
$k = intval($first_multiple_input[1]);
$q = intval($first_multiple_input[2]);

$a_temp = rtrim(fgets(STDIN));
$a = array_map('intval', preg_split('/ /', $a_temp, -1, PREG_SPLIT_NO_EMPTY));

$queries = array();
for ($i = 0; $i < $q; $i++) {
    $queries_item = intval(trim(fgets(STDIN)));
    $queries[] = $queries_item;
}

$result = circularArrayRotation($a, $k, $queries);

fwrite($fptr, implode("\n", $result) . "\n");

fclose($fptr);
Leave a Comment