Find the largest after deleting the given elements Last Updated : 07 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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. Implementation: 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; } Java // Java program to find the largest number // from the array after n deletions import java.util.*; class GFG { // Returns maximum element from arr[0..m-1] after deleting // elements from del[0..n-1] static int findlargestAfterDel(int arr[], int m, int del[], int n) { // Hash Map of the numbers to be deleted HashMap<Integer, Integer> mp = new HashMap<Integer, Integer>(); 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 int largestElement = Integer.MIN_VALUE; 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 largestElement = Math.max(largestElement, arr[i]); } return largestElement; } // Driver Code public static void main(String[] args) { int array[] = { 5, 12, 33, 4, 56, 12, 20 }; int m = array.length; int del[] = { 12, 33, 56, 5 }; int n = del.length; System.out.println(findlargestAfterDel(array, m, del, n)); } } // This code is contributed by Rajput-Ji 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 C# // C# program to find the largest number // from the array after n deletions using System; using System.Collections.Generic; class GFG { // Returns maximum element from arr[0..m-1] // after deleting elements from del[0..n-1] static int findlargestAfterDel(int []arr, int m, int []del, int n) { // 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[arr[i]] = mp[arr[i]] + 1; } else { mp.Add(del[i], 1); } } // Initializing the largestElement int largestElement = int.MinValue; for (int i = 0; i < m; i++) { // Search if the element is present if (mp.ContainsKey(arr[i])) { // Decrement its frequency mp[arr[i]] = mp[arr[i]] - 1; // If the frequency becomes 0, // erase it from the map if (mp[arr[i]] == 0) mp.Remove(arr[i]); } // Else compare it largestElement else largestElement = Math.Max(largestElement, arr[i]); } return largestElement; } // Driver Code public static void Main(String[] args) { int []array = { 5, 12, 33, 4, 56, 12, 20 }; int m = array.Length; int []del = { 12, 33, 56, 5 }; int n = del.Length; Console.WriteLine(findlargestAfterDel(array, m, del, n)); } } // This code is contributed by Princi Singh JavaScript <script> // Javascript program to find the largest number // from the array after n deletions // Returns maximum element from arr[0..m-1] after deleting // elements from del[0..n-1] function findlargestAfterDel(arr,m,del,n) { // Hash Map of the numbers to be deleted let mp = new Map(); for (let i = 0; i < n; ++i) { // Increment the count of del[i] if(mp.has(del[i])) { mp.set(del[i], mp.get(del[i]) + 1); } else { mp.set(del[i], 1); } } // Initializing the largestElement let largestElement = Number.MIN_VALUE; for (let i = 0; i < m; i++) { // Search if the element is present if (mp.has(arr[i])) { // Decrement its frequency mp.set(arr[i], mp.get(arr[i]) - 1); // If the frequency becomes 0, // erase it from the map if (mp.get(arr[i]) == 0) mp.delete(arr[i]); } // Else compare it largestElement else largestElement = Math.max(largestElement, arr[i]); } return largestElement; } // Driver Code let array=[5, 12, 33, 4, 56, 12, 20]; let m = array.length; let del = [ 12, 33, 56, 5 ]; let n = del.length; document.write(findlargestAfterDel(array, m, del, n)); // This code is contributed by patel2127 </script> Output20 Complexity Analysis: Time Complexity: O(max(m, n)Auxiliary Space: O(m + n) Comment More infoAdvertise with us Next Article Queries to find the maximum array element after removing elements from a given range I imdhruvgupta Follow Improve Article Tags : Greedy Searching Hash Technical Scripter DSA Arrays Technical Scripter 2018 +3 More Practice Tags : ArraysGreedyHashSearching Similar Reads Find the k largest numbers after deleting the given elements 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 at least k elements will be left after deleting n elem 7 min read 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 7 min read Find k largest elements in an array Given an array arr[] and an integer k, the task is to find k largest elements in the given array. Elements in the output array should be in decreasing order.Examples:Input: [1, 23, 12, 9, 30, 2, 50], k = 3Output: [50, 30, 23]Input: [11, 5, 12, 9, 44, 17, 2], k = 2Output: [44, 17]Table of Content[Nai 15+ min read Find Second largest element in an array | Set 2 Given an array arr[] consisting of N integers, the task is to find the second largest element in the given array using N+log2(N) - 2 comparisons. Examples: Input : arr[] = {22, 33, 14, 55, 100, 12}Output : 55 Input : arr[] = {35, 23, 12, 35, 19, 100}Output : 35 Sorting and Two-Traversal Approach: Re 9 min read Queries to find the maximum array element after removing elements from a given range Given an array arr[] and an array Q[][] consisting of queries of the form of {L, R}, the task for each query is to find the maximum array element after removing array elements from the range of indices [L, R]. If the array becomes empty after removing the elements from given range of indices, then p 10 min read Largest possible number by deleting given digit Find the largest positive integer that can be formed by deleting only one occurrence of a given digit. Examples: Input: num = 56321, digit = 5Output: 6321Explanation: Since the number 56321 contain only 1 occurrence of 5, we can remove it to get 6321 which is the largest possible positive number. In 7 min read Like