Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Merge Sorted Array

XPChallenge Points: 10
levelLevel: Easy

You are given two sorted arrays list1 and list2, along with their lengths m and n.
Your task is to merge them into a single sorted array inside list1.


• list1 has enough space to hold all elements (size m + n).
• Only the first m elements of list1 are valid; the remaining positions are 0 and act as empty slots.
• list2 has n valid elements.

Example 1:

Input: list1 = [2,4,7,0,0,0], m = 3 list2 = [1,3,6], n = 3

Output: [1,2,3,4,6,7]

Explanation:

Merge [2,4,7] and [1,3,6] into [1,2,3,4,6,7].

Example 2:

Input: list1 = [5], m = 1 list2 = [], n = 0

Output: [5]

Explanation:

Nothing to merge; result is [5].

Example 3:

Input: list1 = [0], m = 0 list2 = [8], n = 1

Output: [8]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result