Print all array elements appearing more than N / K times | Function to + find the upper_bound of an array element ; Stores minimum index in which K lies ; Stores maximum index in which K lies ; Calculate the upper bound of K ; Stores mid element of l and r ; If arr [ mid ] is less than or equal to K ; Right subarray ; Left subarray ; Function to prall array elements whose frequency is greater than N / K ; Sort the array arr ; Stores index of an array element ; Traverse the array ; Stores upper bound of arr [ i ] ; If frequency of arr [ i ] is greater than N / 4 ; Update i ; Driver Code ; Given array arr ; Size of array ; Function Call
def upperBound ( arr , N , K ) : NEW_LINE INDENT l = 0 ; NEW_LINE r = N ; NEW_LINE while ( l < r ) : NEW_LINE INDENT mid = ( l + r ) // 2 ; NEW_LINE if ( arr [ mid ] <= K ) : NEW_LINE INDENT l = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT r = mid ; NEW_LINE DEDENT DEDENT return l ; NEW_LINE DEDENT def NDivKWithFreq ( arr , N , K ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE i = 0 ; NEW_LINE while ( i < N ) : NEW_LINE INDENT X = upperBound ( arr , N , arr [ i ] ) ; NEW_LINE if ( ( X - i ) > N // 4 ) : NEW_LINE INDENT print ( arr [ i ] , end = " " ) ; NEW_LINE DEDENT i = X ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 4 ; NEW_LINE NDivKWithFreq ( arr , N , K ) ; NEW_LINE DEDENT