> For the complete documentation index, see [llms.txt](https://joshualbarb.gitbook.io/leetcode-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://joshualbarb.gitbook.io/leetcode-problems/two-pointers/merge-two-sorted-arrays.md).

# Merge Two Sorted Arrays

Two Pointers

## Problem

Given two sorted integer arrays A and B, merge B into A as one sorted array.

{% hint style="info" %}
For example:

```
Input : 
         nums1 = [1 5 8]
         nums2 = [6 9]

Output: [1 5 6 8 9]
```

{% endhint %}

## Solution

```
def mergeH(nums1, nums2):
    m = len(nums1)
    n = len(nums2)
    i=len(nums1)-1
    j=len(nums2)-1
    C = [0 for p in range(m+n)]
    k=len(C)-1
    while i>-1 and j>-1:
        if nums1[i]>nums2[j]:
            C[k]=nums1[i]
            i-=1
        else:
            C[k]=nums2[j]
            j-=1
        k-=1
    while i>-1:
        C[k]=nums1[i]
        i-=1
        k-=1
    while j>-1:
        C[k]=nums2[j]
        j-=1
        k-=1
            
    return C

```
