Positive and negative elements | Practice | GeeksforGeeks

Given an array arr[ ] containing equal number of positive and negative elements, arrange the array such that every positive element is followed by a negative element.
Note- The relative order of positive and negative numbers should be maintained.
Example 1:
Input:
N = 6
arr[] = {-1, 2, -3, 4, -5, 6}
Output:  
2 -1 4 -3 6 -5
Explanation: Positive numbers in order 
are 2, 4 and 6. Negative numbers in order 
are -1, -3 and -5. So the arrangement we 
get is 2, -1, 4, -3, 6 and -5.

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

Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function arranged() that takes array arr[ ] and its size integer N as parameters and returns the required array.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 106
1 ≤ arr[i] ≤ 109
0
mayank20212 weeks ago
C++ : 0.28/1.6 vector<int> arranged(int a[],int n)   {       vector<int> vec;       int b[n], i=0, j=n-1;              for(int k=0; k<n ; k++)        {            if(a[k] >0) b[i++]=a[k];            else                b[j--]=a[k];        }       i=0, j=n-1;       while(i<j)        {            vec.push_back(b[i++]);            vec.push_back(b[j--]);           }        return vec;   }
0
arvind16yadav4 weeks ago
/*JAVA SOLUTION*/
class Solution{   ArrayList<Long> arranged(long a[], int n)   {       long b[] = new long[n];              int positive_counter = 0;       int negative_counter = 1;              for(long x: a){           if(x >= 0){               b[positive_counter] = x;               positive_counter += 2;           }           else{               b[negative_counter] = x;               negative_counter += 2;           }       }              ArrayList<Long> ans = new ArrayList<>();       for(long y : b){           ans.add(y);       }              return ans;        
   }} 
0
priyaranjandash1 month ago
class Solution{   ArrayList<Long> arranged(long a[], int n)   {     ArrayList<Long> list = new ArrayList<Long>(n);     ArrayList<Long> positive = new ArrayList<Long>(n/2);     ArrayList<Long> negative = new ArrayList<Long>(n/2);//         long negative[] = new long[n/2];//         long positive[] = new long[n/2];        for(int i=0;i<n;i++){            if(a[i] > 0)               positive.add(a[i]);              else                negative.add(a[i]);        }                 for(int i=0, j=0;i<n/2 && j<n/2;i++,j++){             list.add(positive.get(i));             list.add(negative.get(j));         }            return list;
   }}
+1
hasnainraza1998hr1 month ago
vector<int> arranged(int a[],int n)   {       vector<int> v(n);       for(int i=0,pIndex=0,nIndex=1;i<n;i++){           if(a[i]>0){               v[pIndex]=a[i];               pIndex += 2;           }           else{               v[nIndex]=a[i];               nIndex += 2;           }       }       return v;   }
0
coderaky2 months ago
coderaky.com
vector<int> arranged(int a[],int n)   {       vector<int>res(n,0);       for(int i=0,e=0,o=1;i<n;i++){           if(a[i]>0)res[e]=a[i],e+=2;           else res[o]=a[i],o+=2;       }       return res;   }
0
dangrio2 months ago
vector<int> arranged(int a[],int n)   {       vector<int> vec(n);       int ind = 0;       for(int i=0;i<n;i++){           if(a[i]>0){               vec[ind] = a[i];               ind += 2;           }       }       ind = 1;       for(int j=0;j<n;j++){           if(a[j]<0){               vec[ind] = a[j];               ind += 2;           }       }       return vec;   }
0
codersheaven693 months ago
Python Solution
 
 positive =[]       negative = []       new_array = []       for i in range(n):           if a[i] >= 0:               positive.append(a[i])               # return positive           else:               negative.append(a[i])               # return negative       # print(positive)       # print(negative)              for i in range(n//2):           new_array.append(positive[i])           new_array.append(negative[i])              return new_array
0
abhishekworld4 months ago
vector<int> arranged(int arr[],int n)   {           vector<int> output;                      bool flag = true;           int i = 0;           int j = 0;                      while(i<n ||  j<n){                              if(flag){                   if(arr[i]>0){                       output.push_back(arr[i]);                       flag = false;                   }                   i++;               }               else{                   if(arr[j]<0){                       output.push_back(arr[j]);                       flag = true;                   }                   j++;               }                          }           return output;                         }
+1
sr52465796 months ago
class Solution:   def arranged(self,a,n):       #Code here       neg = []       pos = []       for i in a:           if i<0:               neg.append(i)           else:               pos.append(i)       s=[]       for i in range(n//2):           s.append(pos[i])           s.append(neg[i])       return s
+2
badgujarsachin836 months ago
 vector<int> arranged(int a[],int n)
    {
        vector<int> aa,b,c;
        for(int i=0;i<n;i++){
            if(a[i]<0){
                aa.push_back(a[i]);
            }
            if(a[i]>0){
                b.push_back(a[i]);
            }
        }
        for(int i=0;i<n;i++){
            c.push_back(b[i]);
            c.push_back(aa[i]);
        }
        return c;
    }
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.