-
Notifications
You must be signed in to change notification settings - Fork 0
/
returnSquares.java
36 lines (35 loc) · 1.08 KB
/
returnSquares.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
// Given a sorted array in non-decreasing order,
// return an array of squares of each number,
// also in non-decreasing order. For example:
// [-4,-2,-1,0,3,5] -> [0,1,4,9,16,25]
// time - O(n)
// space - O(n)
public class returnSquares {
public static int[] returnSquare(int[] a){
int start = 0;
int end = a.length - 1;
int[] ans = new int[a.length];
int endIndex = ans.length - 1;
while (start <= end) {
int leftSquare = a[start] * a[start];
int rightSquare = a[end] * a[end];
if(leftSquare > rightSquare){
ans[endIndex] = leftSquare;
start++;
}
else{
ans[endIndex] = rightSquare;
end--;
}
endIndex--;
}
return ans;
}
public static void main(String[] args) {
int[] inputArray = {-4,-2,-1,0,3,5};
int[] resultArray = returnSquare(inputArray);
for(int i = 0 ; i < resultArray.length ; i++){
System.out.print(resultArray[i] + " ");
}
}
}