Reverse the Array

Reverse the Array

Easy PHP PHP Arrays 32 views
Explanation Complexity

Problem Statement

Print the array in reverse order.

Input Format

First n. Next line n integers.

Output Format

One line reversed.

Example

5
1 2 3 4 5
5 4 3 2 1

Constraints

n

Input / Output Format

Input Format
First n. Next line n integers.
Output Format
One line reversed.
Constraints
n

Examples

Input:
5 1 2 3 4 5
Output:
5 4 3 2 1

Example Solution (Public)

PHP
<?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);
?>

Official Solution Code

<?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);
?>
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.