Related Articles
Find the k largest numbers after deleting the given elements
• Difficulty Level : Easy
• Last Updated : 03 Feb, 2021

Given an array of integers, find the k largest number after deleting the given elements. In case of repeating elements, delete one instance for every instance of the element present in the array containing the elements to be deleted.
Assume that atleast k elements will be left after deleting n elements.
Examples:
Input : array[] = { 5, 12, 33, 4, 56, 12, 20 }, del[] = { 12, 56, 5 }, k = 3
Output : 33 20 12
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 Max heap.
• After inserting all the elements excluding the ones which are to be deleted, Pop out k elements from the Max heap.

## C++

 `#include "iostream"``#include "queue"``#include "unordered_map"``using` `namespace` `std;` `// Find k maximum 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]]++;``    ``}` `    ``// Initializing the largestElement``    ``priority_queue<``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 compare it largestElement``        ``else``            ``heap.push(arr[i]);``    ``}` `    ``// Print top k elements in the 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);` `    ``int` `del[] = { 12, 56, 5 };``    ``int` `n = ``sizeof``(del) / ``sizeof``(del);` `    ``int` `k = 3;` `    ``findElementsAfterDel(array, m, del, n, k);``    ``return` `0;``}`

## Java

 `import` `java.io.*;``import` `java.util.*;` `class` `GFG``{` `  ``// Find k maximum element from arr[0..m-1] after deleting``  ``// elements from del[0..n-1]``  ``static` `void` `findElementsAfterDel(``int` `arr[], ``int` `m,``                                   ``int` `del[], ``int` `n, ``int` `k)``  ``{` `    ``// Hash Map of the numbers to be deleted``    ``Map mp = ``new` `HashMap();``    ``for` `(``int` `i = ``0``; i < n; ++i)``    ``{` `      ``// Increment the count of del[i]``      ``if``(mp.containsKey(del[i]))``      ``{         ``        ``mp.put(del[i], mp.get(del[i]) + ``1``);``      ``}``      ``else``      ``{``        ``mp.put(del[i], ``1``);``      ``}``    ``}` `    ``// Initializing the largestElement``    ``PriorityQueue heap =``      ``new` `PriorityQueue(``      ``Collections.reverseOrder());``    ``for` `(``int` `i = ``0``; i < m; ++i)``    ``{` `      ``// Search if the element is present``      ``if``(mp.containsKey(arr[i]))``      ``{` `        ``// Decrement its frequency``        ``mp.put(arr[i], mp.get(arr[i]) - ``1``);` `        ``// If the frequency becomes 0,``        ``// erase it from the map``        ``if``(mp.get(arr[i]) == ``0``)``        ``{``          ``mp.remove(arr[i]);``        ``}``      ``}` `      ``// Else compare it largestElement``      ``else``      ``{``        ``heap.add(arr[i]);``      ``}``    ``}` `    ``// Print top k elements in the heap``    ``for``(``int` `i = ``0``; i < k; ++i)``    ``{``      ``// Pop the top element``      ``System.out.print(heap.poll() + ``" "``);``    ``}``  ``}` `  ``// Driver code``  ``public` `static` `void` `main (String[] args)``  ``{``    ``int` `array[] = { ``5``, ``12``, ``33``, ``4``, ``56``, ``12``, ``20` `};``    ``int` `m = array.length;``    ``int` `del[] = { ``12``, ``56``, ``5` `};``    ``int` `n = del.length;``    ``int` `k = ``3``;``    ``findElementsAfterDel(array, m, del, n, k);``  ``}``}` `// This code is contributed by rag2127`

## 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()``    ``heap ``=` `heap[::``-``1``]``    ``return` `heap[:k]` `# Driver code``array ``=` `[``5``, ``12``, ``33``, ``4``, ``56``, ``12``, ``20``]``m ``=` `len``(array)` `dell ``=` `[``12``, ``4``, ``56``, ``5` `]``n ``=` `len``(dell)``k ``=` `3``print``(``*``findElementsAfterDel(array, m, dell, n, k))` `# This code is contributed by``# mohit kumar 29`

## C#

 `using` `System;``using` `System.Collections.Generic;` `class` `GFG``{` `  ``// Find k maximum element from arr[0..m-1] after deleting``  ``// elements from del[0..n-1]``  ``static` `void` `findElementsAfterDel(``int``[] arr, ``int` `m,``                                   ``int``[] del, ``int` `n, ``int` `k)``  ``{` `    ``// Hash Map of the numbers to be deleted``    ``Dictionary<``int``, ``int``> mp = ``new` `Dictionary<``int``, ``int``>();``    ``for` `(``int` `i = 0; i < n; ++i)``    ``{` `      ``// Increment the count of del[i]``      ``if``(mp.ContainsKey(del[i]))``      ``{``        ``mp[del[i]]++;``      ``}``      ``else``      ``{``        ``mp.Add(del[i], 1);``      ``}``    ``}` `    ``// Initializing the largestElement``    ``List<``int``> heap = ``new` `List<``int``>();``    ``for` `(``int` `i = 0; i < m; ++i)``    ``{` `      ``// Search if the element is present``      ``if``(mp.ContainsKey(arr[i]))``      ``{` `        ``// Decrement its frequency``        ``mp[arr[i]]--;` `        ``// If the frequency becomes 0,``        ``// erase it from the map``        ``if``(mp[arr[i]] == 0)``        ``{``          ``mp.Remove(arr[i]);``        ``}``      ``}` `      ``// Else compare it largestElement``      ``else``      ``{``        ``heap.Add(arr[i]);``      ``}``    ``}``    ``heap.Sort();``    ``heap.Reverse();` `    ``// Print top k elements in the heap``    ``for``(``int` `i = 0; i < k; ++i)``    ``{``      ``Console.Write(heap[i] + ``" "``);``    ``}       ``  ``}` `  ``// Driver code``  ``static` `public` `void` `Main ()``  ``{``    ``int``[] array = { 5, 12, 33, 4, 56, 12, 20 };``    ``int` `m = array.Length;``    ``int``[] del = { 12, 56, 5 };``    ``int` `n = del.Length;``    ``int` `k = 3;``    ``findElementsAfterDel(array, m, del, n, k);``  ``}``}` `// This code is contributed by avanitrachhadiya2155`
Output:
`33 20 12` My Personal Notes arrow_drop_up