-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquaresOfASortedArray.js
63 lines (54 loc) · 1.61 KB
/
squaresOfASortedArray.js
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
const sortedSquares = A => {
const squared = [];
let negativePointer;
for (let i = 0; i < A.length; i++) {
if (A[i] < 0) negativePointer = i;
squared.push(A[i]**2);
}
let positivePointer = negativePointer + 1;
const output = [];
if (negativePointer >= 0) {
while (negativePointer >= 0 && positivePointer < squared.length) {
if (squared[negativePointer] < squared[positivePointer]) {
output.push(squared[negativePointer]);
negativePointer--;
} else {
output.push(squared[positivePointer]);
positivePointer++;
}
}
for (let i = negativePointer; i >= 0; i--) {
output.push(squared[i]);
}
for (let i = positivePointer; i < squared.length; i++) {
output.push(squared[i]);
}
return output;
} else {
return squared;
}
};
/*
specification
input: array of sorted integers in increasing order
output: array of the squares of the sorted integers sorted in increasing order
constraints:
edge cases:
justification:
to square an array of integers and sort the results in increasing order
explanation
the input array of integers will determine the output array
visualization
approximation
loop through integers and square them
square the integers
if there are negatives keep track of the the index of where they are
declare empty output array
populate an output array based on whether there are negative numbers
return output array
verification
implementation
*/
console.log(sortedSquares([-4,-1,0,3,10])); // [0,1,9,16,100]
console.log(sortedSquares([-7,-3,2,3,11])); // [4,9,9,49,121]
console.log(sortedSquares([-2,0])); // [0,4]