-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
27 lines (24 loc) · 768 Bytes
/
Copy pathMain.java
File metadata and controls
27 lines (24 loc) · 768 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
public class Main {
public static void main(String[] args) {
int[] A = {0};
int[] res = SquareArray(A);
System.out.println(res);
}
public static int[] SquareArray(int[] A) {
// write your code here
int[] res = new int[A.length];
int leftIndex = 0, rightIndex = A.length - 1;
int tmpIndex = A.length - 1;
while (rightIndex >= leftIndex) {
if (Math.abs(A[rightIndex]) >= Math.abs(A[leftIndex])) {
res[tmpIndex] = A[rightIndex] * A[rightIndex];
rightIndex--;
} else {
res[tmpIndex] = (A[leftIndex]) * A[leftIndex];
leftIndex++;
}
tmpIndex--;
}
return res;
}
}