Welcome to LeetCode Discuss.  Please read the FAQ to help yourself making the best use of Discuss.
Ask a Question
Back to Problem

Welcome to LeetCode Discuss.

This is a place to ask questions related to only OJ problems.

Please read the FAQ to help yourself making the best use of Discuss.

Can it be done without creating a new merged array, meaning sorting using only B and A?

0 votes
711 views

Can it be done without creating a new merged array, meaning sorting using only B and A?

asked Dec 7, 2013 in Merge Sorted Array by alik (260 points)

That was easy if you think out of the box - in reverse :)

Yet another solution that merges from the end of A. Maybe this is easier to understand:

{{

class Solution {

public:

void merge(int A[], int m, int B[], int n) {

    // Can be done this way also:
    //   copy( B, B + n, A + m);
    //   sort(A, A + m + n);

    // Merge elements inplace starting from the end of A[]:
    int *write_it = A + m + n - 1;
    int *read_a = A + m - 1;
    int *read_b = B + n - 1;
    while (read_a >= A && read_b >= B) {

        int a = *read_a;
        int b = *read_b;

        if (a > b) {

            (*write_it) = a;
            read_a--;
        }
        else {

            (*write_it) = b;
            read_b--;
        }

        write_it--;
    }

    // If there some extra element in B yet to be moved:        
    if (read_b >= B) {

        copy(B, read_b + 1, A);
    }

}

};

}}

6 Answers

+8 votes
 
Best answer

Yes, you are suppose to merge B into A without using any extra space.

answered Dec 7, 2013 by 1337c0d3r (11,410 points)
selected Dec 8, 2013 by alik
+7 votes

yes, you can do this at runtime:

class Solution {
public:
void merge(int A[], int m, int B[], int n) {
    for (int idx = m + n - 1; idx >= 0; idx--) {
        if (m <= 0) {
            A[idx] = B[--n]; 
        } else if (n <= 0) {
            break;
        } else if (A[m-1] < B[n-1]) {
            A[idx] = B[--n];
        } else {
            A[idx] = A[--m];
        }
    }
}
};
answered Feb 10 by raywill (510 points)
+1 vote

Yes, Try solve it by fill proper int to array A's tail first.

answered Feb 13 by dq (240 points)
0 votes

of course, you can just copy B into A, from end to the start

if you're familiar with STL, you can refer to the method of rbegin iterator

answered Jan 7 by h4breeze (450 points)
0 votes
class Solution {
public:
    void merge(int A[], int m, int B[], int n) {

        auto posA = m - 1;
        auto posB = n - 1;
        auto posWrite =  n + m - 1;

        while (posB >= 0 && posA >= 0) {
            if (B[posB] < A[posA])
                A[posWrite--] = A[posA--];
            else
                A[posWrite--] = B[posB--];
        }

        for (; posB >= 0; --posWrite, --posB)
            A[posWrite] = B[posB];
    }
};
answered Feb 22 by ether (340 points)

Answers you've written should describe your code with more detail content. Please read the FAQ (http://oj.leetcode.com/discuss/faq) for more info.

0 votes
public void merge(int A[], int m, int B[], int n) {
        int index=m+n-1;
        for(int i=m-1,j=n-1;index>=0;index--) {
            if(i>=0&&j>=0) {
                if(B[j]>A[i]) 
                    A[index] =B[j--];
                else 
                    A[index] =A[i--];
            } else {
                if(j>i) 
                    A[index] =B[j--];
                else 
                    A[index] =A[i--];

            }
        } 
}
answered Mar 29 by JohnWeiGitHub (720 points)

...