Guys could you please tell me what's wrong with this code ?


  • 0
    N
    public void merge(int A[], int m, int B[], int n)
    

    {

     int i = m-1;
    
     int j = n-1;
    
     
     for(int k = m+n-1; k>=0 ; k--)
     {
         if(i<0)
         {
             A[k] = A[j];
             k--;
             j--;
         }
         else if (j<0)
         {
             A[k] = A[i];
             k--;
             i--;
         }
         else
         {
             if (A[i]>=A[j])
             {
                  A[k] = A[i];
             k--;
             i--;
             }
             else if (A[i]<A[j])
             {
                 A[k] = A[j];
             k--;
             j--;
             }
         }
     
     
     
    }    
    

    }


  • 0
    D

    remove k--; inside the for loop


Log in to reply
 

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