Find the k smallest numbers after deleting given elements | Find k minimum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else push it in the min heap ; Print top k elements in the min heap ; Pop the top element ; Driver code
function findElementsAfterDel ( arr , m , del , n , k ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; ++ i ) { if ( mp . has ( del [ i ] ) ) { mp . set ( del [ i ] , mp . get ( del [ i ] ) + 1 ) ; } else { mp . set ( del [ i ] , 1 ) ; } } let heap = [ ] ; for ( let i = 0 ; i < m ; ++ i ) { if ( mp . has ( arr [ i ] ) ) { mp . set ( arr [ i ] , mp . get ( arr [ i ] ) - 1 ) ; if ( mp . get ( arr [ i ] ) == 0 ) mp . delete ( arr [ i ] ) ; } else heap . push ( arr [ i ] ) ; } heap . sort ( function ( a , b ) { return a - b ; } ) ; for ( let i = 0 ; i < k ; ++ i ) { document . write ( heap [ 0 ] + " " ) ; heap . splice ( 0 , 1 ) ; } } let array = [ 5 , 12 , 33 , 4 , 56 , 12 , 20 ] ; let m = array . length ; let del = [ 12 , 56 , 5 ] ; let n = del . length ; let k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ;