array manipulation
integer sorting
positive and negative integers
re-arrange array
algorithm design

Given an array of positive and negative integers, re-arrange it so that you have positive integers on one end and negative integers on other

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

When working with arrays of integers, a common requirement is to segregate the array into two parts: one containing all the positive integers and the other containing all the negative integers. This article will explore various approaches to achieve this, delve into technical explanations for each, and offer guidance for practical implementations.

Problem Explanation

Given an array of positive and negative integers, the task is to rearrange it such that all positive integers are at one end and all negative integers at the other end. The key is to achieve this separation while maintaining an efficient approach in terms of time and space complexity.

Approaches to Solve the Problem

Several methods can achieve the separation of positive and negative integers in an array. Let's explore the most common and efficient techniques.

1. Two-Pointer Technique

The two-pointer technique is a common method used to solve a variety of array partition problems. This technique involves using two pointers: one starting at the beginning of the array (`left`) and one at the end (`right`). Here's how the process works:

  1. Initialize two pointers: `left` starting at index 0 and `right` starting at the last index of the array.
  2. Increment the `left` pointer until you find a negative number.
  3. Decrement the `right` pointer until you find a positive number.
  4. Swap the elements at `left` and `right`.
  5. Continue this process until the `left` pointer is greater than the `right`.

Example

Consider the array `arr = [12, -7, 5, -8, 2, -3]`. Applying the two-pointer method would work as follows:

  • Initialize: `left = 0, right = 5`.
  • Iteration 1: Swap elements at indices 0 and 5. Result: `[-3, -7, 5, -8, 2, 12]`.
  • Iteration 2: Move `left` to index 2, `right` to index 3, and swap. Result: `[-3, -7, -8, 5, 2, 12]`.
  • Iteration ends as `left` is greater than `right`.

2. Quick Sort Partitioning

This approach modifies the partitioning principle of the Quick Sort algorithm:

  1. Choose a pivot, here it is zero.
  2. Iterate over the array, maintaining two partitions: numbers less than the pivot (negative integers) and others.
  3. Adjust the array by swapping elements to ensure all negatives are on one side.

Example

For the array `arr = [-5, 3, -1, 8, -6]`:

  • Start with the first element as the pivot: 0.
  • Partition the array such that negatives are to its left.

Resulting array might be `[-5, -1, -6, 3, 8]` after the partitioning step.

3. Auxiliary Arrays

Another method involves using an additional array to store values temporarily:

  1. Traverse the original array and copy positive integers to one auxiliary array and negative integers to another.
  2. Combine the auxiliary arrays into the original.

This method is straightforward but involves additional space, which can be a constraint in memory-limited environments.

4. Counting and Rearranging

This method involves counting the number of positive and negative integers and then rearranging the original array:

  1. Traverse to count the positive and negative integers.
  2. Rewrite the original array starting with all negative integers followed by positive ones.

This approach ensures the array is efficiently modified in-place but involves two passes: one for counting and one for rearranging.

Comparison of Approaches

ApproachTime ComplexitySpace ComplexityFavorable Scenarios
Two-PointerO(n)O(n)O(1)O(1)Arrays where in-place rearrangement is needed.
Quick SortO(n)O(n)O(1)O(1)When familiar with Quick Sort partitioning.
Auxiliary ArraysO(n)O(n)O(n)O(n)Simplicity is prioritized over space efficiency.
Counting & Rearr.O(n)O(n)O(1)O(1)Useful for counting and immediate rearrangement.

Conclusion

Choosing the right approach depends on the constraints and requirements specified. The two-pointer method emerges as a balanced choice offering both time efficiency and minimal space usage, making it suitable for real-world applications where memory is a critical factor.

Understanding different methods enables informed decision-making and the ability to tailor a solution that best fits specific needs. When dealing with arrays involving a mix of positive and negative integers, these techniques provide a comprehensive toolkit for efficient problem-solving.


Course illustration
Course illustration

All Rights Reserved.