Given an unsorted array arr[ ] having both negative and positive integers. Place all negative elements at the end of the array without changing the order of positive elements and negative elements.
Examples:
Input: arr[] = [1, -1, 3, 2, -7, -5, 11, 6]
Output: [1, 3, 2, 11, 6, -1, -7, -5]
Explanation: All negative elements [-1, -7, -5] were arranged after positive elements [1, 3, 2, 11, 6] and the relative ordering was also preserved.Input: arr[] = [-5, 7, -3, -4, 9, 10, -1, 11]
Output: [7, 9, 10, 11, -5, -3, -4, -1]
Explanation: All negative elements [-5, -3, -4, -1] were arranged after positive elements [7, 9, 10, 11] and the relative ordering was also preserved.
[Expected Approach] Using a Single Array - O(n) Time and O(n) Space
The idea is to use a temporary array that first stores positives and then negatives. Finally copy temp back to arr.
For example, arr[] = [1, -1, -3, -2, 7, 5, 11, 6]
- Iterate through array and store only positives in a temp array, temp[] = [1, 7, 5, 11, 6]
- Iterate the array again and add negative elements to temp, temp[] = [1, 7, 5, 11, 6, -1, -3, -2]
- Copy temp[] back into the original array.
#include <iostream>
#include <vector>
using namespace std;
// Function to move all -ve element to end of array
// in same order.
void segregateElements(vector<int>& arr) {
int n = arr.size();
// Create an empty array to store result
vector<int> temp(n);
int idx = 0;
// First fill non-negative elements into the
// temporary array
for (int i = 0; i < n; i++) {
if (arr[i] >= 0)
temp[idx++] = arr[i];
}
// Now fill negative elements into the
// temporary array
for (int i = 0; i < n; i++) {
if (arr[i] < 0)
temp[idx++] = arr[i];
}
// copy the elements from temp to arr
arr = temp;
}
int main() {
vector<int> arr = {1 ,-1 ,-3 , -2, 7, 5, 11, 6};
segregateElements(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
import java.util.Arrays;
class GfG {
// Function to move all -ve element to end of array
// in same order.
static void segregateElements(int[] arr) {
int n = arr.length;
// Create an empty array to store result
int[] temp = new int[n];
int idx = 0;
// First fill non-negative elements into the
// temporary array
for (int i = 0; i < n; i++) {
if (arr[i] >= 0)
temp[idx++] = arr[i];
}
// Now fill negative elements into the
// temporary array
for (int i = 0; i < n; i++) {
if (arr[i] < 0)
temp[idx++] = arr[i];
}
// copy the elements from temp to arr
System.arraycopy(temp, 0, arr, 0, n);
}
public static void main(String[] args) {
int[] arr = {1, -1, -3, -2, 7, 5, 11, 6};
segregateElements(arr);
for (int ele: arr)
System.out.print(ele + " ");
}
}
def segregateElements(arr):
n = len(arr)
# Create an empty array to store result
temp = [0] * n
idx = 0
# First fill non-negative elements into the
# temporary array
for i in range(n):
if arr[i] >= 0:
temp[idx] = arr[i]
idx += 1
# Now fill negative elements into the
# temporary array
for i in range(n):
if arr[i] < 0:
temp[idx] = arr[i]
idx += 1
# copy the elements from temp to arr
for i in range(n):
arr[i] = temp[i]
if __name__ == "__main__":
arr = [1, -1, -3, -2, 7, 5, 11, 6]
segregateElements(arr)
print(" ".join(map(str, arr)))
using System;
class GfG {
// Function to move all -ve element to end of array
// in same order.
static void segregateElements(int[] arr) {
int n = arr.Length;
// Create an empty array to store result
int[] temp = new int[n];
int idx = 0;
// First fill non-negative elements into the
// temporary array
for (int i = 0; i < n; i++) {
if (arr[i] >= 0)
temp[idx++] = arr[i];
}
// Now fill negative elements into the
// temporary array
for (int i = 0; i < n; i++) {
if (arr[i] < 0)
temp[idx++] = arr[i];
}
// copy the elements from temp to arr
Array.Copy(temp, arr, n);
}
static void Main() {
int[] arr = {1, -1, -3, -2, 7, 5, 11, 6};
segregateElements(arr);
Console.WriteLine(string.Join(" ", arr));
}
}
// Function to move all -ve element to end of array
// in same order.
function segregateElements(arr) {
let n = arr.length;
// Create an empty array to store result
let temp = new Array(n);
let idx = 0;
// First fill non-negative elements into the
// temporary array
for (let i = 0; i < n; i++) {
if (arr[i] >= 0)
temp[idx++] = arr[i];
}
// Now fill negative elements into the
// temporary array
for (let i = 0; i < n; i++) {
if (arr[i] < 0)
temp[idx++] = arr[i];
}
// copy the elements from temp to arr
for (let i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
// Driver Code
let arr = [1, -1, -3, -2, 7, 5, 11, 6];
segregateElements(arr);
console.log(arr.join(" "));
Output
1 7 5 11 6 -1 -3 -2
Related Articles:
Rearrange positive and negative numbers with constant extra space
Alternate Rearrangement of Positives and Negatives