# Merge 2 sorted array with o(1) space

Hi, I was doing this question, in the question it is mentioned that expected TC will be O((m+n)log(m+n))
my solution getting TLE. why?
Approach - taking 2 pointers one at the end of array1, and other at the starting of array2.
comparing both, if arr1>arr2, swap, and decrement pointer 1, and increment pointer 2, otherwise break.

``````import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int size1=sc.nextInt();
int size2=sc.nextInt();
int[] arr1=new int[size1];
int[] arr2=new int[size2];
for(int i=0;i<size1;i++) arr1[i]=sc.nextInt();
for(int i=0;i<size2;i++)arr2[i]=sc.nextInt();
int i=size1-1;int j=0;
while(j<=size2-1&&i>=0){
if(arr2[j]<arr1[i]){
int temp=arr2[j];
arr2[j]=arr1[i];
arr1[i]=temp;
i--;j++;
}
else break;
}
Arrays.sort(arr1);
Arrays.sort(arr2);
for(int k=0;k<arr1.length;k++)System.out.print(arr1[k]+" ");
for(int k=0;k<arr2.length;k++)System.out.print(arr2[k]+" ");
System.out.println();
}
}
}
``````

Thank you guys, it has solved by using bufferedReader

``````/* package codechef; // don't place package name! */
//@Author=Mr.Kashyap
import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{

public static void main(String[] args) throws IOException
{
Scan sc = new Scan();
Print print=new Print();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));

int t=sc.scanInt();
while(t-->0){
int size1=sc.scanInt();
int size2=sc.scanInt();
int[] arr1=new int[size1];
int[] arr2=new int[size2];
for(int i=0;i<size1;i++) arr1[i]=sc.scanInt();
for(int i=0;i<size2;i++)arr2[i]=sc.scanInt();
int i=size1-1;int j=0;
while(j<=size2-1&&i>=0){
if(arr2[j]<arr1[i]){
int temp=arr2[j];
arr2[j]=arr1[i];
arr1[i]=temp;
i--;j++;
}
else break;
}
Arrays.sort(arr1);
Arrays.sort(arr2);
for(int k=0;k<arr1.length;k++)bw.append(arr1[k]+" ");
for(int k=0;k<arr2.length;k++)bw.append(arr2[k]+" ");
bw.append("\n");
}

bw.close();
}

/*BufferedWriter with scanInt, (long)scanDouble, scanString , bw.append(""+\n)*/

static class Print
{
private final OutputStream out;
/*public Print(OutputStream outputStream)
{
writer=new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}*/
public Print()
{
this.out=System.out;
}
public void print(String str)throws IOException
{
//buf=str.getBytes();
for (int i = 0; i < str.length(); i++)
{
/*if (i != 0)
out.write(' ');*/
out.write(str.charAt(i));
}
}
public void printLine(String str)throws IOException
{
print(str);
out.write('\n');
}
public void close()throws IOException
{
out.close();
}
}
static class Scan
{
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}

}``````