Move all negative elements to end | Practice | GeeksforGeeks

Given an unsorted array arr[] of size N having both negative and positive integers. The task is place all negative element at the end of array without changing the order of positive element and negative element.
 
Example 1:
Input : 
N = 8
arr[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output : 
1  3  2  11  6  -1  -7  -5

Example 2:
Input : 
N=8
arr[] = {-5, 7, -3, -4, 9, 10, -1, 11}
Output :
7  9  10  11  -5  -3  -4  -1

 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function segregateElements() which takes the array arr[] and its size N as inputs and store the answer in the array arr[] itself.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
 

Constraints:
1 ≤ N ≤ 105
-105  ≤ arr[] ≤ 105
0
fernweh00Premium11 hours ago
C++ Solution 
void segregateElements(int arr[],int n)
   {
       // Your code goes here
       vector<int> v;
       
       for(int i=0;i<n;i++){
           if(arr[i]>0){
               v.push_back(arr[i]);
           }
       }
       for(int i = 0;i<n;i++){
           
           if(arr[i]<0){
               v.push_back(arr[i]);
           }
           arr[i] = v[i];
       }
       
      
   }
0
kumarr320312 hours ago
//c++ solution
   void segregateElements(int a[],int n)
    {
        // Your code goes here
        vector<int>pos,neg;
        
        for(int i=0;i<n;i++)
        {
            if(a[i]>=0)
            {
                pos.push_back(a[i]);
            }
            else
            {
                neg.push_back(a[i]);
            }
        }
        int temp=0;
        for(int i=0;i<pos.size();i++)
        {
            a[temp++]=pos[i];
        }
        for(int i=0;i<neg.size();i++)
        {
            a[temp++]=neg[i];
        }
    }
0
krativishnoi904512 hours ago
“Main idea is to makes in the given array itself”
       vector<int>pos;       vector<int>neg;                     for(int i=0;i<n;i++)       {           if(arr[i]>0)           {               pos.push_back(arr[i]);           }           else           {               neg.push_back(arr[i]);           }       }             pos.insert(pos.end(),neg.begin(),neg.end());       copy(pos.begin(),pos.end(),arr);          }
 
 
-1
aakash_s_182 days ago
class Solution:   def segregateElements(self, arr, n):       arr1 = []       arr2 = []       for i in arr :           if i > 0 :               arr1.append(i)           else :               arr2.append(i)       return (arr1+arr2)        
is working correct in vs code but in GFG its showing wring input.
0
shridhardaga3 days ago
Java Solution using stream: 
 
public void segregateElements(int arr[], int n)
    {
        // Your code goes here
        int[] positive = Arrays.stream(arr)
                .filter(num -> num > 0)
                .toArray();

        int[] negative = Arrays.stream(arr)
                .filter(num -> num < 0)
                .toArray();
        
        int i = 0, j = 0;
        
        while(i < positive.length){
            arr[i] = positive[i];
            i++;
        }
        
        while(i < n){
            arr[i] = negative[j];
            i++; j++;
        }
    }
0
peramsaicharanyadav993 days ago
Java Solution
public void segregateElements(int arr[], int n)   {        int pos[]=new int[n];       int neg[]=new int[n];       for(int i=0;i<n;i++){           if(arr[i]>0)pos[i]=arr[i];           else neg[i]=arr[i];       }       int radpos[]=new int[n];       int radneg[]=new int[n];       int rp=0;       int rn=0;       for(int i=0;i<n;i++){           if(pos[i]!=0)           {radpos[rp]=pos[i];           rp++;}       }        for(int i=0;i<n;i++){           if(neg[i]!=0)           {radneg[rn]=neg[i];           rn++;}       }       int m=0;       for(int i=0;i<n;i++){                      if(radpos[i]==0){radpos[i]=radneg[m];               m++;           }       }     for(int i=0;i<n;i++){         arr[i]=radpos[i];     }   }
0
tonroyjptt6 days ago
Java solution: Using ArrayList
 ArrayList<Integer> pos = new ArrayList<>();       ArrayList<Integer> neg = new ArrayList<>();              for(int i=0; i<n; i++)       {           if(arr[i] > 0)           {               pos.add(arr[i]);           }           else           {               neg.add(arr[i]);           }       }       int size1 = pos.size();       int size2 = neg.size();       int a = 0;              for(int i=0; i<size1; i++)       {           arr[a] = pos.get(i);           a++;       }       for(int i=0; i<size2; i++)       {           arr[a] = neg.get(i);           a++;       }
0
sanskritisrivastava201916 days ago
Java solution:
public void segregateElements(int arr[], int n)
   {
       int[] res = new int[n];
       int idx=0;
       //first add all positive numbers
       for(int i=0;i<n;i++){
           if(arr[i]>=0){
               res[idx]=arr[i];
               idx++;
           }
       }
       //then add all neg numbers
       for(int i=0;i<n;i++){
           if(arr[i]<0){
               res[idx]=arr[i];
               idx++;
           }
       }
       //copy all the el from res to arr
       int i=0;
       for(int el:res){
           arr[i]=el;
           i++;
       }
   }
+1
varunchourasiya456 days ago
void segregateElements(int arr[],int n)
    {
      vector<int> v1;
      vector<int> v2;
      for(int i=0;i<n;i++){
          if(arr[i]<0){
              v1.push_back(arr[i]);
          }else{
              v2.push_back(arr[i]);
          }
      }
      int j = v2.size();
      for(int i=0;i<v2.size();i++){
          arr[i] = v2[i];
      }for(int i=0;i<v1.size();i++){
          arr[i+j] = v1[i];
          
      }
    }
+1
ms78739666 days ago
vector<int> temp;       for(int i=0;i<n;i++){           if(arr[i]>0)           temp.push_back(arr[i]);       }           // int u=temp.size();   for(int i=0; i<n;i++){       if(arr[i]<0)           temp.push_back(arr[i]);   }      for(int i=0;i<temp.size();i++){       arr[i]=temp[i];   }
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.