Alternate positive and negative numbers | Practice | GeeksforGeeks

Given an unsorted array Arr of N positive and negative numbers. Your task is to create an array of alternate positive and negative numbers without changing the relative order of positive and negative numbers.
Note: Array should start with positive number.
 
Example 1:
Input: 
N = 9
Arr[] = {9, 4, -2, -1, 5, 0, -5, -3, 2}
Output:
9 -2 4 -1 5 -5 0 -3 2

Example 2:
Input: 
N = 10
Arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8}
Output:
5 -5 2 -2 4 -8 7 1 8 0 



Your Task:  
You don't need to read input or print anything. Your task is to complete the function rearrange() which takes the array of integers arr[] and n as parameters. You need to modify the array itself.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 
Constraints:
1 ≤ N ≤ 107
-106 ≤ Arr[i] ≤ 107
0
loveyvishnanics198 hours ago
void rearrange(int arr[], int n) {       // code here       ArrayList<Integer> pos = new ArrayList<Integer>();       ArrayList<Integer> neg = new ArrayList<Integer>();       int i;       for(i=0; i<n; i++){           if(arr[i]>=0){               pos.add(arr[i]);           }       }       for( i=0; i<n; i++){           if(arr[i]<0){               neg.add(arr[i]);           }       }       for(i=0; i<n; i++){           if(!pos.isEmpty() && i%2==0){               arr[i] = pos.get(0);               pos.remove(0);           }           else if(!neg.isEmpty() && i%2!=0 ){               arr[i] = neg.get(0);               neg.remove(0);           }           else if(pos.isEmpty() || neg.isEmpty()){               break;           }       }       while(!pos.isEmpty()){               arr[i++] = pos.get(0);               pos.remove(0);       }       while(!neg.isEmpty() ){               arr[i++] = neg.get(0);               neg.remove(0);           }   }
+2
sidvas582 days ago
void rearrange(int a[], int n) {    int j=0,k=0,b[n],c[n];    for(int i=0;i<n;i++)    {        if(a[i]>=0)        b[j++]=a[i];        else        c[k++]=a[i];    }    int p=0,q=0,i=0;    while(p<j&&q<k)    {        a[i++]=b[p++];        a[i++]=c[q++];    }    if(p<j)    {        while(p<j)        a[i++]=b[p++];    }    if(q<k)    {        while(q<k)        a[i++]=c[q++];    }}
0
rajat8213 days ago
class Solution {   void rearrange(int arr[], int n) {
       ArrayList<Integer> pos = new ArrayList<>();       ArrayList<Integer> neg = new ArrayList<>();              for(int i=0 ; i<n ; i++)       {           if(arr[i]<0) neg.add(arr[i]);           else pos.add(arr[i]);       }              for(int i=0 ; i<n ; i++)       {           if(pos.isEmpty()){               arr[i]=neg.get(0);               neg.remove(0);           }           else if(neg.isEmpty()){               arr[i] = pos.get(0);                   pos.remove(0);           }           else{               if(i%2==0) {                   arr[i] = pos.get(0);                   pos.remove(0);               }               else{                   arr[i] = neg.get(0);                   neg.remove(0);               }           }       }   }}
0
ayckerayush3 days ago
check this out
 vector<int>ans1,ans2;
	    for(int i=0;i<n;i++){
	        if(arr[i]<0)
	        ans1.push_back(arr[i]);
	        else
	        ans2.push_back(arr[i]);
	    }
	    int i=0;
	    for(;i<2*min(ans1.size() , ans2.size());i++){
	        if(i%2 == 0){
	            arr[i]=ans2[i/2];
	        }
	        else
	        arr[i]=ans1[i/2];
	    }
	    int j=i;
	    i/=2;
	    if(ans1.size() > ans2.size()){
	        for(;j<n;){
	            arr[j++]=ans1[i++];
	        }
	    }
	    else if(ans1.size() < ans2.size()){
	        for(;j<n;){
	            arr[j++]=ans2[i++];
	        }
	    }
0
krishnan8radha5 days ago
void rearrange(int arr[], int n) {
        ArrayList<Integer> positive = new ArrayList<>();
        ArrayList<Integer> negative = new ArrayList<>();
        for(int i=0;i<n;i++) {
        	if(arr[i]>=0) {
        		positive.add(arr[i]);
        	}
        	else {
        		negative.add(arr[i]);
        	}
        }
        for(int j=0,m=0;j<n;j=j+2,m++) {
        		arr[j]=positive.get(m);
        }
        for(int k=1,l=0;k<n;k=k+2,l++) {
        		arr[k]=negative.get(l);
        }
    }
0
niravramotiya0032 weeks ago
class Solution:   def rearrange(self,arr, n):       t=0       p=[]       n=[]       for i in arr:           if i>=0:               p.append(i)           else:               n.append(i)       arr.clear()       if len(p)>len(n):           while t<len(p):               arr.append(p[t])               if t<len(n):                   arr.append(n[t])               t+=1       elif len(n)>len(p):           while t<len(n):               if t<len(p):                   arr.append(p[t])               arr.append(n[t])               t+=1       else:           while t<len(p):               arr.append(p[t])               arr.append(n[t])               t+=1
0
niravramotiya0032 weeks ago
class Solution:   def rearrange(self,arr, n):       # code here       t=0       p=[]       n=[]       for i in arr:           if i>=0:               p.append(i)           else:               n.append(i)       arr.clear()       if len(p)>len(n):           while t<len(p):               arr.append(p[t])               if t<len(n):                   arr.append(n[t])               t+=1       elif len(n)>len(p):           while t<len(n):               if t<len(p):                   arr.append(p[t])               arr.append(n[t])               t+=1       else:           while t<len(p):               arr.append(p[t])               arr.append(n[t])               t+=1
0
niravramotiya0032 weeks ago
#Python solution 
#time 1.8
class Solution:   def rearrange(self,arr, n):       # code here       t=0       p=[]       n=[]       for i in arr:           if i>=0:               p.append(i)           else:               n.append(i)       arr.clear()       if len(p)>len(n):           while t<len(p):               arr.append(p[t])               if t<len(n):                   arr.append(n[t])               t+=1       elif len(n)>len(p):           while t<len(n):               if t<len(p):                   arr.append(p[t])               arr.append(n[t])               t+=1       else:           while t<len(p):               arr.append(p[t])               arr.append(n[t])               t+=1
+1
sameerhakeentc20192 weeks ago
class Solution{public:
int next_positive(int arr[],int position,int n)   {       for(int i=position+1;i<n;i++)       {           if(arr[i]>=0)            return i;       }       return n;   }   int next_negative(int arr[],int position,int n)   {       for(int i=position+1;i<n;i++)       {           if(arr[i]<0)            return i;       }       return n;   }   void rearrange(int arr[], int n) {    // code here    int positive=0;    int negative=0;   bool flag1=true,flag2=false;      for(int i=0;i<n;i++)   {       if(arr[i]>0)        {            positive=i;            break;        }   }       for(int i=0;i<n;i++)   {       if(arr[i]<0)        {            negative=i;            break;        }   }       while(positive<n and negative<n)    {        if(arr[positive] >= 0 and flag1)        {            cout<<arr[positive]<<" ";            positive=next_positive(arr,positive,n);            flag1=false;            flag2=true;            continue;        }                if( arr[negative]<0 and flag2 )        {            cout<<arr[negative]<<" ";            negative=next_negative(arr,negative,n);            flag1=true;            flag2=false;        }                    }    if(negative==n)    {        for(int i=positive;i<n;i++)        {            if(arr[i]>0)            cout<<arr[i]<<" ";        }    }         if(positive==n)    {        for(int i=negative;i<n;i++)        {            if(arr[i]>0)            cout<<arr[i]<<" ";        }    }}}; 
+1
krushnaoza2 weeks ago
Space Complexity: O(1) ; Time Complexity: O(n^2)

 public void rotateright(int[] A, int i, int j){
        int tmp = A[j];
        for(int k=j; k>i; k--){
            A[k] = A[k-1];
        }
        A[i]=tmp;
    }
  void rearrange(int A[], int n) {
        for(int i=0; i<n; i++){
            if((i&1)==1 && A[i]>=0){
                int j=i+1;
                while(j<n && A[j]>=0) j++;

                if(j==n) break;

                rotateright(A,i,j);
                
            } else if(i%2==0 && A[i]<0) {
                int j=i+1;
                while(j<n && A[j]<0) j++;

                if(j==n) break;
                rotateright(A,i,j);
                
            }
        }
    }
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.