My code is running on local compiler but here it gives run error(NOTE: this is the main program I did include all the main files while writing it)

bool nextpermutation(int arr[],int n)
{

if(n==1)
	return false;
	
int i=n-2;

while(i>=0 && arr[i]>arr[i+1])
{
    i--;
}

if(i==0)
   return false;

else
{
    auto it=upper_bound(arr+i,arr+n,arr[i]);
    int index=it-arr;
    swap(arr[i],arr[index]);
    reverse(arr+i+1,arr+n);
    return true;
}

}

void printarray(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" “;
}
cout<<”\n";
}

int main()
{
int t;
cin>>t;
while(t–)
{
int n;
cin>>n;

    int *arr=new int[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }


    if(nextpermutation(arr,n))
    {
        printarray(arr,n);
    }

    else
    {
        sort(arr,arr+n);
        printarray(arr,n);
    }
}

}

the problem is in this line:
if(i==0)
return false;

this should be:
if(i==-1)
return false;

thanks

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.