Reverse the Array
PHP
Easy
5 views
Problem Description
Print the array in reverse order.
Input Format
First n. Next line n integers.
Output Format
One line reversed.
Official Solution
<?php
$inputText=trim(stream_get_contents(STDIN));
if($inputText==='') exit;
$tokens=preg_split('/\\s+/', $inputText);
$i=0; $n=intval($tokens[$i++] ?? 0);
$a=[];
for($k=0;$k<$n;$k++) $a[] = $tokens[$i++] ?? '0';
$output=[];
for($k=$n-1;$k>=0;$k--) $output[]=$a[$k];
echo implode(' ',$output);
?>
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!