package ag;
import java.util.Arrays;
public class MergeD {
public static void main(String[] args) {
int [] a={9,10,1,4,3,6,8,12};
int m=a.length/2;
int [][] matrix=new int[m][2];
int [][] res=new int [m][2];
for(int i=0;i<m;i++)
{
for(int j=0;j<2;j++)
{
matrix[i][j]=a[i*2+j];
}
}
Arrays.sort(matrix,(a1,a2)->(a1[0]-a2[0]));
res[0]=matrix[0];
int j=0;
for(int i=1;i<matrix.length;i++)
{
if(res[j][1]>=matrix[i][0])
{
res[j][1]=Math.max(matrix[i][1],res[j][1]);
}
else
{
j++;
res[j]=matrix[i];
}
}
int [][]res2=(Arrays.copyOf(res,j+1));
for(int i=0;i<j+1;i++)
{
for(int k=0;k<2;k++)
{
System.out.println(res2[i][k]);
}
}
}
}