My code is not working when i give correct output input of sorted array input

#include
#include
using namespace std;

int pivot(int a[],int beg,int last)
{
int mid;

while(beg<=last)
{
    mid=(beg+last)/2;

    if(mid>beg && a[mid]<a[mid-1])
    {
        return mid-1;
    }
    else if(mid<last && a[mid]>a[mid+1])
    {
        return mid;
    }
    else
    {
        if(a[mid]<=a[beg])
        {
            last=mid-1;
        }
        else
        {
            beg=mid+1;
        }
    }
}

}

int main()
{
int a[]={2,3,4,5,6};

int n=sizeof(a)/sizeof(a[0]);

int index=pivot(a,0,n-1);

if(index<0)
    cout<<"not found\n";
else
    cout<<"Pivot element is found at index "<<index;

return 0;

}