Given an array of integers, find the smallest number after deleting given elements. In case of repeating elements, we delete one instance (from the original array) for every instance present in the array containing elements to be deleted.
Examples:
Input : Array = {5, 12, 33, 4, 56, 12, 20}
To Delete = {12, 4, 56, 5}
Output : 12
After deleting given elements, array becomes {33, 12, 20} and minimum element becomes 12. Note that there are two occurrences of 12 and we delete one of them.Input : Array = {1, 20, 3, 4, 10}
To Delete = {1, 4, 10}
Output : 3
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 smallest number min to be INT_MAX.
- 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 min variable and change its value if the value of the element is less than the min value.
C++
// C++ program to find the smallest number // from the array after n deletions #include "climits" #include "iostream" #include "unordered_map" using namespace std; // Returns minimum element from arr[0..m-1] after deleting // elements from del[0..n-1] int findSmallestAfterDel(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 smallestElement int smallestElement = INT_MAX; 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 smallestElement else smallestElement = min(smallestElement, arr[i]); } return smallestElement; } int main() { int array[] = { 5, 12, 33, 4, 56, 12, 20 }; int m = sizeof(array) / sizeof(array[0]); int del[] = { 12, 4, 56, 5 }; int n = sizeof(del) / sizeof(del[0]); cout << findSmallestAfterDel(array, m, del, n); return 0; } |
Java
// Java program to find the smallest number // from the array after n deletions import java.util.*; class GFG { // Returns minimum element from arr[0..m-1] // after deleting elements from del[0..n-1] static int findSmallestAfterDel(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 smallestElement int smallestElement = Integer.MAX_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 smallestElement else smallestElement = Math.min(smallestElement, arr[i]); } return smallestElement; } // Driver Code public static void main(String[] args) { int array[] = { 5, 12, 33, 4, 56, 12, 20 }; int m = array.length; int del[] = { 12, 4, 56, 5 }; int n = del.length; System.out.println(findSmallestAfterDel(array, m, del, n)); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program to find the smallest # 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 findSmallestAfterDel(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 SmallestElement SmallestElement = 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 SmallestElement else: SmallestElement = min(SmallestElement, arr[i]) return SmallestElement # Driver code array = [5, 12, 33, 4, 56, 12, 20] m = len(array) dell = [12, 4, 56, 5] n = len(dell) print(findSmallestAfterDel(array, m, dell, n)) # This code is contributed # by mohit kumar 29 |
C#
// C# program to find the smallest number // from the array after n deletions using System; using System.Collections.Generic; class GFG { // Returns minimum element from arr[0..m-1] // after deleting elements from del[0..n-1] static int findSmallestAfterDel(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[del[i]] = mp[del[i]] + 1; } else { mp.Add(del[i], 1); } } // Initializing the smallestElement int smallestElement = int.MaxValue; 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 smallestElement else smallestElement = Math.Min(smallestElement, arr[i]); } return smallestElement; } // Driver Code public static void Main(String[] args) { int []array = { 5, 12, 33, 4, 56, 12, 20 }; int m = array.Length; int []del = { 12, 4, 56, 5 }; int n = del.Length; Console.WriteLine(findSmallestAfterDel(array, m, del, n)); } } // This code is contributed by Princi Singh |
12
Time Complexity – O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find the k smallest numbers after deleting given elements
- Find the largest after deleting the given elements
- Find the k largest numbers after deleting the given elements
- Change in Median of given array after deleting given elements
- Maximum possible middle element of the array after deleting exactly k elements
- Find last element after deleting every second element in array of n integers
- Reduce the array by deleting elements which are greater than all elements to its left
- Find maximum points which can be obtained by deleting elements from array
- Last element remaining by deleting two largest elements and replacing by their absolute difference if they are unequal
- Average of remaining elements after removing K largest and K smallest elements from array
- Find the Largest Cube formed by Deleting minimum Digits from a number
- Maximum score of deleting an element from an Array based on given condition
- Find the smallest and second smallest elements in an array
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- PHP | Deleting an element from array using array_diff()
- Deleting all files from a folder using PHP
- R - Creating, Listing, and Deleting Objects in Memory
- Determine the winner of a game of deleting Characters from a String
- Maximize the size of array by deleting exactly k sub-arrays to make array prime
- K-th smallest element after removing given integers from natural numbers | Set 2
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.

