1 min read
https://www.geeksforgeeks.org/move-negative-numbers-beginning-positive-end-constant-extra-space/
Cases:
- negative negative: i++
- positive positive: j–
- negative positive: i++, j–
- positive negative: swap, i++, j–
void shiftall(int arr[], int n) {
int i = 0, j = n - 1;
while (i <= j) {
if (arr[i] < 0 && arr[j] < 0) {
i++;
} else if (arr[i] > 0 && arr[j] > 0) {
j--;
} else if (arr[i] < 0 && arr[j] > 0) {
i++;
j--;
} else {
swap(arr[i], arr[j]);
i++;
j--;
}
}
}