-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.java
More file actions
56 lines (48 loc) · 1.3 KB
/
temp.java
File metadata and controls
56 lines (48 loc) · 1.3 KB
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
import java.util.Arrays;
import java.util.Scanner;
public class temp {
public int[] sortedSquares(int[] a) {
int n = a.length;
int temp[] = new int[n];
// tim i: vi tri dau tien cua so am
int i = -1;
while(i + 1 < n && a[i+1] < 0) {
i++;
}
// tim j: vi tri dau tien cua so duong
int j = i + 1;
int k =0;
while(i >=0 && j < n) {
if(a[i] * a[i] < a[j] * a[j]) {
temp[k++] = a[i] * a[i];
i--;
} else {
temp[k++] = a[j] * a[j];
j++;
}
}
while(i >= 0) {
temp[k++] = a[i] * a[i];
i--;
}
while(j < n) {
temp[k++] = a[j] * a[j];
j++;
}
return temp;
}
public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// int n = scanner.nextInt();
// int[] a = new int[n];
// for (int i = 0; i < n; i++) {
// a[i] = scanner.nextInt();
// }
int a [] = {-4,-1,0,3,10};
temp myTemp = new temp();
int[] result = myTemp.sortedSquares(a);
for (int i = 0; i < a.length; i++) {
System.out.print(result[i] + " ");
}
}
}