Dada una array de enteros arr de tamaño N . La tarea es encontrar el máximo de elementos posibles en la array que sean divisibles por 2 después de modificar la array. Uno puede realizar la siguiente operación un número arbitrario de veces (posiblemente cero veces).
Reemplace dos elementos cualquiera en la array con su suma.
Ejemplos:
Entrada: arr = [1, 2, 3, 1, 3]
Salida: 3
Después de agregar elementos en los índices 0 y 2, y en los índices 3 y 4, la array se convierte en arr=[4, 2, 4].
Entrada: arr = [1, 2, 3, 4, 5]
Salida: 3
Después de sumar 1 y 3, la array se convierte en arr=[4, 2, 4, 5].
Enfoque :
Primero, la observación es que no necesitamos modificar elementos que son divisibles por 2 (es decir, números pares). Luego nos fuimos con los números impares. La suma de dos números dará un número par que es divisible por 2.
Entonces, finalmente, el resultado será:
cuenta_par + cuenta_impar /2.
A continuación se muestra la implementación del enfoque anterior:
CPP
// CPP program to find maximum possible // elements which divisible by 2 #include <bits/stdc++.h> using namespace std; // Function to find maximum possible // elements which divisible by 2 int Divisible(int arr[], int n) { // To store count of even numbers int count_even = 0; for (int i = 0; i < n; i++) if (arr[i] % 2 == 0) count_even++; // All even numbers and half of odd numbers return count_even + (n - count_even) / 2; } // Driver code int main() { int arr[] = { 1, 2, 3, 4, 5 }; int n = sizeof(arr) / sizeof(arr[0]); // Function call cout << Divisible(arr, n); return 0; }
Java
// Java program to find maximum possible // elements which divisible by 2 class GFG { // Function to find maximum possible // elements which divisible by 2 static int Divisible(int arr[], int n) { // To store count of even numbers int count_even = 0; for (int i = 0; i < n; i++) if (arr[i] % 2 == 0) count_even++; // All even numbers and half of odd numbers return count_even + (n - count_even) / 2; } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 3, 4, 5 }; int n = arr.length; // Function call System.out.println(Divisible(arr, n)); } } // This code is contributed by AnkitRai01
Python3
# Python3 program to find maximum possible # elements which divisible by 2 # Function to find maximum possible # elements which divisible by 2 def Divisible(arr, n): # To store count of even numbers count_even = 0 for i in range(n): if (arr[i] % 2 == 0): count_even+=1 # All even numbers and half of odd numbers return count_even + (n - count_even) // 2 # Driver code arr=[1, 2, 3, 4, 5] n = len(arr) # Function call print(Divisible(arr, n)) # This code is contributed by mohit kumar 29
C#
// C# program to find maximum possible // elements which divisible by 2 using System; class GFG { // Function to find maximum possible // elements which divisible by 2 static int Divisible(int []arr, int n) { // To store count of even numbers int count_even = 0; for (int i = 0; i < n; i++) if (arr[i] % 2 == 0) count_even++; // All even numbers and half of odd numbers return count_even + (n - count_even) / 2; } // Driver code static public void Main () { int []arr = { 1, 2, 3, 4, 5 }; int n = arr.Length; // Function call Console.Write(Divisible(arr, n)); } } // This code is contributed by ajit.
Javascript
<script> // Javascript program to find maximum possible // elements which divisible by 2 // Function to find maximum possible // elements which divisible by 2 function Divisible(arr, n) { // To store count of even numbers let count_even = 0; for (let i = 0; i < n; i++) if (arr[i] % 2 == 0) count_even++; // All even numbers and half of odd numbers return count_even + parseInt((n - count_even) / 2); } // Driver code let arr = [ 1, 2, 3, 4, 5 ]; let n = arr.length; // Function call document.write(Divisible(arr, n)); </script>
3
Complejidad temporal: O(N).
Espacio Auxiliar : O(1).
Publicación traducida automáticamente
Artículo escrito por Ripunjoy Medhi y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA