Help me in solving ENDSORTED problem

My issue

I am unable to find what’s causing the error

My code

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int for1(vector <int> &a,int *d,int n)
{
    int t;
    auto it=a.find(a.begin(),a.end(),1);
    t=a[it];
    a[it]=a[it-1];
    a[it-1]=t;
    auto i=a.find(a.begin(),a.end(),4);
    t=a[i];
    a[i]=a[i+1];
    a[i+1]=t;
    if(a[0]==1 && a[n-1]==4)
    return a;
    else
    {
        *d++;
        for1(a);
    
    }
}


int main() {
	int t;
	cin>>t;
	while(t--)
	{
	int j,num,d=0;
	vector <int> vec;
	for(int i=0;i<n;i++)
	{cin>>j;
	vec.push_back(j);}
	for1(vec,d,num);
	cout<<d<<endl;
	}
	return 0;
}

Problem Link: ENDSORTED Problem - CodeChef

@codestar05
I have coded it in a much simpler way .
Please refer the solution for better understanding.

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	int t;
	cin>>t;
	while(t--)
	{
	    int ans=0;
	    int n;
	    cin>>n;
	    int a[n];
	    for(int i=0;i<n;i++)
	    {
	        cin>>a[i];
	    }
	    if(a[0]==1&&a[n-1]==n)
	    cout<<0;
	    else
	    {
	        for(int i=0;i<n;i++)
	        {
	            if(a[i]==n&&i<n-1)
	            {
	            swap(a[i],a[i+1]);
	            ans++;
	            }
	        }
	        for(int i=n-1;i>=0;i--)
	        {
	            if(a[i]==1&&i>0)
	            {
	                swap(a[i],a[i-1]);
	                ans++;
	            }
	        }
	        cout<<ans;
	    }
	    cout<<endl;
	}
	return 0;
}

@dpcoder_007
Thank you