# Find the largest after deleting the given elements

Given an array of integers, find the 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.

**Examples:**

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

del[] = { 12, 33, 56, 5 }

Output : 20

Explanation : We get {12, 20} after deleting given elements. Largest among remaining element is 20

**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.
- Initialize largest number max to be INT_MIN.
- Traverse through the array. Check if the element is present in the hash map.
- If present, erase it from the hash map, else if not present compare it with max variable and change its value if the value of the element is greater than the max value.

## C++

`// C++ program to find the largest number ` `// from the array after n deletions ` `#include "climits" ` `#include "iostream" ` `#include "unordered_map" ` `using` `namespace` `std; ` ` ` `// Returns maximum element from arr[0..m-1] after deleting ` `// elements from del[0..n-1] ` `int` `findlargestAfterDel(` `int` `arr[], ` `int` `m, ` `int` `del[], ` `int` `n) ` `{ ` ` ` `// 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 ` ` ` `int` `largestElement = INT_MIN; ` ` ` ` ` `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` ` ` `largestElement = max(largestElement, arr[i]); ` ` ` `} ` ` ` ` ` `return` `largestElement; ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `array[] = { 5, 12, 33, 4, 56, 12, 20 }; ` ` ` `int` `m = ` `sizeof` `(array) / ` `sizeof` `(array[0]); ` ` ` ` ` `int` `del[] = { 12, 33, 56, 5 }; ` ` ` `int` `n = ` `sizeof` `(del) / ` `sizeof` `(del[0]); ` ` ` ` ` `cout << findlargestAfterDel(array, m, del, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 program to find the largest

# number from the array after n deletions

import math as mt

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

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

def findlargestAfterDel(arr, m, dell, n):

# 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

# Initializing the largestElement

largestElement = -10**9

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 compare it largestElement

else:

largestElement = max(largestElement,

arr[i])

return largestElement

# Driver code

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

m = len(array)

dell = [12, 33, 56, 5]

n = len(dell)

print(findlargestAfterDel(array, m, dell, n))

# This code is contributed

# by mohit kumar 29

**Output :**

20

**Time Complexity** – O(N)

## Recommended Posts:

- Find the k largest numbers after deleting the given elements
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Find the smallest after deleting given elements
- Find the k smallest numbers after deleting given elements
- Find maximum points which can be obtained by deleting elements from array
- Find the largest three elements in an array
- Python program to find N largest elements from a list
- Maximum possible middle element of the array after deleting exactly k elements
- Average of remaining elements after removing K largest and K smallest elements from array
- Find last element after deleting every second element in array of n integers
- Length of the largest subarray with contiguous elements | Set 1
- Length of the largest subarray with contiguous elements | Set 2
- Largest sub-array whose all elements are perfect squares
- Third largest element in an array of distinct elements
- Largest subset whose all elements are Fibonacci numbers

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.