Merge Sorted Array

To solve this coding challenge, we need to merge two sorted arrays,
nums1
and
nums2
, into one sorted array in-place within
nums1
. The array
nums1
has enough space to accommodate all elements from both arrays, with unused slots filled with zeros. Here's an approach and methodology to achieve this in an efficient manner.

Explanation

The challenge requires us to merge two sorted arrays in a way that the result is stored inside the first array (
nums1
). The final merged array should be sorted in non-decreasing order. Key points include:
  • nums1
    has additional space (filled with zeros) to accommodate all elements from both arrays.
  • We need to consider the valid elements both in
    nums1
    and
    nums2
    .
  • We will do this in-place and aim for O(m + n) time complexity, which is efficient for merging sorted arrays.

Approach

  1. Three Pointers Technique : We'll use three pointers:
    • p1
      to track the end of the valid elements in
      nums1
      .
    • p2
      to track the end of
      nums2
      .
    • p
      to track the position in
      nums1
      where the next largest element should be placed.
  2. Comparing from the End : Starting from the end of both arrays, compare elements from
    nums1
    and
    nums2
    :
    • If the element in
      nums1
      is greater, place it at the current position indicated by
      p
      and move
      p1
      one step back.
    • Otherwise, place the element from
      nums2
      at the current position and move
      p2
      one step back.
    • Always move the pointer
      p
      back after placing an element.
  3. Remaining Elements : If any elements are left in
    nums2
    after we have exhausted all elements in
    nums1
    , copy them over to
    nums1
    .
  4. This approach ensures we always place the largest remaining element at the end of the combined space, ensuring sorted order.

    Detailed Steps in Pseudocode

  5. Initialize three pointers:
    p1 = m - 1
    ,
    p2 = n - 1
    , and
    p = m + n - 1
    .
  6. While both
    p1
    and
    p2
    are valid (i.e., not below zero):
    • Compare
      nums1[p1]
      and
      nums2[p2]
      .
    • Place the larger element at
      nums1[p]
      .
    • Move the respective pointer (
      p1
      or
      p2
      ) back.
    • Move pointer
      p
      back by one.
  7. If any elements remain in
    nums2
    after
    p1
    is exhausted, copy them to the beginning of
    nums1
    .

Pseudocode with Comments

                                            
# Initialize pointers to the end of the respective regions in nums1 and nums2
p1 = m - 1   # Pointer to end of valid elements in nums1
p2 = n - 1   # Pointer to end of nums2
p = m + n - 1  # Pointer to the end of merged array in nums1

# Merge the arrays starting from the end
while p1 >= 0 and p2 >= 0:
    if nums1[p1] > nums2[p2]:
        nums1[p] = nums1[p1]  # Place the larger element at the end position
        p1 -= 1  # Move pointer p1 back
    else:
        nums1[p] = nums2[p2]  # Place the larger element at the end position
        p2 -= 1  # Move pointer p2 back
    p -= 1  # Move pointer p back

# If nums2 has remaining elements, copy them over to nums1
if p2 >= 0:
    nums1[0:p2+1] = nums2[0:p2+1]  # Copy remaining elements of nums2

                                        
Here, we ensure all elements are compared and placed in the correct position, starting from the greatest values, which helps achieve the merge efficiently without needing additional space. This method takes O(m + n) time, ensuring an efficient solution to the problem.