Dada una array arr[] que consta de N enteros de punto flotante, la tarea es modificar la array ejecutando ceil() o floor() en cada elemento de la array de modo que la suma de los elementos de la array esté cerca de 0 .
Ejemplos:
Entrada: arr[] = {6.455, -1.24, -3.87, 2.434, -4.647}
Salida: {6, -2, -3, 3, -4}
Explicación:
Realice la operación de piso en los elementos de la array en los índices {0, 1} y la operación ceil en los índices {2, 3, 4} modifica la array a {6, -2, -3, 3, -4} cuya suma de elementos es 0, que es lo más cercano a 0.Entrada: arr[] = {-12.42, 9.264, 24.24, -13.04, 4.0, -9.66, -2.99}
Salida: {-13, 9, 24, -13, 4, -9, -2}
Enfoque: el problema dado se puede resolver encontrando la suma del valor ceil() de todos los elementos de la array y, si la suma de la array es positiva, encuentre el límite de esa cantidad de elementos de modo que se acerque más al valor 0. Siga los siguientes pasos para resolver el problema dado:
- Inicialice una variable, digamos sum to 0 que almacene la suma de los elementos de la array.
- Inicialice una array, digamos A[] que almacena los elementos de array actualizados.
- Recorra la array arr[] y encuentre la suma de ceil() de los elementos de la array y actualice el valor de A[i] al valor ceil(arr[i]) .
- Si el valor de la suma es positivo, encuentre el piso de algún elemento de la array para reducir la suma al más cercano a 0 y el recuento de dicho elemento de la array viene dado por el mínimo de la suma y N.
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 modify the array element // such that the sum is close to 0 void setSumtoZero(double arr[], int N) { // Stores the modified elements int A[N]; // Stores the sum of array element int sum = 0; // Stores minimum size of the array int m = INT_MIN; // Traverse the array and find the // sum for (int i = 0; i < N; i++) { sum += ceil(arr[i]); A[i] = ceil(arr[i]); } // If sum is positive if (sum > 0) { // Find the minimum number of // elements that must be changed m = min(sum, N); // Iterate until M elements are // modified or the array end for (int i = 0; i < N && m > 0; i++) { // Update the current array // elements to its floor A[i] = floor(arr[i]); if (A[i] != floor(arr[i])) m--; } } // Print the resultant array for (int i = 0; i < N; i++) { cout << A[i] << " "; } } // Driver Code int main() { double arr[] = { -2, -2, 4.5 }; int N = sizeof(arr) / sizeof(arr[0]); setSumtoZero(arr, N); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to modify the array element // such that the sum is close to 0 static void setSumtoZero(double arr[], int N) { // Stores the modified elements int []A = new int[N]; // Stores the sum of array element int sum = 0; // Stores minimum size of the array int m = Integer.MIN_VALUE; // Traverse the array and find the // sum for (int i = 0; i < N; i++) { sum += Math.ceil(arr[i]); A[i] = (int) Math.ceil(arr[i]); } // If sum is positive if (sum > 0) { // Find the minimum number of // elements that must be changed m = Math.min(sum, N); // Iterate until M elements are // modified or the array end for (int i = 0; i < N && m > 0; i++) { // Update the current array // elements to its floor A[i] = (int) Math.floor(arr[i]); if (A[i] != Math.floor(arr[i])) m--; } } // Print the resultant array for (int i = 0; i < N; i++) { System.out.print(A[i]+ " "); } } // Driver Code public static void main(String[] args) { double arr[] = { -2, -2, 4.5 }; int N = arr.length; setSumtoZero(arr, N); } } // This code is contributed by shikhasingrajput
Python3
# Python 3 program for the above approach import sys from math import ceil,floor # Function to modify the array element # such that the sum is close to 0 def setSumtoZero(arr, N): # Stores the modified elements A = [0 for i in range(N)] # Stores the sum of array element sum = 0 # Stores minimum size of the array m = -sys.maxsize-1 # Traverse the array and find the # sum for i in range(N): sum += ceil(arr[i]) A[i] = ceil(arr[i]) # If sum is positive if (sum > 0): # Find the minimum number of # elements that must be changed m = min(sum, N) # Iterate until M elements are # modified or the array end i = 0 while(i < N and m > 0): # Update the current array # elements to its floor A[i] = floor(arr[i]) if (A[i] != floor(arr[i])): m -= 1 i += 1 # Print the resultant array for i in range(N): print(A[i],end = " ") # Driver Code if __name__ == '__main__': arr = [-2, -2, 4.5] N = len(arr) setSumtoZero(arr, N) # This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach using System; public class GFG{ // Function to modify the array element // such that the sum is close to 0 static void setSumtoZero(double []arr, int N) { // Stores the modified elements int []A = new int[N]; // Stores the sum of array element int sum = 0; // Stores minimum size of the array int m = int.MinValue; // Traverse the array and find the // sum for (int i = 0; i < N; i++) { sum += (int)Math.Ceiling(arr[i]); A[i] = (int) Math.Ceiling(arr[i]); } // If sum is positive if (sum > 0) { // Find the minimum number of // elements that must be changed m = Math.Min(sum, N); // Iterate until M elements are // modified or the array end for (int i = 0; i < N && m > 0; i++) { // Update the current array // elements to its floor A[i] = (int) Math.Floor(arr[i]); if (A[i] != Math.Floor(arr[i])) m--; } } // Print the resultant array for (int i = 0; i < N; i++) { Console.Write(A[i]+ " "); } } // Driver Code public static void Main(String[] args) { double []arr = { -2, -2, 4.5 }; int N = arr.Length; setSumtoZero(arr, N); } } // This code is contributed by 29AjayKumar
Javascript
<script> // JavaScript Program to implement // the above approach // Function to modify the array element // such that the sum is close to 0 function setSumtoZero(arr, N) { // Stores the modified elements let A = new Array(N); // Stores the sum of array element let sum = 0; // Stores minimum size of the array let m = Number.MIN_VALUE; // Traverse the array and find the // sum for (let i = 0; i < N; i++) { sum += Math.ceil(arr[i]); A[i] = Math.ceil(arr[i]); } // If sum is positive if (sum > 0) { // Find the minimum number of // elements that must be changed m = Math.min(sum, N); // Iterate until M elements are // modified or the array end for (let i = 0; i < N && m > 0; i++) { // Update the current array // elements to its floor A[i] = Math.floor(arr[i]); if (A[i] != Math.floor(arr[i])) m--; } } // Print the resultant array for (let i = 0; i < N; i++) { document.write(A[i] + " "); } } // Driver Code let arr = [-2, -2, 4.5]; let N = arr.length; setSumtoZero(arr, N); // This code is contributed by Potta Lokesh </script>
-2 -2 4
Complejidad temporal: O(N)
Espacio auxiliar: O(N)