# Is the input proper ?

• So, here is my approach

``````public class Solution {
public void merge(int A[], int m, int B[], int n) {

if(B.length==0)
return;

for(int i=0,j=0,k=0 ; i<A.length ; i++)
{
if(A[i]<=B[k])
continue;

else
{
j=A.length - B.length;

while(j>i)
{
A[j]=A[j-1];
j--;
}
A[i]=B[k];
k++;
}
}
}
}
``````

Now why are they giving [2], [1] as the input ? It says that the first array is big enough to hold both of them. Am i missing something obvious ? Are we supposed to create a new array. Come to think of it, I also dont see the use for variables m and n.

• The inputs imply m = 1 and n = 1. For the first array, they only list the part that needs to be sorted

• If you write the code like this.You could understand the usage of m and n. It helps to avoid moving data within the for loop.

``````class Solution {
public:
void merge(int A[], int m, int B[], int n) {
int i = m-1, j = n-1;
int cur = m+n-1;
while (i >= 0 && j >= 0)
{
if (A[i] > B[j])
{
A[cur--] = A[i--];
}
else
{
A[cur--] = B[j--];
}
}
while (j >= 0)
{
A[cur--] = B[j--];
}
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.