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
tanashah3 days ago
        vector<int>v1,v2;
        for(int i=0;i<n;i++){
            if(arr[i]>0){
                v1.push_back(arr[i]);
            }else{
                 v2.push_back(arr[i]);
            }
        }
        for(int i=0;i<v2.size();i++){
            v1.push_back(v2[i]);
        }
        for(int i=0;i<n;i++){
            arr[i]=v1[i];
        }
0
niravramotiya0035 days ago
def segregateElements(self, arr, n):
       arr1,arr2=[],[]
       for i in arr:
           if i<0:
               arr1.append(i)
           else:
               arr2.append(i)
       arr2.extend(arr1)
       arr1.clear()
       for i in range(n):
           arr[i]=arr2[i]
       arr2.clear()
0
shividee101 week ago
class Solution{   segregateElements(arr,n){       let newArrNegative = [];       let newArrPositive = [];       for(let i=0; i<n; i++) {           if(arr[i] < 0) {               newArrNegative.push(arr[i]);           } else {               newArrPositive.push(arr[i]);           }       }       arr = [];       console.log(arr.concat(newArrPositive,newArrNegative));
       return arr.concat(newArrPositive,newArrNegative)   }}Why is console.log priniting the correct value but return printing the same input array?
0
mannukhurana103971 week ago
Java
 
 public void segregateElements(int arr[], int n)
    {
        // 1 -1 3 2 -7 -5 11 6
        
        // space : 0
        // time : O(N*logN)
        
        // for(int i=1; i<n; i++){ // n
        //     if(arr[i] >0){
        //         int j=i-1; //6
        //         while(j>=0 &&  arr[j]<0) // 1, n-1
        //             arr[j+1] = arr[j--];
        //         arr[++j] = temp;
        //     }
            
        // }
    //------------------------------------------------------------- 
        // space : O(N)
        // time: O(2N)
        
        List<Integer> pos = new LinkedList<Integer>(); 
        List<Integer> neg = new LinkedList<Integer>();
        for(int i=0;i<n;i++){
            if(arr[i]>0) pos.add(arr[i]);
            else neg.add(arr[i]);
        }
        
        int i=0;
        for(int j: pos)
            arr[i++] = j;
        for(int j: neg)
            arr[i++] = j;
        
        
        
    }
0
iamprakhars2 weeks ago
void segregateElements(int arr[],int n)   {       vector<int>temp;       for(int i=0; i<n; i++){           if(arr[i]<0)temp.push_back(arr[i]);       }       int idx=0;       for(int i=0; i<n; i++){           if(arr[i]<0)continue;           arr[idx++]=arr[i];       }       for(int i=idx; i<n; i++){           arr[i]=temp[i-idx];       }   }
0
yadavbanti4952 weeks ago
void segregateElements(int arr[],int n)   {      int count1=0,arr1[n];      for(int i=0;i<n;i++)      {          if(arr[i]>0)          {              count1++;          }      }      for(int i=0,j=0;i<n;i++)      {          if(arr[i]>0)           {               arr1[j]=arr[i];               j++;           }           else{               arr1[count1]=arr[i];               count1++;           }      }      for(int i=0;i<n;i++)      {          arr[i]=arr1[i];      }   }
0
lankelokeshkumar2 weeks ago
 void segregateElements(int arr[],int n)   {       int cnt=0,temp[n];       for(int i=0;i<n;i++) {           if(arr[i]>0)  cnt++;       }       for(int i=0,j=0;i<n;i++) {           if(arr[i]>0)  {  temp[j]=arr[i];      j++;     }           else          {  temp[cnt]=arr[i];  cnt++;   }       }        for(int i=0;i<n;i++)    { arr[i]=temp[i];  }   }
0
suprithsk20012 weeks ago
void segregateElements(int arr[],int n)   {       // Your code goes here       vector<int> ans1;       vector<int> ans2;       for(int i=0;i<n;i++){           if(arr[i]<0){               ans2.push_back(arr[i]);           }           else{               ans1.push_back(arr[i]);           }       }       for(int i=0;i<ans1.size();i++){           arr[i]=ans1[i];       }       int k=ans1.size();       for(int i=0;i<ans2.size() && k<n;i++){           arr[k]=ans2[i];           k++;       }   }
+1
nk01879122 weeks ago
//Simple Two pointer java solution
class Solution {      public void segregateElements(int arr[], int n){              if(arr.length <= 1)       return;              int start = 0;       int end = 0;              ArrayList<Integer> list = new ArrayList<>();              while(end < arr.length){                      if(arr[end] > -1){               arr[start++] = arr[end];           }           else               list.add(arr[end]);           end++;       }              end = 0;              while(start < arr.length){           arr[start++] = list.get(end++);       }          }} 
0
bhaskarparadox2 weeks ago
C++ Solution
    void segregateElements(int arr[],int n)
    {
        vector<int> v;
        int k = 0;
        int i = 0;
        for(int i=0; i<n; i++){
            if(arr[i]>=0){
                arr[k++] = arr[i];
            }
            else{
                v.push_back(arr[i]);
            }
        }
        i = 0;
        while(k<n){
            arr[k++] = v[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.