2ms solution in C++


  • 0
    R
    int removeElement(int A[], int n, int elem) {
        int i,up=0,down=n-1,count=0;
        
        while(up<down)
        {
            if(A[up]==elem )
            {   if(A[down]==elem)
               { while(A[down]==elem && up<down)
            {
                down--;
            }}
            
            int temp=A[up];
            A[up]=A[down];
            A[down]=temp;
            up++;
            down--;
            }
            else
            up++;
            
        }
        for(i=0;i<n;i++)
        {
            if(A[i]==elem)
            count++;
        }
       
        n=n-count;
        return n;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.