FastPrepMerge Two Sorted Arrays
Problem · Array

Merge Two Sorted Arrays

Learn this problem
EasyFlexTrade logoFlexTradeFULLTIMEOA

Problem statement

Given two integer arrays first and second, each sorted in non-decreasing order, return a new array containing every value from both inputs in non-decreasing order.

Retain every occurrence of a value, including duplicates within one input or across both inputs.

Function

mergeSortedArrays(first: int[], second: int[]) → int[]

Examples

Example 1

first = [1,2,4]second = [1,3,4]return = [1,1,2,3,4,4]

The two values equal to 1 and the two values equal to 4 are all retained in sorted order.

Example 2

first = []second = [2,2]return = [2,2]

The first array is empty, so the result contains both values from second.

Example 3

first = [-5,-1,0]second = [-3,2]return = [-5,-3,-1,0,2]

Values from the two arrays alternate in the merged ordering.

Constraints

  • 0 <= first.length <= 200000
  • 0 <= second.length <= 200000
  • Every value is a signed 32-bit integer.
  • Both input arrays are sorted in non-decreasing order.

More FlexTrade problems

drafts saved locally
public int[] mergeSortedArrays(int[] first, int[] second) {
  // Write your code here.
}
first[1,2,4]
second[1,3,4]
expected[1,1,2,3,4,4]
checking account