Run time error for a seemingly fine code


  • 0
    P

    I tested the code below and it works on local machine, for input {2,2,2} ,2 , it returns length=0 and none of the numbers in the array is counted as remaining. However when I submit I get run time error for input
    {2,2,2}, 2 , does anyone know where it went wrong?

     int removeElement(int A[], int n, int elem){
         int length=n;
         int i=0;
         while(i<length){
          if(A[i]==elem){
            for(int j=i;j<length;j++){
              int temp=A[j];
              A[j]=A[j+1];
              A[j+1]=temp;
            }
            length--;
            cout<<length<<endl;
          }
          else i++;
        }
        return length;
      }

Log in to reply
 

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