-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#118.cpp
58 lines (48 loc) · 1.13 KB
/
#118.cpp
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
#include<iostream>
using namespace std;
void reverse(int arr[], int left, int right) {
if (right <= left) return;
int len = right - left + 1;
for (int i = 0; i < len / 2; i++) {
int temp = arr[left+i];
arr[left+i] = arr[right-i];
arr[right-i] = temp;
}
}
void square_and_sort(int arr[], int n) {
// Find index of last negative number
int index = -1;
while (index + 1 < n && arr[index + 1] < 0) {
index++;
}
// Reverse the negative numbers in array
reverse(arr, 0, index);
int temp[n];
int i = 0;
int j = 0;
int k = index + 1;
// Merge the squares of the two sorted subportions of the array
while (j <= index && k < n) {
if (arr[j] * arr[j] < arr[k] * arr[k])
temp[i++] = arr[j] * arr[j++];
else
temp[i++] = arr[k] * arr[k++];
}
while (j <= index) {
temp[i++] = arr[j] * arr[j++];
}
while (k < n) {
temp[i++] = arr[k] * arr[k++];
}
for (int count = 0; count < n; count++) {
arr[count] = temp[count];
}
}
int main() {
int arr[] = {-9,-6,-2,0,2,3,5};
square_and_sort(arr, sizeof(arr)/sizeof(arr[0]));
for (int i = 0; i < sizeof(arr)/sizeof(arr[0]); i++) {
cout << arr[i] << ",";
}
cout << endl;
}