##### 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 A_{i}).

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<=10^{3}

1<=A_{i}<=10^{4} where A_{i}is i^{th} 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[1000];
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)])
```

## 5 comments: On Arrays – DS Hackerrank problem solution

ShivamThanks, buddy for the answer.

Anonymousyour python code is not working ???

check mine:

Solution:

array = []

intsize = int(input())

for i in range(intsize):

array.append(int(input()))

for j in reversed(range(len(array))):

print(array[j], end=” “)

hasecticThanks

hasecticHello Steve, thanx. For your interest. You have asked about the two for loops and going in details if you see..in the first loop we are taking the input in the array while in the second we are printing it from last and the last limit is stored at n-1 index.

arr[I] is the element of array at ith index.

Hope it will help you.For any further assistance please revert me back.

Thank you

Steven ArmooHi! Thanks for the answer but can you please explain it with comments to describe what each line of code does. Especially…

for(i=0;i>a[i];

}

for(i=n-1;i>=0;i–)

{

cout<<a[i]<<" ";