Dada una array de enteros, encuentre el número más pequeño después de eliminar los elementos dados. En caso de elementos repetidos, eliminamos una instancia (de la array original) por cada instancia presente en la array que contiene los elementos que se eliminarán.
Ejemplos:
Entrada: array = {5, 12, 33, 4, 56, 12, 20}
Para eliminar = {12, 4, 56, 5}
Salida: 12
Después de eliminar elementos dados, la array se convierte en {33, 12, 20} y mínimo elemento se convierte en 12. Tenga en cuenta que hay dos ocurrencias de 12 y eliminamos una de ellas.
Entrada: array = {1, 20, 3, 4, 10}
Para eliminar = {1, 4, 10}
Salida: 3
Acercarse :
- Inserte todos los números en el mapa hash que se eliminarán de la array, de modo que podamos verificar si el elemento en la array también está presente en la array Eliminar en tiempo O (1).
- Inicialice el número más pequeño min para que sea INT_MAX.
- Atraviesa la array. Compruebe si el elemento está presente en el mapa hash.
- Si está presente, bórrelo del mapa hash; de lo contrario, si no está presente, compárelo con la variable mínima y cambie su valor si el valor del elemento es menor que el valor mínimo.
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
Javascript
<script> // JavaScript program to find the smallest number // from the array after n deletions // Returns minimum element from arr[0..m-1] // after deleting elements from del[0..n-1] function findSmallestAfterDel(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 smallestElement let smallestElement = Number.MAX_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 smallestElement else smallestElement = Math.min(smallestElement, arr[i]); } return smallestElement; } // Driver Code let array=[5, 12, 33, 4, 56, 12, 20]; let m = array.length; let del=[12, 4, 56, 5]; let n = del.length; document.write(findSmallestAfterDel(array, m, del, n)); // This code is contributed by patel2127 </script>
12
Complejidad del tiempo – O(N)
Publicación traducida automáticamente
Artículo escrito por imdhruvgupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA