Rearrange Positive and Negative Numbers Alternatively
  • Twitter
  • Facebook
  • Snapchat
  • Instagram

38. Rearrange Positive and Negative Numbers Alternatively in C

šŸ“‘ On this page:
  • Introduction
  • C Program to Rearrange Numbers
  • Sample Output
  • Program Explanation
  • Step-by-Step Approach
  • Time & Space Complexity
  • Practice Exercise
  • Frequently Asked Questions
šŸ“š In this tutorial, you will learn:
  • What is alternating positive and negative array rearrangement
  • Different approaches to rearrange numbers
  • Using two-pointer technique
  • In-place rearrangement with O(1) extra space
  • Time and space complexity analysis

Introduction to Alternating Arrangement

The alternating positive and negative numbers problem requires rearranging an array so that positive and negative numbers appear in alternating order, starting with either positive or negative. For example, [-1, 2, -3, 4, -5, 6] is an alternating arrangement.

Key characteristics:

  • Alternating pattern: Positive, Negative, Positive, Negative, ...
  • Order preservation: May or may not preserve original order
  • Extra space: Can be done with or without extra memory
  • Applications: Signal processing, data organization, algorithm design

šŸ’” Example: For array [1, -2, 3, -4, 5, -6], the arrangement is already alternating. For [-1, 2, -3, 4, -5, 6], it's also alternating starting with negative.

C Program to Rearrange Numbers Alternatively

#include <stdio.h>
#include <stdlib.h>

// Function to swap two elements
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

// Approach 1: Using separate arrays (extra space)
void rearrangeWithExtraSpace(int arr[], int n) {
    int *positive = (int *)malloc(n * sizeof(int));
    int *negative = (int *)malloc(n * sizeof(int));
    int posCount = 0, negCount = 0;
    
    // Separate positive and negative numbers
    for (int i = 0; i < n; i++) {
        if (arr[i] >= 0) {
            positive[posCount++] = arr[i];
        } else {
            negative[negCount++] = arr[i];
        }
    }
    
    // Merge alternatively starting with positive
    int i = 0, j = 0, k = 0;
    while (i < posCount && j < negCount) {
        arr[k++] = positive[i++];
        arr[k++] = negative[j++];
    }
    
    // Copy remaining elements if any
    while (i < posCount) {
        arr[k++] = positive[i++];
    }
    while (j < negCount) {
        arr[k++] = negative[j++];
    }
    
    free(positive);
    free(negative);
}

// Approach 2: In-place rearrangement using two-pointer technique
void rearrangeInPlace(int arr[], int n) {
    // First, separate positive and negative numbers
    int j = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] < 0) {
            if (i != j) {
                swap(&arr[i], &arr[j]);
            }
            j++;
        }
    }
    
    // j is the index where negative numbers end
    int negPos = 0;
    int posPos = j;
    
    // Swap positive and negative alternately
    while (negPos < j && posPos < n && arr[negPos] < 0) {
        swap(&arr[negPos], &arr[posPos]);
        negPos += 2;
        posPos++;
    }
}

// Approach 3: Using rotation technique (preserves order)
void rotateArray(int arr[], int start, int end) {
    int temp = arr[end];
    for (int i = end; i > start; i--) {
        arr[i] = arr[i - 1];
    }
    arr[start] = temp;
}

void rearrangePreservingOrder(int arr[], int n) {
    // Strategy: For each position, find the right element and rotate
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0) {
            // Even index: should be positive
            if (arr[i] < 0) {
                // Find next positive element
                int j = i + 1;
                while (j < n && arr[j] < 0) {
                    j++;
                }
                if (j < n) {
                    rotateArray(arr, i, j);
                }
            }
        } else {
            // Odd index: should be negative
            if (arr[i] >= 0) {
                // Find next negative element
                int j = i + 1;
                while (j < n && arr[j] >= 0) {
                    j++;
                }
                if (j < n) {
                    rotateArray(arr, i, j);
                }
            }
        }
    }
}

// Function to print the array
void printArray(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

// Function to check if arrangement is alternating
int isAlternating(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        if ((arr[i] >= 0 && arr[i + 1] >= 0) || (arr[i] < 0 && arr[i + 1] < 0)) {
            return 0;
        }
    }
    return 1;
}

// Driver program to test rearrangement
int main() {
    int arr1[] = {1, 2, 3, -4, -5, -6, 7, -8, 9, -10};
    int n1 = sizeof(arr1) / sizeof(arr1[0]);
    
    printf("Original array: ");
    printArray(arr1, n1);
    
    // Test Approach 1: Extra space
    int *arr1Copy1 = (int *)malloc(n1 * sizeof(int));
    for (int i = 0; i < n1; i++) arr1Copy1[i] = arr1[i];
    rearrangeWithExtraSpace(arr1Copy1, n1);
    printf("Using extra space: ");
    printArray(arr1Copy1, n1);
    printf("Alternating? %s\n\n", isAlternating(arr1Copy1, n1) ? "Yes" : "No");
    free(arr1Copy1);
    
    // Test Approach 2: In-place
    int *arr1Copy2 = (int *)malloc(n1 * sizeof(int));
    for (int i = 0; i < n1; i++) arr1Copy2[i] = arr1[i];
    rearrangeInPlace(arr1Copy2, n1);
    printf("In-place (no order preservation): ");
    printArray(arr1Copy2, n1);
    printf("Alternating? %s\n\n", isAlternating(arr1Copy2, n1) ? "Yes" : "No");
    free(arr1Copy2);
    
    // Test Approach 3: Preserving order
    int *arr1Copy3 = (int *)malloc(n1 * sizeof(int));
    for (int i = 0; i < n1; i++) arr1Copy3[i] = arr1[i];
    rearrangePreservingOrder(arr1Copy3, n1);
    printf("Preserving order (using rotation): ");
    printArray(arr1Copy3, n1);
    printf("Alternating? %s\n\n", isAlternating(arr1Copy3, n1) ? "Yes" : "No");
    free(arr1Copy3);
    
    // Test with equal positives and negatives
    int arr2[] = {-1, 2, -3, 4, -5, 6};
    int n2 = sizeof(arr2) / sizeof(arr2[0]);
    printf("Original: ");
    printArray(arr2, n2);
    
    int *arr2Copy = (int *)malloc(n2 * sizeof(int));
    for (int i = 0; i < n2; i++) arr2Copy[i] = arr2[i];
    rearrangeInPlace(arr2Copy, n2);
    printf("After in-place: ");
    printArray(arr2Copy, n2);
    free(arr2Copy);
    
    // Test with all positives or all negatives
    int arr3[] = {1, 2, 3, 4, 5};
    int n3 = sizeof(arr3) / sizeof(arr3[0]);
    printf("\nArray with all positives: ");
    printArray(arr3, n3);
    
    int *arr3Copy = (int *)malloc(n3 * sizeof(int));
    for (int i = 0; i < n3; i++) arr3Copy[i] = arr3[i];
    rearrangeInPlace(arr3Copy, n3);
    printf("After rearrangement: ");
    printArray(arr3Copy, n3);
    free(arr3Copy);
    
    return 0;
}

Sample Output

Original array: 1 2 3 -4 -5 -6 7 -8 9 -10 
Using extra space: 1 -4 2 -5 3 -6 7 -8 9 -10 
Alternating? Yes

In-place (no order preservation): -4 -5 -6 1 2 3 7 -8 9 -10 
Alternating? No

Preserving order (using rotation): 1 -4 2 -5 3 -6 7 -8 9 -10 
Alternating? Yes

Original: -1 2 -3 4 -5 6 
After in-place: -1 2 -3 4 -5 6 

Array with all positives: 1 2 3 4 5 
After rearrangement: 1 2 3 4 5

Program Explanation

The program demonstrates three different approaches:

  1. Using Extra Space (O(n) time, O(n) space):
    • Separates positive and negative numbers into two arrays
    • Merges them alternatively
    • Preserves original order within each group
    • Simple but uses extra memory
  2. In-place Using Two-Pointer (O(n) time, O(1) space):
    • First separates positives and negatives in the array
    • Then swaps elements to create alternating pattern
    • Does not preserve original order
    • Most memory efficient
  3. Using Rotation Technique (O(n²) time, O(1) space):
    • Finds the right element for each position and rotates
    • Preserves the original order of elements
    • Less efficient but maintains order

Step-by-Step Approach for In-Place Rearrangement

Step Action Explanation
1SeparateMove all negative numbers to the left side
2Two PointersSet negPos at start and posPos at first positive
3Swap AlternatelySwap elements to create alternating pattern
4RepeatContinue until all elements are processed

Algorithm for In-Place Rearrangement

  1. Separate: Move all negative numbers to the left of the array
  2. Initialize: Set negPos = 0, posPos = index of first positive
  3. Swap: While negPos < number of negatives and posPos < n:
    • Swap arr[negPos] and arr[posPos]
    • negPos += 2, posPos++
  4. Done: Array is now alternating

Time & Space Complexity

Approach Time Complexity Space Complexity Order Preserved
Extra Space O(n) O(n) āœ… Yes
In-place O(n) O(1) āŒ No
Rotation O(n²) O(1) āœ… Yes

šŸ“Š Recommendation:

  • If order doesn't matter: Use the two-pointer in-place approach (O(n) time, O(1) space)
  • If order matters: Use the extra space approach (O(n) time, O(n) space)
  • If memory is a constraint and order matters: Use the rotation approach (O(n²) time, O(1) space)

šŸ’» Practice Exercise

Challenge 1: Modify the program to rearrange numbers starting with a negative number.

Challenge 2: Implement an O(n) solution that preserves the original order of numbers.

Challenge 3: Rearrange the array so that all negative numbers come before positives (segregation) first.

šŸ” Click to Show Solution for Challenge 1
// To start with negative, swap the merge order
void rearrangeStartNegative(int arr[], int n) {
    int *positive = (int *)malloc(n * sizeof(int));
    int *negative = (int *)malloc(n * sizeof(int));
    int posCount = 0, negCount = 0;
    
    for (int i = 0; i < n; i++) {
        if (arr[i] >= 0) positive[posCount++] = arr[i];
        else negative[negCount++] = arr[i];
    }
    
    int i = 0, j = 0, k = 0;
    // Start with negative
    while (i < posCount && j < negCount) {
        arr[k++] = negative[j++];
        arr[k++] = positive[i++];
    }
    
    while (i < posCount) arr[k++] = positive[i++];
    while (j < negCount) arr[k++] = negative[j++];
    
    free(positive);
    free(negative);
}

Frequently Asked Questions

1. What happens if there are unequal numbers of positive and negative elements?

If counts are unequal, the extra elements are appended at the end. For example, if there are more positives than negatives, the remaining positives are placed after the alternating pattern.

2. Does this algorithm preserve the original order?

The extra space approach preserves order, and the rotation approach also preserves order. However, the simple two-pointer in-place approach does not preserve the original order of elements.

3. What is the most efficient approach?

The two-pointer approach (O(n) time, O(1) space) is most efficient in terms of space, but it doesn't preserve order. If order preservation is required, the extra space approach (O(n) time, O(n) space) is the best choice.

4. Can this algorithm handle zero?

Yes, in this implementation, zero is treated as positive (arr[i] >= 0). You can change this behavior by using arr[i] > 0 for strictly positive numbers.

5. What are the real-world applications of this problem?

This problem is used in signal processing (alternating signals), data organization (separating different types of data), and algorithm design (partitioning problems).

šŸ’” Pro Tip: This problem is a classic interview question that tests your understanding of array manipulation, in-place algorithms, and partition techniques.

šŸ“– Related Array Assignments

  • 10. Write a program in C to sort elements in an array in ascending order.
  • 11. Write a program in C to sort elements in an array in descending order.
  • 15. Write a program in C to remove duplicate elements from an array.
  • 16. Write a program in C to find the intersection of two arrays.
  • 17. Develop a program in C to find the union of two arrays.
  • 18. Write a program in C to shift elements in an array to the left by a given number of positions.
  • 19. Write a program in C to check if two arrays are equal.

šŸ’” Tip: Practice these assignments in order, starting from basic to advanced. Each problem builds upon concepts from previous ones.

Previous Topic: -->> Delete Duplicate Elements in C   ||   Next topic: -->> Equilibrium Index in C


C Language

  • Home
  • Why C Language
  • History of C Language
  • Applications of C Language
  • Introduction To C
    • What is Program?
    • Structure of C Program
    • Working Of C Program
    • CHARACTER SET
    • VARIABLES AND IDENTIFIERS
    • BUILT-IN DATA TYPES
    • OPERATORS AND EXPRESSIONS
    • CONSTANTS AND LITERALS
    • SIMPLE ASSIGNMENT STATEMENT
    • BASIC INPUT/OUTPUT STATEMENT
    • SIMPLE 'C' PROGRAMS
    • Assignments
  • Operators in C Programming
    • Arithmetic Operators
    • Assignment Operators
    • Increment and Decrement Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Other Operators
    • Assignments
  • Conditional Statements
    • DECISION MAKING WITHIN A PROGRAM
    • CONDITIONS
    • IF STATEMENT
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • SWITCH CASE
    • Assignments
  • Loops Statements
    • Introduction to Loops
    • GO TO Statement
    • Do while Loop
    • While Loop
    • Nested While Loop
    • Difference Between While and Do while
    • Difference Between Goto and loop
    • while loop assignments
    • C FOR Loop
    • C For loop examples
    • Nested for loop
    • Nested for loop examples
    • Infinite while Loops
    • Infinite for Loops
    • Continue in Loops
    • break in Loops
    • difference while do..while & for
    • Assignments
  • Arrays
    • One Dimensional Array
    • Declaring 1D Arrays
    • Initilization of 1D arrays
    • Accessing element of one 1D Array
    • Read and Display 1D Arrays
    • Two Dimensional Arrays
    • Declare 2D Arrays
    • Read and Display 2D Arrays
    • Assignments/Examples
  • Functions
    • Introduction
    • Need For User-Defined Function
    • Multiple Function Program
    • Modular Programming
    • Elements Of User Defined Function
    • Function Definition
    • Function Declaration
    • Types of functions
    • Nesting of Function
    • Recursion
    • Passing Array To Functions
    • Scope,Visibility and Lifetime of Variables
    • Assignments
  • Structure
    • Introduction
    • Array vs Structure
    • Defining Structure
    • Declaring Structure Variables
    • Type Defined Structure
    • Accessing Structure Members
    • Structure Initilization
    • Copying & Comparing Structure Variables
    • Array of Structure
    • Arrays Within Structure
    • Structures Within Structures
    • Structures and Functions
    • Structure Examples/Assignments
  • Union
    • Define Union
    • Create and use Union
    • Difference Between Structure and Union
    • Union Examples
    • Union FAQ
  • Pointers
    • What Are Pointers In C?
    • How Do We Use Pointers In C?
    • Declaration Of A Pointer
    • The Initialization Of A Pointer
    • Syntax Of Pointer Initialization
    • Use Of Pointers In C
    • The Pointer To An Array
    • The Pointer To A Function
    • The Pointer To A Structure
    • Types Of Pointers
    • The Null Pointer
    • The Void Pointer
    • The Wild Pointer
    • The Near Pointer
    • The Huge Pointer
    • The far Pointer
    • dangling pointer
    • Accessing Pointers- Indirectly And Directly
    • Pros Of Using Pointers In C
    • Cons Of Pointers In C
    • Applications Of Pointers In C
    • The & Address Of Operator In C
    • How To Read The Complex Pointers In C?
    • Practice Problems On Pointers
  • File Processing
    • File Handling In C
    • Types Of Files In C
    • Operations Done In File Handling
    • File Examples
    • Binary Files
    • count words,lines in a file
    • Copy files
    • Update File
    • count vowels in a file
  • Preprocessor
    • Macro substitution division
    • File Inclusion
    • Conditional Compilation
    • Other directives
    • Examples
  • Dynamic Memory Allocation
    • malloc
    • calloc
    • free
    • realloc
    • Examples
  • Storage Classes
  • Graphics
  • Frequently Asked Interview Questions (FAQ)
    • Introduction To C FAQ
    • Operators FAQ
    • Conditional Statements FAQ
    • Loops FAQ
    • Arrays FAQ
    • Function FAQ
    • Structure FAQ
    • Pointers FAQ
    • Files FAQ
    • Storage classes FAQ
    • Dynamic Memory FAQ
  • Programs/Assignments
    • Introduction To C
    • Operators
    • Conditional Statements
    • Loops
    • Arrays
    • Function
    • Structure
    • Pointers
    • Files
    • Storage classes
    • Dynamic Memory
  • Case Studies
  • Multiple Choice Questions
    • Introduction To C MCQ
    • Operators MCQ
    • Conditional Statements MCQ
    • Loops MCQ
    • Arrays MCQ
    • Function MCQ
    • Structure MCQ
    • Pointers MCQ
    • Files MCQ
    • Storage classes MCQ
    • Dynamic Memory MCQ
    • More MCQ

Get in touch

  • tech2dsm@gmail.com

© Sankalan Data Tech. All rights reserved.