Stores the count
int count = 0 ; int maxi = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( next_greater [ i ] - i > k && maxi < arr [ i ] ) { maxi = max ( maxi , arr [ i ] ) ; count ++ ; } } return count ; }