Codxplore.com

Latest news:

Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif

Hacker Rank: Day 7 Arrays


Hacker Rank: Day 7 Arrays

Task

Given an array,A, of N integers, print A's elements in reverse order as a single line of space-separated numbers.

Example

A=[1,2,3,4]

Print 4 3 2 1. Each integer is separated by one space.

Input Format

The first line contains an integer,N(the size of our array).

The second line contains N space-separated integers that describe array A's elements.

Constraints

1 <= N <= 1000

1 <= A[i] <= 10000, where A[i] is the ith integer in the array.

Output Format

Print the elements of array A in reverse order as a single line of space-separated numbers.

Sample Input

4

1 4 3 2

Sample Output

2 3 4 1

PHP Solutions:

$stdin = fopen("php://stdin", "r");fscanf($stdin, "%d\n", $n);
fscanf($stdin, "%[^\n]", $arr_temp);
$num = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
$start = 0;
$end = $n-1;
while($start < $end){
    $temp = $num[$start];
    $num[$start] = $num[$end];
    $num[$end] = $temp;
    $start+=1;
    $end-=1;
}
for($i=0;$i<$n;$i++){
    print $num[$i].' ';
}
fclose($stdin);
Tags : hackerRank, PHP,

Views : 1079

Subscribe Us


Follow Us