Read an Array

Lab Java Beginner 10 points

Read n integers into an array. Print them in reverse order on one line.

Input

Line 1: n. Line 2: n integers.

Output

One line.

Example

Input

5
1 2 3 4 5

Output

5 4 3 2 1
Sample input available Sample output available
Editor
Java

Hints

new int[n] creates an array of n zeros.

Indexes run from 0 to n - 1.

Solution explanation

An array has a fixed length, known when it's created (a.length).

Show solution
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        StringBuilder sb = new StringBuilder();
        for (int i = n - 1; i >= 0; i--) {
            sb.append(a[i]);
            if (i > 0) {
                sb.append(' ');
            }
        }
        System.out.println(sb);
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD