Rearrange positive and negative numbers in O(n) time and O(1) extra space in an array and also you have to have to maintain order of appearance of positive and negetive number .

Revision en2, by rinku11, 2021-12-29 14:26:52

Here is the brief explanation of my question -:

Given an array of positive and negative numbers, arrange them in an alternate fashion such that every positive number is followed by negative and vice-versa maintaining the order of appearance. Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in the end of the array.

Example: Input: arr[] = {1, 2, 3, -4, -1, 4} Output: arr[] = {-4, 1, -1, 2, 3, 4}

Input: arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8} output: arr[] = {-5, 5, -2, 2, -8, 4, 7, 1, 8, 0}

I'm trying to solve this problem with the constraint O(n) time complexity and O(1) space complexity.But I didn't get any logic

Tags array

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English rinku11 2021-12-29 14:26:52 721
en1 English rinku11 2021-12-29 13:03:59 295 Initial revision (published)