Challenge: Merge Two Sorted Arrays

Given two sorted arrays, merge them into one array, which should also be sorted. Implement the solution in Java and see if your code runs successfully!

Problem Statement #

In this problem, given two sorted arrays, you have to implement the int[] mergeArrays(int[] arr1, int[] arr2) method, which returns an array consisting of all elements of both arrays in a sorted way.

Method Prototype #

int[] mergeArrays(int[] arr1, int[] arr2)

Here arr1 and arr2 are sorted already.

Output #

Merged array consisting of all elements of both arrays in a sorted way.

Sample Input #

arr1 = {1, 3, 4, 5}  
arr2 = {2, 6, 7, 8}

Sample Output #

arr = {1, 2, 3, 4, 5, 6, 7, 8}

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.