Dado un entero k y un arreglo arr[] , la tarea es repetir la siguiente operación exactamente k veces:
encuentre el elemento mínimo distinto de cero en el arreglo, imprímalo y luego reste este número de todos los elementos distintos de cero del arreglo. formación. Si todos los elementos de la array son < 0 , simplemente imprima 0 .
Ejemplos:
Entrada: arr[] = {3, 6, 4, 2}, k = 5
Salida: 2 1 1 2 0
k = 1; Elija 2 y actualice arr[] = {1, 4, 2, 0}
k = 2; Elija 1, arr[] = {0, 3, 1, 0}
k = 3; Elija 1, arr[] = {0, 2, 0, 0}
k = 4; Elija 2, arr[] = {0, 0, 0, 0}
k = 5; No hay nada que elegir, así que imprima 0
Entrada: arr[] = {1, 2}, k = 3
Salida: 1 1 0
Enfoque: ordene la array y tome una variable adicional llamada suma que almacenará el elemento anterior que se convirtió en 0 .
Tomando arr[] = {3, 6, 4, 2} e inicialmente sum = 0 después de ordenar la array, se convierte en arr[] = {2, 3, 4, 6} .
Ahora sum = 0 , e imprimimos el primer elemento distinto de cero, es decir, 2 y asignamos sum = 2 .
En la siguiente iteración, elija el segundo elemento, es decir, 3 e imprima 3 – sume, es decir , 1 como 2ya se ha restado de todos los demás elementos distintos de cero. Repita estos pasos exactamente k veces.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; #define ll long long int // Function to perform the given operation on arr[] void operations(int arr[], int n, int k) { sort(arr, arr + n); ll i = 0, sum = 0; while (k--) { // Skip elements which are 0 while (i < n && arr[i] - sum == 0) i++; // Pick smallest non-zero element if (i < n && arr[i] - sum > 0) { cout << arr[i] - sum << " "; sum = arr[i]; } // If all the elements of arr[] are 0 else cout << 0 << endl; } } // Driver code int main() { int k = 5; int arr[] = { 3, 6, 4, 2 }; int n = sizeof(arr) / sizeof(arr[0]); operations(arr, n, k); return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to perform the given operation on arr[] static void operations(int arr[], int n, int k) { Arrays.sort(arr); int i = 0, sum = 0; while (k-- > 0) { // Skip elements which are 0 while (i < n && arr[i] - sum == 0) i++; // Pick smallest non-zero element if (i < n && arr[i] - sum > 0) { System.out.print(arr[i] - sum + " "); sum = arr[i]; } // If all the elements of arr[] are 0 else System.out.println("0"); } } // Driver code public static void main(String args[]) { int k = 5; int arr[] = { 3, 6, 4, 2 }; int n = arr.length; operations(arr, n, k); } } // This code is contributed by Princi Singh
Python3
# Python implementation of the approach # Function to perform the given operation on arr[] def operations(arr, n, k): arr.sort(); i = 0; sum = 0; while (k > 0): # Skip elements which are 0 while (i < n and arr[i] - sum == 0): i+=1; # Pick smallest non-zero element if (i < n and arr[i] - sum > 0): print(arr[i] - sum, end= " "); sum = arr[i]; # If all the elements of arr[] are 0 else: print(0); k-=1; # Driver code k = 5; arr = [ 3, 6, 4, 2 ]; n = len(arr); operations(arr, n, k); # This code is contributed by PrinciRaj1992
C#
// C# implementation of the approach using System; class GFG { // Function to perform the given operation on arr[] static void operations(int []arr, int n, int k) { Array.Sort(arr); int i = 0, sum = 0; while (k-- > 0) { // Skip elements which are 0 while (i < n && arr[i] - sum == 0) i++; // Pick smallest non-zero element if (i < n && arr[i] - sum > 0) { Console.Write(arr[i] - sum + " "); sum = arr[i]; } // If all the elements of arr[] are 0 else Console.WriteLine("0"); } } // Driver code public static void Main(String []args) { int k = 5; int []arr = { 3, 6, 4, 2 }; int n = arr.Length; operations(arr, n, k); } } // This code has been contributed by 29AjayKumar
Javascript
<script> // JavaScript program implementation of the approach // Function to perform the given operation on arr[] function operations(arr, n, k) { arr.sort(); let i = 0, sum = 0; while (k-- > 0) { // Skip elements which are 0 while (i < n && arr[i] - sum == 0) i++; // Pick smallest non-zero element if (i < n && arr[i] - sum > 0) { document.write(arr[i] - sum + " "); sum = arr[i]; } // If all the elements of arr[] are 0 else document.write("0"); } } // Driver code let k = 5; let arr = [ 3, 6, 4, 2 ]; let n = arr.length; operations(arr, n, k); </script>
2 1 1 2 0
Complejidad de tiempo: O(nlog(n)+n*k)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por sahilshelangia y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA