Dada una array arr[] enteros positivos, la tarea es encontrar el número de formas de convertir la suma de la array incluso si se nos permite eliminar solo un elemento.
Ejemplos:
Entrada: arr[] = { 1, 3, 3, 2 }
Salida: 3
Explicación:
1. Quite 1, luego la suma es 3 + 3 + 2 = 8.
2. Quite 3, luego la suma es 1 + 3 + 2 = 6.
3. Quitar 3, entonces la suma es 1 + 3 + 2 = 6.
Entrada: arr[] = { 4, 8, 3, 3, 6 }
Salida: 3
Explicación:
1. Quitar 4, entonces la suma es 8 + 3 + 3 + 6 = 20.
2. Quite 8, entonces la suma es 4 + 3 + 3 + 6 = 16.
3. Quite 6, entonces la suma es 4 + 8 + 3 + 3 = 18.
Enfoque: La observación clave de la declaración del problema anterior es:
- Si tenemos un número impar de elementos impares, entonces la suma siempre es impar, entonces tenemos que eliminar un número impar de la array arr[] para que la suma sea par. Como tenemos que eliminar un elemento, el número total de formas de hacer que la suma sea par es el número de elementos impares en el arreglo arr[] .
- Si tenemos un número par de elementos impares, la suma siempre es par. Dado que tenemos que eliminar un elemento para que la suma sea par, el número total de formas de hacer que la suma sea par es el número de elementos pares en el arreglo arr[]
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 a number of ways // to make array element sum even by // removing one element int find_num_of_ways(int arr[], int N) { int count_even = 0, count_odd = 0; // Finding the count of even // and odd elements for (int i = 0; i < N; i++) { if (arr[i] % 2) { count_odd++; } else { count_even++; } } // If count_odd is odd then // no. of ways is count_odd if (count_odd % 2) { return count_odd; } // Else no. of ways is count_even else { return count_even; } } // Driver Code int main() { // Given array arr[] int arr[] = { 1, 3, 3, 2 }; int N = sizeof(arr) / sizeof(arr[0]); // Function Call cout << find_num_of_ways(arr, N); return 0; }
Java
// Java program for the above approach import java.util.*; class GFG{ // Function to find a number of ways // to make array element sum even by // removing one element static int find_num_of_ways(int arr[], int N) { int count_even = 0, count_odd = 0; // Finding the count of even // and odd elements for(int i = 0; i < N; i++) { if (arr[i] % 2 == 1) { count_odd++; } else { count_even++; } } // If count_odd is odd then // no. of ways is count_odd if (count_odd % 2 == 1) { return count_odd; } // Else no. of ways is count_even else { return count_even; } } // Driver Code public static void main (String[] args) { // Given array arr[] int arr[] = { 1, 3, 3, 2 }; int N = 4; // Function call System.out.print(find_num_of_ways(arr, N)); } } // This code is contributed by Ritik Bansal
Python3
# Python3 program for the above approach # Function to find a number of ways # to make array element sum even by # removing one element def find_num_of_ways(arr, N): count_even = 0 count_odd = 0 # Finding the count of even # and odd elements for i in range(N): if (arr[i] % 2): count_odd += 1 else: count_even += 1 # If count_odd is odd then # no. of ways is count_odd if (count_odd % 2): return count_odd # Else no. of ways is count_even else: return count_even # Driver Code if __name__ == '__main__': # Given array arr[] arr = [ 1, 3, 3, 2 ] N = len(arr) # Function call print(find_num_of_ways(arr, N)) # This code is contributed by mohit kumar 29
C#
// C# program for the above approach using System; class GFG{ // Function to find a number of ways // to make array element sum even by // removing one element static int find_num_of_ways(int []arr, int N) { int count_even = 0, count_odd = 0; // Finding the count of even // and odd elements for(int i = 0; i < N; i++) { if (arr[i] % 2 == 1) { count_odd++; } else { count_even++; } } // If count_odd is odd then // no. of ways is count_odd if (count_odd % 2 == 1) { return count_odd; } // Else no. of ways is count_even else { return count_even; } } // Driver Code public static void Main(string[] args) { // Given array arr[] int []arr = { 1, 3, 3, 2 }; int N = 4; // Function call Console.Write(find_num_of_ways(arr, N)); } } // This code is contributed by Rutvik
Javascript
<script> // javascript program for the above approach // Function to find a number of ways // to make array element sum even by // removing one element function find_num_of_ways(arr , N) { var count_even = 0, count_odd = 0; // Finding the count of even // and odd elements for(i = 0; i < N; i++) { if (arr[i] % 2 == 1) { count_odd++; } else { count_even++; } } // If count_odd is odd then // no. of ways is count_odd if (count_odd % 2 == 1) { return count_odd; } // Else no. of ways is count_even else { return count_even; } } // Driver Code // Given array arr var arr = [ 1, 3, 3, 2 ]; var N = 4; // Function call document.write(find_num_of_ways(arr, N)); // This code is contributed by Amit Katiyar </script>
3
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por IshwarGupta y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA