0

The question description is as such:

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.

The leetcode gives an example case:

Input: [0] 0 [1] 1 Expected: [1]

I don't understand that. Shouldn't the answer be [0,1]? Since 0 and 1 are all inserted. Could anyone explain that?

Here is my code:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {

        int i = 0;
        int j = 0;
        int index = 0;
        int []result = new int[nums1.length + nums2.length];

        if (nums1 == null || nums2 == null) {
            result = null;
        }

        while (i < nums1.length && j < nums2.length) {
            if (nums1[i] < nums2[j]) {
                result[index++] = nums1[i++];
            }
            else {
                result[index++] = nums2[j++];
            }
        }

        while (i < nums1.length) {
            result[index++] = nums1[i++];
        }
        while (j < nums2.length) {
            result[index++] = nums2[j++];
        }

    }
}
6
  • The elements in the array. Commented Nov 29, 2017 at 14:18
  • Are there any other examples? I suspect m is the number of elements in nums1 to be merged, and n for nums2. So it's actually merging [] and [1], but shows nums1 as [0] to show it's large enough to store the result Commented Nov 29, 2017 at 14:20
  • Also the code you posted is a good way to merge two arrays, except the result is thrown away at the end and it isn't what the question is asking for. It's asking for the result to placed inside the first input array (you can copy all of the elements from result to nums1) Commented Nov 29, 2017 at 14:22
  • I don't think your understanding of input [0] 0 [1] 1 is correct. When it says it has 0 elements, the input is [] 0 [1] 1. problem doesnt mention that the first array contains a number 0 in it. Hence the output as [1] Commented Nov 29, 2017 at 18:48
  • U R so right...:) Commented Nov 30, 2017 at 2:02

4 Answers 4

1

Input: [0] 0 [1] 1 Expected: [1]

The 0 is m and the 1 is n where m is the number of elements in the first array and n is the number of elements in the second array.

Thus, the first array does not contain the element 0. In fact, it contains no elements at all (0). The [0] in the first array is actually a placeholder for the elements to be merged (since the size of the first array is supposed to be (m+n). So merging them would give an output [1] which is just the element of the second array merged into the first.

Here is my code for the problem:

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int mPtr = m - 1;
        int nPtr = n - 1;
        int index = nums1.length - 1;
        while (nPtr >= 0) {
            if (mPtr >= 0 && nums1[mPtr] >= nums2[nPtr]) {
                nums1[index--] = nums1[mPtr--];
            } else {
                nums1[index--] = nums2[nPtr--];
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

The number of elements initialized in nums1 and nums2 are m and n respectively. You may assume that nums1 has enough space (size that is equal to m + n) to hold additional elements from nums2.

I'm posting solution in Python.

Please find the code.

Analysis- Just check for zeros in first list and then replace with element in second list. If either of the list is empty, just print list 1.

def merge(nums1, m, nums2, n):

a=len(nums1)
i=0
j=0
while i< a:
    if n  == 0 or m==0 :
        return nums1
    if nums1[i]==0:
        nums1[i]=nums2[j]
        j+=1
    i+=1
return sorted(nums1)

Comments

0

I have the solution in c++

class Solution {
public:
    void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {        
    int i = m - 1;
    int j = n - 1;
    int k = m + n - 1;

    while (i >= 0 && j >= 0) {
        if (nums1[i] > nums2[j])
            nums1[k--] = nums1[i--];
        else
            nums1[k--] = nums2[j--];
    }

    while (j >= 0)
        nums1[k--] = nums2[j--];
}
    
        
    
};

Comments

0

Basically it uses divide and conquer algorithm that repeatedly splits an array into halves, sorts each half, and then merges the sorted halves into a single sorted array. Make sure the formula for finding mid is MId=STARTINDEX + (ENDINDEX - STARTINDEX)/2 it cause space complexity

import java.util.*
public class main {

    public static void mergeSort(int[] arr, int left, int right) {
        if (left < right) {
            int mid = left + (right - left) / 2;
            mergeSort(arr, left, mid);
            mergeSort(arr, mid + 1, right);
            merge(arr, left, mid, right);
        }
    }

    public static void merge(int[] arr, int left, int mid, int right) {
        int n1 = mid - left + 1;
        int n2 = right - mid;

        int[] L = new int[n1];
        int[] R = new int[n2];

        for (int i = 0; i < n1; i++)
            L[i] = arr[left + i];
        for (int j = 0; j < n2; j++)
            R[j] = arr[mid + 1 + j];

        int i = 0, j = 0, k = left;

        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }

        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    public static void printArray(int[] arr) {
        for (int num : arr)
            System.out.print(num + " ");
        System.out.println();
    }

    public static void main(String[] args) {
        int[] arr = { 12, 11, 13, 5, 6, 7 };
        System.out.println("Given Array:");
        printArray(arr);

        mergeSort(arr, 0, arr.length - 1);

        System.out.println("Sorted Array:");
        printArray(arr);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.