Help me in solving HILLS problem

My issue

I worked up a solution and it ran for the test case(provided in the example but it gave a wrong answer when I submitted it.

My code

#include <bits/stdc++.h>
using namespace std;


int main() {
	// your code goes here
	int T;
	cin>>T;
	while(T--){
	    int N,U,D;
	    cin>>N>>U>>D;
	    int P=1;
	    int H[N];
	    int nh=1;
	    /*cout<<N<<' '<<U<<' '<<D<<endl;*/
	    for(int i=0;i<N;i++){
	        int h;
	        cin>>h;
	        H[i]=h;

	   
	    }
	    for(int i=0;i<(N-1);i++){
	       if((H[(i+1)])==(H[i])){ 
	           nh++;

	       }
	       else if((H[(i+1)]>H[i])&&(H[(i+1)]<=(H[i]+U))){

	           nh++;
	       }
	       else if(H[i]>H[(i+1)]){
	                
	           if(H[i]<=(H[(i+1)]+D)){
	               nh++;
	           }
	           else if(P==1){
	               P--;
	               nh++;
	           }
	       }
	       else {
	            
	          break;
	           
	       }
	            
	           
	    }
	    
	    
	    cout<<nh<<"\n";
	    
	    
	    
	}

}

Problem Link: Jumping in the hills Practice Coding Problem - CodeChef

@bensmari
plzz refer the following solution

#include<iostream>
using namespace std;

int main(){
    long t;
    cin>>t;
    while(t--){
        long n,u,d,c=0,count=1;
        cin>>n>>u>>d;
        long arr[n];
        
        for (long i=0;i<n;i++){
            cin>>arr[i];
        }
        
        for(long i=0;i<n-1;i++){
            if(c==0){
                if(arr[i]==arr[i+1]){
                    count++;
                }
                else if(arr[i]<arr[i+1]){
                    if(arr[i+1]-arr[i]<=u){
                        count++;
                    }
                    else{
                        break;
                    }
                }
                
                else if(arr[i]>arr[i+1]){
                    if(arr[i]-arr[i+1]<=d){
                        count++;
                    }
                    else{
                        count++;
                        c++;
                    }
                }
            }
            
            else if(c>0){
                if(arr[i]==arr[i+1]){
                    count++;
                }
                else if(arr[i]<arr[i+1]){
                    if(arr[i+1]-arr[i]<=u){
                        count++;
                    }
                    else{
                        break;
                    }
                }
                
                else if(arr[i]>arr[i+1]){
                    if(arr[i]-arr[i+1]<=d){
                        count++;
                    }
                    else{
                        break;
                    }
                }
            }
            
            
        }
        cout<<count<<endl;
        
    }
    return 0;
}