Source : https://www.hackerrank.com/challenges/arrays-ds
An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, , of size , each memory location has some unique index, (where ), that can be referenced as (you may also see it written as ).
Given an array, , of integers, print each element in reverse order as a single line of space-separated integers.
Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this.
Input Format
The first line contains an integer, (the number of integers in ).
The second line contains space-separated integers describing .
Constraints
Output Format
Print all integers in in reverse order as a single line of space-separated integers.
Sample Input 1
4
1 4 3 2
Sample Output 1
2 3 4 1
Source : https://www.hackerrank.com/challenges/arrays-ds
Solution
// Karthikalapati.blogspot.com | |
import java.util.Scanner; | |
public class Solution { | |
public static void main(String[] args) { | |
/* Save input */ | |
Scanner scan = new Scanner(System.in); | |
int size = scan.nextInt(); | |
int [] array = new int[size]; | |
for (int i = 0; i < size; i++) { | |
array[i] = scan.nextInt(); | |
} | |
scan.close(); | |
/* Print elements in reverse order */ | |
for (int i = size - 1; i >= 0; i--) { | |
System.out.print(array[i] + " "); | |
} | |
} | |
} |
No comments:
Post a Comment