-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArraysDS.java
66 lines (52 loc) · 1.94 KB
/
ArraysDS.java
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package HackerRank.ProblemSolving;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ArraysDS {
private static int[] reverseArray(int[] a) {
// int[] revArr = new int[a.length];
// for (int i = 0; i < a.length; i++){
// for (int j = a.length-1; j>0; j--) {
// revArr[i] = a[j];
//
// }
// }
//
// return revArr;
for(int i=a.length-1, j=0; i!=j && i>j; i--, j++) {
int temp = a[j];
a[j]=a[i];
a[i]=temp;
}
return a;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
// Seems like this is the number that determines the array length
int arrCount = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
// Initializing an empty array, with N allocated labels (determined by arrCount num)
int[] arr = new int[arrCount];
// Filling out the array with String values
String[] arrItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
// Converting the string values into Integers and pushing them to arr
for (int i = 0; i < arrCount; i++) {
int arrItem = Integer.parseInt(arrItems[i]);
arr[i] = arrItem;
}
// Using a reverseArray function to reverse the array elements
int[] res = reverseArray(arr);
for (int i = 0; i < res.length; i++) {
bufferedWriter.write(String.valueOf(res[i]));
if (i != res.length - 1) {
bufferedWriter.write(" ");
}
}
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}