Dada una array arr[] de tamaño N , la tarea es hacer que todos los elementos de la array sean iguales a 0 reemplazando todos los elementos de una subsecuencia de elementos iguales por cualquier número entero, un número mínimo de veces.
Ejemplos:
Entrada: arr[] = {3, 7, 3}, N = 3
Salida: 2
Explicación:
Seleccionar una subsecuencia { 7 } y reemplazar todos sus elementos por 0 modifica arr[] a { 3, 3, 3 }.
Seleccionar la array { 3, 3, 3 } y reemplazar todos sus elementos por 0 modifica arr[] a { 0, 0, 0 }Entrada: arr[] = {1, 5, 1, 3, 2, 3, 1}, N = 7
Salida: 4
Enfoque: El problema se puede resolver usando la técnica Greedy . La idea es contar los distintos elementos presentes en el arreglo que no es igual a 0 e imprimir el conteo obtenido. Siga los pasos a continuación para resolver el problema:
- Inicialice un Set para almacenar los distintos elementos presentes en la array, que no es igual a 0 .
- Recorra la array arr[] e inserte los elementos de la array en el Set .
- Finalmente, imprime el tamaño del Set .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find minimum count of operations // required to convert all array elements to zero // br replacing subsequence of equal elements to 0 void minOpsToTurnArrToZero(int arr[], int N) { // Store distinct elements // present in the array unordered_set<int> st; // Traverse the array for (int i = 0; i < N; i++) { // If arr[i] is already present in // the Set or arr[i] is equal to 0 if (st.find(arr[i]) != st.end() || arr[i] == 0) { continue; } // Otherwise, increment ans by // 1 and insert current element else { st.insert(arr[i]); } } cout << st.size() << endl; } // Driver Code int main() { // Given array int arr[] = { 3, 7, 3 }; // Size of the given array int N = sizeof(arr) / sizeof(arr[0]); minOpsToTurnArrToZero(arr, N); return 0; }
Java
// Java program for the above approach import java.io.*; import java.util.*; class GFG { // Function to find minimum count of operations // required to convert all array elements to zero // br replacing subsequence of equal elements to 0 static void minOpsToTurnArrToZero(int[] arr, int N) { // Store distinct elements // present in the array Set<Integer> st = new HashSet<Integer>(); // Traverse the array for (int i = 0; i < N; i++) { // If arr[i] is already present in // the Set or arr[i] is equal to 0 if (st.contains(arr[i]) || arr[i] == 0) { continue; } // Otherwise, increment ans by // 1 and insert current element else { st.add(arr[i]); } } System.out.println(st.size()); } // Driver Code public static void main(String args[]) { // Given array int arr[] = { 3, 7, 3 }; // Size of the given array int N = arr.length; minOpsToTurnArrToZero(arr, N); } } // This code is contributed by 18bhupendrayadav18
Python3
# Python3 program for the above approach # Function to find minimum count of # operations required to convert all # array elements to zero by replacing # subsequence of equal elements to 0 def minOpsToTurnArrToZero(arr, N): # Store distinct elements # present in the array st = dict() # Traverse the array for i in range(N): # If arr[i] is already present in # the Set or arr[i] is equal to 0 if (i in st.keys() or arr[i] == 0): continue # Otherwise, increment ans by # 1 and insert current element else: st[arr[i]] = 1 print(len(st)) # Driver Code # Given array arr = [ 3, 7, 3 ] # Size of the given array N = len(arr) minOpsToTurnArrToZero(arr, N) # This code is contributed by susmitakundugoaldanga
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to find minimum count of operations // required to convert all array elements to zero // br replacing subsequence of equal elements to 0 static void minOpsToTurnArrToZero(int[] arr, int N) { // Store distinct elements // present in the array HashSet<int> st = new HashSet<int>(); // Traverse the array for (int i = 0; i < N; i++) { // If arr[i] is already present in // the Set or arr[i] is equal to 0 if (st.Contains(arr[i]) || arr[i] == 0) { continue; } // Otherwise, increment ans by // 1 and insert current element else { st.Add(arr[i]); } } Console.WriteLine(st.Count); } // Driver Code public static void Main(String []args) { // Given array int []arr = { 3, 7, 3 }; // Size of the given array int N = arr.Length; minOpsToTurnArrToZero(arr, N); } } // This code is contributed by gauravrajput1
Javascript
<script> // Javascript program for the above approach // Function to find minimum count of operations // required to convert all array elements to zero // br replacing subsequence of equal elements to 0 function minOpsToTurnArrToZero(arr, N) { // Store distinct elements // present in the array var st = new Set(); // Traverse the array for(var i = 0; i < N; i++) { // If arr[i] is already present in // the Set or arr[i] is equal to 0 if (st.has(arr[i]) || arr[i] == 0) { continue; } // Otherwise, increment ans by // 1 and insert current element else { st.add(arr[i]); } } document.write(st.size) } // Driver Code // Given array var arr = [ 3, 7, 3 ]; // Size of the given array var N = arr.length; minOpsToTurnArrToZero(arr, N); // This code is contributed by noob2000 </script>
2
Complejidad temporal: O(N)
Espacio auxiliar: O(N)
Publicación traducida automáticamente
Artículo escrito por ananyadixit8 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA