Run Error in MergeSort

The code is running on both CB IDE & Offline Compiler, but its showing a run error on the challenges compiler.

Here is the code:
#include <bits/stdc++.h>
using namespace std;

void merge(int *arr, int s, int e)
{
int mid=(s+e)/2;
int i=s;
int j=mid+1;
int k=s;

int temp[1000]={0};

while(i<=mid and j<=e)
{
	if(arr[i]<arr[j])
	{
		temp[k]=arr[i];
		i++;
		k++;
	}
	else
	{
		temp[k++]=arr[j++];
	}
}
while(i<=mid)
{
	temp[k++]=arr[i++];
}
while(j<=e)
{
	temp[k++]=arr[j++];
}

for(int x=s;x<k;x++)
{
	arr[x]=temp[x];
}
return;

}

void merge_sort(int *arr, int s, int e)
{
if(s>=e)
{
return;
}
int mid=(s+e)/2;
merge_sort(arr,s,mid);
merge_sort(arr,mid+1,e);
merge(arr,s,e);
return;
}

int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
merge_sort(arr,0,n-1);
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}

return 0;

}

Hi @S19IPC0010, pls try 2 things:

  1. do not keep the size of temp array as 1000 use e-s+1 instead.
  2. in merge function in last while loop write:

    … x= s ; x <= e ; x++ …
    /// i.e. instead of <k write <=e

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.