Help with the constraint and index out of bound exception

Merge sort is not an inplace sorting algorithm, i.e it returns a new sorted array rather than sorting the original array. Similarly, when you are implementing merge2SortedArrays, you are keeping the merged array in a temp array but you are not returning that to your function. So your code will not work. Take this as a hint, that keep the return types of both functions as arrays and try to make necessary changes to your code. If you are not able to do so, then let me know and I’ll give more hints.