-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse-an-array.java
More file actions
37 lines (28 loc) · 893 Bytes
/
reverse-an-array.java
File metadata and controls
37 lines (28 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Reverse an Array solution
import java.io.*;
import java.util.*;
public class Main{
// Function to reverse an array
public static void reverse(int[] a){
for(int i=0, j=a.length-1; i<a.length/2; i++, j--){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
public static void display(int[] a){
StringBuilder sb = new StringBuilder();
for(int val: a){ sb.append(val + " "); }
System.out.println(sb);
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = Integer.parseInt(br.readLine());
}
reverse(a);
display(a);
}
}