You are not taking the complete input. Eg- For N=1 case which you handled separately, you did not take in the given array value as input, which in turn got taken as your next N value in next iteration. Please take the complete input and try again. Failing case-

@saini30 - You fail even at sample input output. Firstly - else if((a[i]-a[i+1])<=d) add a check that a[i]\ge a[i+1] because the condition above it can fail even when a[i]\le a[i+1]

What is happening is, your code doesnt stop if height of next hill is more than U. it goes to else if((a[i]-a[i+1])<=d) this condition and increases c if this one satisfies.