Merge Sorted Array
Try to solve the Merge Sorted Array problem.
We'll cover the following
Statement#
Given two sorted integer arrays, and , and the number of data elements in each array, and , implement a function that merges the second array into the first one. You have to modify in place.
Note: Assume that has a size equal to , meaning it has enough space to hold additional elements from .
Constraints:
nums1.lengthnums2.length-
nums1[i],nums2[j]
Example#
The inputs will be two integer arrays and two integers representing the number of data elements in each array.
1 of 2
2 of 2
The zeroes at the end of represent uninitialized integers. This additional space will be used to merge it with .
Understand the problem#
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Merge Sorted Array
What is the output if the following arrays and values of and are given as input?
nums1 = [1, 2, 7, 92, 0, 0, 0], m = 4
nums2 = [3, 4, 5], n = 3
[1, 2, 3, 4, 5, 7, 92]
[1, 2, 7, 92, 3, 4, 5]
[3, 4, 5, 1, 2, 7, 92]
Figure it out!#
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself#
Implement your solution in main.py in the following coding playground. We have provided useful code templates in the other file that you may build on to solve this problem.
K-way Merge: Introduction
Solution: Merge Sorted Array