Move Negative Elements in the Array to one side
Description
- An array contains both positive and negative numbers in random order. Rearrange the array elements so that all negative numbers appear before all positive numbers.
Constraints
Code
Approach 1: Using built-in sort()
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void move(vector<int> &arr)
{
sort(arr.begin(), arr.end());
}
int main()
{
vector<int> arr = {-1, 2, -3, 4, 5, 6, -7, 8, 9};
move(arr);
for (int e : arr)
cout << e << " ";
return 0;
}Explanation
- Here we simply use the inbuilt
sortfunction to re-arrange the arrayarrin an ascending order.
Analysis
- Time Complexity:
O(n log n) - Space Complexity:
O(n)
Approach 2: Two Pointer Method
#include <iostream>
using namespace std;
void shiftall(int arr[], int left, int right)
{
while (left <= right)
{
if (arr[left] < 0 && arr[right] < 0)
left += 1;
else if (arr[left] > 0 && arr[right] < 0)
{
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left += 1;
right -= 1;
}
else if (arr[left] > 0 && arr[right] > 0)
right -= 1;
else
{
left += 1;
right -= 1;
}
}
}
void display(int arr[], int right)
{
for (int i = 0; i <= right; ++i)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr[] = {-12, 11, -13, -5,
6, -7, 5, -3, 11};
int arr_size = sizeof(arr) /
sizeof(arr[0]);
shiftall(arr, 0, arr_size - 1);
display(arr, arr_size - 1);
return 0;
}Explanation
- The shiftall function takes an array
arr, a left indexleft, and a right indexrightas input. - The function uses a
whileloop to iterate while theleftindex is less than or equal to therightindex. - Inside the loop, the function checks the values at the
leftandrightindices of the array. - If both values are negative, the
leftindex is incremented by1. - If the value at the
leftindex is positive and the value at therightindex is negative, the function swaps the values and increments theleftindex by1and decrements therightindex by1. - If both values are positive, the
rightindex is decremented by1. - If none of the above conditions are met, it means that the value at the
leftindex is negative and the value at therightindex is positive. In this case, both indices are incremented and decremented by1, respectively. - The function continues this shifting operation until the
leftindex exceeds therightindex.
Analysis
- Time Complexity:
O(n) - Space Complexity:
O(1)
def shift_all(arr, left, right):
while left <= right:
if arr[left] < 0 and arr[right] < 0:
left += 1
elif arr[left] > 0 and arr[right] < 0:
arr[left], arr[right] = arr[right], arr[left]
left += 1
right -= 1
elif arr[left] > 0 and arr[right] > 0:
right -= 1
else:
left += 1
right -= 1Explanation
-
The function
shift_alltakes an arrayarr, a left indexleft, and a right indexrightas input. It performs the same shifting operation as the given C++ code. -
In Python, you don’t need to explicitly define the array size when passing it to a function.
Analysis
- Time Complexity:
O(N) - Space Complexity:
O(1)