Stores the count of smaller elements on its right
int * countSmaller = ( int * ) malloc ( sizeof ( int ) * n ) ; constructLowerArray ( A , countSmaller , n ) ; int maxi = INT_MIN ; for ( int i = 0 ; i <= ( n - K - 1 ) ; i ++ ) { if ( A [ i ] > maxi && countSmaller [ i ] >= K ) { count ++ ; maxi = A [ i ] ; } } return count ; }