Find the largest after deleting the given elements Last Updated : 07 Sep, 2022 Summarize Comments Improve Suggest changes Share 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 Find the largest after deleting the given elements I imdhruvgupta Follow Improve Article Tags : Greedy Searching Hash Technical Scripter DSA Arrays Technical Scripter 2018 +3 More Practice Tags : ArraysGreedyHashSearching Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous 4 min read Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ 3 min read Like