Find the smallest after deleting given elements | C ++ program to find the smallest number from the array after n deletions ; Returns 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 ] ; Initializing the smallestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it smallestElement ; Driver Code
#include " climits " NEW_LINE #include " iostream " NEW_LINE #include " unordered _ map " NEW_LINE using namespace std ; int findSmallestAfterDel ( int arr [ ] , int m , int del [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; ++ i ) { mp [ del [ i ] ] ++ ; } int smallestElement = INT_MAX ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . find ( arr [ i ] ) != mp . end ( ) ) { mp [ arr [ i ] ] -- ; if ( mp [ arr [ i ] ] == 0 ) mp . erase ( arr [ i ] ) ; } else smallestElement = min ( smallestElement , arr [ i ] ) ; } return smallestElement ; } int main ( ) { int array [ ] = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = sizeof ( array ) / sizeof ( array [ 0 ] ) ; int del [ ] = { 12 , 4 , 56 , 5 } ; int n = sizeof ( del ) / sizeof ( del [ 0 ] ) ; cout << findSmallestAfterDel ( array , m , del , n ) ; return 0 ; }