Dada una array A[] de N enteros tal que A[0] + A[1] + A[2] + … A[N – 1] = 0 . La tarea es generar una array B[] tal que B[i] sea ⌊A[i] / 2⌋ o ⌈A[i] / 2⌉ para todas las i válidas y B[0] + B[1] + B[2] + … + B[N – 1] = 0 .
Ejemplos:
Entrada: A[] = {1, 2, -5, 3, -1}
Salida: 0 1 -2 1 0
Entrada: A[] = {3, -5, -7, 9, 2, -2}
Salida : 1 -2 -4 5 1 -1
Enfoque: Para enteros pares, es seguro asumir que B[i] será A[i] / 2 pero para enteros impares, para mantener la suma igual a cero, tome el techo de exactamente la mitad de los enteros impares y el piso de exactamente otros medio enteros impares. Dado que Impar – Impar = Par y Par – Par = Par y 0 también es Par, se puede decir que A[] siempre contendrá un número par de enteros impares, por lo que la suma puede ser 0 . Entonces, para una entrada válida, siempre habrá una respuesta.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print // the array elements void printArr(int arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; } // Function to generate and print // the required array void generateArr(int arr[], int n) { // To switch the ceil and floor // function alternatively bool flip = true; // For every element of the array for (int i = 0; i < n; i++) { // If the number is odd then print the ceil // or floor value after division by 2 if (arr[i] & 1) { // Use the ceil and floor alternatively if (flip ^= true) cout << ceil((float)(arr[i]) / 2.0) << " "; else cout << floor((float)(arr[i]) / 2.0) << " "; } // If arr[i] is even then it will // be completely divisible by 2 else { cout << arr[i] / 2 << " "; } } } // Driver code int main() { int arr[] = { 3, -5, -7, 9, 2, -2 }; int n = sizeof(arr) / sizeof(int); generateArr(arr, n); return 0; }
Java
// Java implementation of the approach // Utility function to print // the array elements import java.util.*; import java.lang.*; class GFG { static void printArr(int arr[], int n) { for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Function to generate and print // the required array static void generateArr(int arr[], int n) { // To switch the ceil and floor // function alternatively boolean flip = true; // For every element of the array for (int i = 0; i < n; i++) { // If the number is odd then print the ceil // or floor value after division by 2 if ((arr[i] & 1) != 0) { // Use the ceil and floor alternatively if (flip ^= true) System.out.print((int)(Math.ceil(arr[i] / 2.0)) + " "); else System.out.print((int)(Math.floor(arr[i] / 2.0)) + " "); } // If arr[i] is even then it will // be completely divisible by 2 else { System.out.print(arr[i] / 2 +" "); } } } // Driver code public static void main(String []args) { int arr[] = { 3, -5, -7, 9, 2, -2 }; int n = arr.length; generateArr(arr, n); } } // This code is contributed by Surendra_Gangwar
Python3
# Python3 implementation of the approach from math import ceil, floor # Utility function to print # the array elements def printArr(arr, n): for i in range(n): print(arr[i], end = " ") # Function to generate and print # the required array def generateArr(arr, n): # To switch the ceil and floor # function alternatively flip = True # For every element of the array for i in range(n): # If the number is odd then print the ceil # or floor value after division by 2 if (arr[i] & 1): # Use the ceil and floor alternatively flip ^= True if (flip): print(int(ceil((arr[i]) / 2)), end = " ") else: print(int(floor((arr[i]) / 2)), end = " ") # If arr[i] is even then it will # be completely divisible by 2 else: print(int(arr[i] / 2), end = " ") # Driver code arr = [3, -5, -7, 9, 2, -2] n = len(arr) generateArr(arr, n) # This code is contributed by Mohit Kumar
C#
// C# implementation of the approach // Utility function to print // the array elements using System; using System.Collections.Generic; class GFG { static void printArr(int []arr, int n) { for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Function to generate and print // the required array static void generateArr(int []arr, int n) { // To switch the ceil and floor // function alternatively bool flip = true; // For every element of the array for (int i = 0; i < n; i++) { // If the number is odd then print the ceil // or floor value after division by 2 if ((arr[i] & 1) != 0) { // Use the ceil and floor alternatively if (flip ^= true) Console.Write((int)(Math.Ceiling(arr[i] / 2.0)) + " "); else Console.Write((int)(Math.Floor(arr[i] / 2.0)) + " "); } // If arr[i] is even then it will // be completely divisible by 2 else { Console.Write(arr[i] / 2 +" "); } } } // Driver code public static void Main(String []args) { int []arr = { 3, -5, -7, 9, 2, -2 }; int n = arr.Length; generateArr(arr, n); } } // This code is contributed by 29AjayKumar
Javascript
<script> // javascript implementation of the approach // Utility function to print // the array elements function printArr(arr , n) { for (i = 0; i < n; i++) document.write(arr[i] + " "); } // Function to generate and print // the required array function generateArr(arr , n) { // To switch the ceil and floor // function alternatively var flip = true; // For every element of the array for (i = 0; i < n; i++) { // If the number is odd then print the ceil // or floor value after division by 2 if ((arr[i] & 1) != 0) { // Use the ceil and floor alternatively if (flip ^= true) document.write(parseInt( (Math.ceil(arr[i] / 2.0))) + " "); else document.write(parseInt( (Math.floor(arr[i] / 2.0))) + " "); } // If arr[i] is even then it will // be completely divisible by 2 else { document.write(arr[i] / 2 + " "); } } } // Driver code var arr = [ 3, -5, -7, 9, 2, -2 ]; var n = arr.length; generateArr(arr, n); // This code is contributed by todaysgaurav </script>
1 -2 -4 5 1 -1
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por mukulbindal170299 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA