# Find the k smallest numbers after deleting given elements

Given an array of integers, find the k smallest numbers after deleting given elements. In case of repeating elements delete only one instance in the given array for every instance of element present in the array containing the elements to be deleted.

Assume that there are at least k elements left in the array after making n deletions.

**Examples:**

Input : array[] = { 5, 12, 33, 4, 56, 12, 20 }, del[] = { 12, 56, 5 }, k = 3

Output : 4 12 20

Explanation : After deletions { 33, 4, 12, 20 } will be left. Print top 3 highest elements from it.

**Approach :**

- Insert all the numbers in the hash map which are to be deleted from the array, so that we can check if the element in the array is also present in the Delete-array in O(1) time.
- Traverse through the array. Check if the element is present in the hash map.
- If present, erase it from the hash map.
- Else, insert it into a Min heap.
- After inserting all the elements excluding the ones which are to be deleted, Pop out k elements from the Min heap.

## C++

`#include "iostream" ` `#include "queue" ` `#include "unordered_map" ` `#include "vector" ` `using` `namespace` `std; ` ` ` `// Find k minimum element from arr[0..m-1] after deleting ` `// elements from del[0..n-1] ` `void` `findElementsAfterDel(` `int` `arr[], ` `int` `m, ` `int` `del[], ` ` ` `int` `n, ` `int` `k) ` `{ ` ` ` `// Hash Map of the numbers to be deleted ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` `for` `(` `int` `i = 0; i < n; ++i) { ` ` ` ` ` `// Increment the count of del[i] ` ` ` `mp[del[i]]++; ` ` ` `} ` ` ` ` ` `priority_queue<` `int` `, vector<` `int` `>, greater<` `int` `> > heap; ` ` ` ` ` `for` `(` `int` `i = 0; i < m; ++i) { ` ` ` ` ` `// Search if the element is present ` ` ` `if` `(mp.find(arr[i]) != mp.end()) { ` ` ` ` ` `// Decrement its frequency ` ` ` `mp[arr[i]]--; ` ` ` ` ` `// If the frequency becomes 0, ` ` ` `// erase it from the map ` ` ` `if` `(mp[arr[i]] == 0) ` ` ` `mp.erase(arr[i]); ` ` ` `} ` ` ` ` ` `// Else push it in the min heap ` ` ` `else` ` ` `heap.push(arr[i]); ` ` ` `} ` ` ` ` ` `// Print top k elements in the min heap ` ` ` `for` `(` `int` `i = 0; i < k; ++i) { ` ` ` `cout << heap.top() << ` `" "` `; ` ` ` ` ` `// Pop the top element ` ` ` `heap.pop(); ` ` ` `} ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `array[] = { 5, 12, 33, 4, 56, 12, 20 }; ` ` ` `int` `m = ` `sizeof` `(array) / ` `sizeof` `(array[0]); ` ` ` ` ` `int` `del[] = { 12, 56, 5 }; ` ` ` `int` `n = ` `sizeof` `(del) / ` `sizeof` `(del[0]); ` ` ` ` ` `int` `k = 3; ` ` ` ` ` `findElementsAfterDel(array, m, del, n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to find the k maximum

# number from the array after n deletions

import math as mt

# Find k maximum element from arr[0..m-1]

# after deleting elements from del[0..n-1]

def findElementsAfterDel(arr, m, dell, n, k):

# Hash Map of the numbers to be deleted

mp = dict()

for i in range(n):

# Increment the count of del[i]

if dell[i] in mp.keys():

mp[dell[i]] += 1

else:

mp[dell[i]] = 1

heap = list()

for i in range(m):

# Search if the element is present

if (arr[i] in mp.keys()):

# Decrement its frequency

mp[arr[i]] -= 1

# If the frequency becomes 0,

# erase it from the map

if (mp[arr[i]] == 0):

mp.pop(arr[i])

# Else push it

else:

heap.append(arr[i])

heap.sort()

return heap[:k]

# Driver code

array = [5, 12, 33, 4, 56, 12, 20]

m = len(array)

dell = [12, 56, 5 ]

n = len(dell)

k = 3

print(*findElementsAfterDel(array, m, dell, n, k))

# This code is contributed

# by mohit kumar 29

**Output:**

4 12 20

## Recommended Posts:

- Find the smallest after deleting given elements
- Find the k largest numbers after deleting the given elements
- Find the largest after deleting the given elements
- Find maximum points which can be obtained by deleting elements from array
- Find the smallest and second smallest elements in an array
- Find smallest subarray that contains all elements in same order
- Program to find the smallest element among three elements
- Find smallest range containing elements from k lists
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find four missing numbers in an array containing elements from 1 to N
- Average of remaining elements after removing K largest and K smallest elements from array
- Find all numbers that divide maximum array elements
- Find last element after deleting every second element in array of n integers
- Smallest greater elements in whole array
- Smallest subarray such that all elements are greater than K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.