Rearrange Positive and Negative Numbers in an Array

Rearrange Positive and Negative Numbers in an Array

Rearrange Positive and Negative Numbers in an Array

Rearranging positive and negative numbers in an array is a common programming problem, typically solved in two distinct methods. Each method has its advantages depending on whether maintaining the order of appearance of the elements is required. Let’s explore both approaches in detail, along with examples and algorithms.

Rearrange Positive and Negative Numbers in an Array

Method 1: Rearrange Without Maintaining Order

Overview

This approach focuses on moving all negative numbers to the beginning of the array and positive numbers to the end without preserving the order of elements.

Steps

  1. Traverse the array.
  2. Whenever a negative element is encountered, swap it with the first positive element.
  3. Continue until all elements have been processed.

Algorithm

  1. Input: Declare an array and input its elements.
  2. Traverse: Iterate through the array, identifying negative elements.
  3. Swap: For each negative element, swap it with the first positive element found.
  4. Output: Print the rearranged array.

Code Example

# Function to rearrange array elements
def rearrange_without_order(arr):
    j = 0  # Pointer for the next negative element
    for i in range(len(arr)):
        if arr[i] < 0:
            arr[i], arr[j] = arr[j], arr[i]
            j += 1
    return arr

# Test case
arr = [1, -1, 2, -2, 3, -3]
print("Rearranged array:", rearrange_without_order(arr))

Test Case

  • Input: [1, -1, 2, -2, 3, -3]
  • Output: [-1, -2, -3, 1, 2, 3]

Key Point

This method is efficient but does not maintain the order of appearance of elements.


Method 2: Rearrange While Maintaining Order

Overview

This approach rearranges the array while maintaining the order of appearance of elements. Negative numbers are moved to the beginning, and positive numbers follow.

Steps

  1. Traverse the array.
  2. For each negative element, shift positive numbers one position to the right.
  3. Insert the negative element in its correct position.

Algorithm

  1. Input: Declare an array and input its elements.
  2. Traverse: Iterate through the array.
    • If a positive element is encountered, continue.
    • If a negative element is encountered, right-shift positive elements to create space for the negative element.
  3. Output: Print the rearranged array.

Code Example

# Function to rearrange array elements
def rearrange_with_order(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        if key < 0:
            j = i - 1
            while j >= 0 and arr[j] > 0:
                arr[j + 1] = arr[j]
                j -= 1
            arr[j + 1] = key
    return arr

# Test case
arr = [1, -1, 2, -2, 3, -3]
print("Rearranged array:", rearrange_with_order(arr))

Test Case

  • Input: [1, -1, 2, -2, 3, -3]
  • Output: [-1, -2, -3, 1, 2, 3]

Key Point

This method preserves the order of appearance but is less efficient compared to Method 1.


Conclusion

By understanding and implementing these methods, you can efficiently solve problems involving rearrangement of numbers in arrays. Experiment with the code examples to deepen your comprehension.

CLICK HERE TO KNOW MORE OUR PROGRAM!

 Rearrange Positive and Negative Numbers in an Array

c