Merging Two Sorted Arrays
InterviewTime limit1.5sMemory limit256 MB
Merge two sorted arrays into one sorted sequence with a two-pointer walk.
- Level
Easy3 of 10
- Topics
- Two pointers, Array
- Solved
- No attempts yet
Problem
You are given two arrays and , each sorted in non-decreasing order. Write a program that combines all of their elements and prints them in non-decreasing order.
Input
The first line contains the size of array and the size of array , separated by a space. ()
The second line contains the elements of array , and the third line contains the elements of array , separated by spaces. Every element is an integer whose absolute value is at most , and both arrays are already sorted in non-decreasing order.
Output
On the first line, print the combined elements of both arrays in non-decreasing order, separated by single spaces. If a value appears several times, print it as many times as it appears.