FastPrepFastPrep
Problem Brief

Merge 2 Arrays

FULLTIMEOA

Given two sorted arrays, merge them to form a single, sorted array with all items in non-decreasing order.

Function Description

Complete the function mergeArrays in the editor below.

mergeArrays has the following parameter(s):

  1. int a[n]: a sorted array of integers
  2. int b[n]: a sorted array of integers

Returns

int[n]: an array of all the elements from both input arrays in non-decreasing order

1Example 1

Input
a = [1, 2, 3], b = [2, 5, 5]
Output
[1, 2, 2, 3, 5, 5]
Explanation
Merge the arrays to create array c as follows: a[0] < b[0] -> c = [a[0]] = [1] a[1] < b[0] -> c = [a[0], b[0]] = [1, 2] a[1] < b[1] -> c = [a[0], b[0], a[1]] = [1, 2, 2] a[2] < b[1] -> c = [a[0], b[0], a[1], a[2]] = [1, 2, 2, 3] No more elements in a -> c = [a[0], b[0], a[1], a[2], b[1], b[2]] = [1, 2, 2, 3, 5, 5] Elements were alternately taken from the arrays in the order given, maintaining precedence.

Constraints

Limits and guarantees your solution can rely on.

  • 1 < n <= 10^5
  • 0 <= a[i], b[i] <= 10^9 where 0 <= i < n
public int[] mergeArrays(int[] a, int[] b) {
    // write your code here
}
Input

a

[1, 2, 3]

b

[2, 5, 5]

Output

[1, 2, 2, 3, 5, 5]

Sign in to submit your solution.