don't know what's wrong in my programs -PUMPWAT

i coded the solution for pumpwat and it gives correct answer for my test cases and the basic test case,
but on submission it get rejected by the compiler .I can’t find any mistake in my method ,please help me with this.

question link-[https://www.codechef.com/COOK99B/problems/PUMPWAT][1]

//code
#include<iostream>
using namespace std;
int maxe(int ar[],int n,int j)
{
    int index,maxa;
    maxa=ar[j];
    index=j;
    for(int i=j;i<n;i++)
    {
        if(ar[i]>maxa){
            maxa=ar[i];index=i;
        }
    }
    return index;
}

int noof(int ar[],int n){
    int n1=n,no=0,j=0;
    do{
        int index= maxe(ar,n1,j);
       int  mid=n1/2;
        if(index<=(mid-1)){
            n1=index;no++;
        }
        else{
            j=index+1;no++;
        }
    }while(n1>0&&j<n1);

    return no;
}
int main(){
    int n,t;
    cin>>t;
    while(t--){
        cin>>n;
        int ar[n];
        for(int i=0;i<n;i++)
            cin>>ar[i];
        cout<<noof(ar,n)<<endl;

    }



}

check
16 17 16 18 16 17 16

and
16 17 16 18 16 16 17

In the constraints it is given that elements are distinct

Greedyly choosing to cover the larger half doesn’t work, because it is not guaranteed that a smaller array can be watered with less reservoirs than a larger one.
E.g. your algorithm doesn’t work for the following case:

1
9
1 3 2 9 8 7 6 5 4
2 Likes