Merge two sorted arrays with O(1) space complexity


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace OneCompiler
{
	public class Program
	{
		public static void Main(string[] args)
		{
			int m = int.Parse(Console.ReadLine());
			
			int[] arr1 = Array.ConvertAll(Console.ReadLine().Split(" "), new Converter<string,int>(StringToInteger));
			
			int n = int.Parse(Console.ReadLine());
			
			int[] arr2 = Array.ConvertAll(Console.ReadLine().Split(" "), new Converter<string,int>(StringToInteger));
			
			Merge(arr1, arr2);
			
			for (int i = 0; i < m; i++)
			{
			  Console.Write(arr1[i] + " ");
			}
			
			Console.WriteLine();
			
			for (int j = 0; j < n; j++)
			{
			  Console.Write(arr2[j] + " ");
			}
		}
		
		private static int StringToInteger(string s)
		{
		  return int.Parse(s);
		}
		
		private static void Merge(int[] arr1, int[] arr2)
		{
		  int m = arr1.Length;
		  
		  int n = arr2.Length;
		  
		  for (int i = n - 1; i >= 0; i--)
		  {
		    int last = arr1[m - 1];
		    int j;
		    
		    for (j = m - 2; j >= 0 && arr1[j] > arr2[i]; j--)
		    {
		      arr1[j + 1] = arr1[j];
		    }
		    
		    if (last > arr2[i])
		    {
		      arr1[j + 1] = arr2[i];
		      arr2[i] = last;
		    }
		  }
		}
	}
}