#include <stdio.h>
int lSize = 4;
int rSize = 3;
int L[4] = {1, 3, 6, 7};
int R[4] = {1, 2, 4};
int finalArray[7];
void merge()
{
int lIndx = 0;
int rIndx = 0;
int i = 0;
for (i = 0; i < (lSize + rSize); i++) {
if (L[lIndx] <= R[rIndx]) {
finalArray[i] = L[lIndx++];
if (lIndx == lSize) {
break;
}
} else {
finalArray[i] = R[rIndx++];
if (rIndx == rSize) {
break;
}
}
}
i++;
if (lIndx != lSize) {
while(1) {
finalArray[i++] = L[lIndx++];
if (lIndx == lSize) {
return;
}
}
}
if (rIndx != rSize) {
while(1) {
finalArray[i++] = R[rIndx++];
if (rIndx == rSize) {
return;
}
}
}
}
void display()
{
int i = 0;
for (i = 0; i < lSize; i++) {
}
for (i = 0; i < rSize; i++) {
}
for (i = 0; i < lSize+rSize; i++) {
}
}
int main()
{
// Test 1
merge();
display();
// Test 2
R[0] = 1;
R[1] = 3;
R[2] = 6;
R[3] = 7;
L[0] = 1;
L[1] = 2;
L[2] = 4;
lSize = 3;
rSize = 4;
merge();
display();
// test 3
L[0] = 1;
R[0] = 1;
lSize = 1;
rSize = 1;
merge();
display();
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgppbnQgbFNpemUgPSA0OwppbnQgclNpemUgPSAzOwppbnQgTFs0XSA9IHsxLCAzLCA2LCA3fTsKaW50IFJbNF0gPSB7MSwgMiwgNH07CmludCBmaW5hbEFycmF5WzddOwoKdm9pZCBtZXJnZSgpCnsKICAgIGludCBsSW5keCA9IDA7CiAgICBpbnQgckluZHggPSAwOwogICAgaW50IGkgPSAwOwoKICAgIGZvciAoaSA9IDA7IGkgPCAobFNpemUgKyByU2l6ZSk7IGkrKykgewoJaWYgKExbbEluZHhdIDw9IFJbckluZHhdKSB7CgkgICAgZmluYWxBcnJheVtpXSA9IExbbEluZHgrK107CgkgICAgaWYgKGxJbmR4ID09IGxTaXplKSB7CgkJYnJlYWs7CgkgICAgfQoJfSBlbHNlIHsKCSAgICBmaW5hbEFycmF5W2ldID0gUltySW5keCsrXTsKCSAgICBpZiAockluZHggPT0gclNpemUpIHsKCQlicmVhazsKCSAgICB9Cgl9CiAgICB9CgogICAgaSsrOwogICAgaWYgKGxJbmR4ICE9IGxTaXplKSB7Cgl3aGlsZSgxKSB7CgkgICAgZmluYWxBcnJheVtpKytdID0gTFtsSW5keCsrXTsKCgkgICAgaWYgKGxJbmR4ID09IGxTaXplKSB7CgkJcmV0dXJuOwoJICAgIH0KCX0KICAgIH0KICAgIGlmIChySW5keCAhPSByU2l6ZSkgewoJd2hpbGUoMSkgewoJICAgIGZpbmFsQXJyYXlbaSsrXSA9IFJbckluZHgrK107CgoJICAgIGlmIChySW5keCA9PSByU2l6ZSkgewoJCXJldHVybjsKCSAgICB9Cgl9CiAgICB9Cn0KCnZvaWQgZGlzcGxheSgpCnsKICAgIGludCBpID0gMDsKCiAgICBwcmludGYoIlxuXG5BcnJheTE6ICIpOwogICAgZm9yIChpID0gMDsgaSA8IGxTaXplOyBpKyspIHsKCXByaW50ZigiJWQgIiwgTFtpXSk7CiAgICB9CgogICAgcHJpbnRmKCJcbkFycmF5MjogIik7CiAgICBmb3IgKGkgPSAwOyBpIDwgclNpemU7IGkrKykgewoJcHJpbnRmKCIlZCAiLCBSW2ldKTsKICAgIH0KCiAgICBwcmludGYoIlxuTWVyZ2UgYXJyYXk6ICIpOwogICAgZm9yIChpID0gMDsgaSA8IGxTaXplK3JTaXplOyBpKyspIHsKCXByaW50ZigiJWQgIiwgZmluYWxBcnJheVtpXSk7CiAgICB9Cn0KCmludCBtYWluKCkKewogICAgLy8gVGVzdCAxCiAgICBtZXJnZSgpOwogICAgZGlzcGxheSgpOwoKICAgIC8vIFRlc3QgMgogICAgUlswXSA9IDE7CiAgICBSWzFdID0gMzsKICAgIFJbMl0gPSA2OwogICAgUlszXSA9IDc7CiAgICBMWzBdID0gMTsKICAgIExbMV0gPSAyOwogICAgTFsyXSA9IDQ7CiAgICBsU2l6ZSA9IDM7CiAgICByU2l6ZSA9IDQ7CiAgICBtZXJnZSgpOwogICAgZGlzcGxheSgpOwoKICAgIC8vIHRlc3QgMwogICAgTFswXSA9IDE7CiAgICBSWzBdID0gMTsKICAgIGxTaXplID0gMTsKICAgIHJTaXplID0gMTsKICAgIG1lcmdlKCk7CiAgICBkaXNwbGF5KCk7CgogICAgcmV0dXJuIDA7Cn0K