algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
2 shown · 0/261 solved

Merge Two Sorted Arrays

BronzeCommunity Beta
Asked atTata Consultancy Services
Solve problem →
2000ms256MBAdded Sep 6, 2026

You are given two integer arrays a and b, each sorted in non-decreasing order.

Return a single array containing all elements of a and b, in non-decreasing order. Duplicates are kept, so the result has exactly a.length + b.length elements.

Examples

Example 1

Input: a = [1, 3, 5, 7], b = [2, 4, 6, 8]
Output: [1, 2, 3, 4, 5, 6, 7, 8]
Explanation: The elements of the two arrays interleave perfectly.

Example 2

Input: a = [2, 2, 4], b = [1, 2, 9]
Output: [1, 2, 2, 2, 4, 9]
Explanation: All three 2s appear in the result.

Constraints

  • 1≤1 \leq1≤ a.length, b.length ≤104\leq 10^4≤104
  • −109≤-10^9 \leq−109≤ a[i], b[i] ≤109\leq 10^9≤109
  • a and b are each sorted in non-decreasing order.

Details

Solved by7 people
Time limit2000ms
Memory256MB
AddedSep 6, 20265 days ago