# Arrays – DS Hackerrank problem solution

##### Arrays – DS Hackerrank

An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, A, of size N, each memory location has some unique index, i (where 0<=i<=N), that can be referenced as A[i] (you may also see it written as Ai).

Given an array, A, of N integers, print each element in reverse order as a single line of space-separated integers.

Input Format

The first line contains an integer, N (the number of integers in A).
The second line contains N space-separated integers describing A.

Constraints

1<=N<=103
1<=Ai<=104 where Aiis ith integer in A
Output Format

Print all N integers in A in reverse order as a single line of space-separated integers.

Sample Input

4
1 4 3 2

Sample Output

2 3 4 1

##### Solution

The 2 solutions provided below both follow this basic logic:
Write each element of input into an array.
Iterate through the array in reverse, printing each element as you go.

```#include <iostream>
using namespace std;
int main()
{
int n,i;
cin>>n;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=n-1;i>=0;i--)
{
cout<<a[i]<<" ";
}
return 0;
}

```
``````import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int[] array = new int[scan.nextInt()];
for(int i = 0; i < array.length; i++){
array[i] = scan.nextInt();
}
scan.close();

for(int i = array.length - 1; i >= 0; i--){
System.out.print(array[i] + " ");
}
}
}
``````

``````#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int i,n,arr;
scanf("%d",&n);

for(i=0;i<n;i++)
scanf("%d",&arr[i]);

for(i=n-1;i>=0;i--)
printf("%d ",arr[i]);

return 0;
}
``````
``````import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = Integer.parseInt(scan.nextLine());
String[] array = scan.nextLine().split(" ");
scan.close();

while(--n >= 0){
System.out.print(array[n] + " ");
}
}
}``````
``````# Enter your code here. Read input from STDIN. Print output to STDOUT
input()
arr = raw_input().split()

print " ".join(reversed(arr))``````
``````# Enter your code here. Read input from STDIN. Print output to STDOUT

N = input()
A = [int(x) for x in raw_input().split()]

print ' '.join([str(x) for x in reversed(A)])``````

### hasectic

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi