Print all array elements appearing more than N / K times | function to sort the array ; 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 print all 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 ; Given array arr [ ] ; Size of array ; Function Call
function sortt ( number ) { var n = number . length ; for ( i = 0 ; i < n ; ++ i ) { for ( j = i + 1 ; j < n ; ++ j ) { if ( number [ i ] > number [ j ] ) { a = number [ i ] ; number [ i ] = number [ j ] ; number [ j ] = a ; } } } } function upperBound ( arr , N , K ) { var l = 0 ; var r = N ; while ( l < r ) { var mid = parseInt ( ( l + r ) / 2 ) ; if ( arr [ mid ] <= K ) { l = mid + 1 ; } else { r = mid ; } } return l ; } function NDivKWithFreq ( arr , N , K ) { sortt ( arr ) ; var i = 0 ; while ( i < N ) { var X = upperBound ( arr , N , arr [ i ] ) ; if ( ( X - i ) > parseInt ( N / 4 ) ) { document . write ( arr [ i ] + " " ) ; } i = X ; } } var arr = [ 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 ] ; var N = arr . length ; var K = 4 ; NDivKWithFreq ( arr , N , K ) ;