The task is to connect 2 arrays (sorted) into 1, and in such a way that it is immediately sorted. That is, if arrays {0, 2} and {1, 3} are fed to the input, then the output should be an array {0, 1, 2, 3}
public static int[] mergeArrays(int[] a1, int[] a2) { if (a1 == null) return a2; if (a2 == null) return a1; int[] r = new int[a1.length + a2.length]; for (int i = 0, a1Index = 0, a2Index = 0; i < r.length; ) { while (a1Index < a1.length && a1[a1Index] < a2[a2Index]) { r[i] = a1[a1Index]; a1Index++; i++; } r[i] = a2[a2Index]; a2Index++; i++; } return r; }
For some reason, I vyprigivaet of the array.