Problem · Array

Merge 2 Arrays

EasyOracleFULLTIMEOA

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

Examples
01 · Example 1
a = [1, 2, 3]
b = [2, 5, 5]
return = [1, 2, 2, 3, 5, 5]
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
  • 1 < n <= 10^5
  • 0 <= a[i], b[i] <= 10^9 where 0 <= i < n
More Oracle problems
drafts saved locally
public int[] mergeArrays(int[] a, int[] b) {
    // write your code here
}
a[1, 2, 3]
b[2, 5, 5]
expected[1, 2, 2, 3, 5, 5]
sign in to submit