Elementos mínimos que se eliminarán de modo que la suma de los elementos adyacentes sea siempre par

Dada una array de N enteros. La tarea es eliminar el número mínimo de elementos tal que en la array resultante la suma de dos valores adyacentes cualesquiera sea par.

Ejemplos: 

Input : arr[] = {1, 2, 3}
Output : 1
Remove 2 from the array.

Input : arr[] = {1, 3, 5, 4, 2}
Output : 2
Remove 4 and 2.

Método: La suma de 2 números es par si ambos son impares o ambos son pares. Esto significa que por cada par de números consecutivos que tengan diferente paridad, eliminar uno de ellos.
Entonces, para que los elementos adyacentes sumen pares, todos los elementos deben ser pares o impares. Así que el siguiente algoritmo codicioso funciona:

  • Revisa todos los elementos en orden.
  • Cuente los elementos pares e impares en la array.
  • Devuelve el recuento mínimo.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum number of eliminations
// such that sum of all adjacent elements is even
int min_elimination(int n, int arr[])
{
    int countOdd = 0;
 
    // Stores the new value
    for (int i = 0; i < n; i++)
 
        // Count odd numbers
        if (arr[i] % 2)
            countOdd++;
 
    // Return the minimum of even and
    // odd count
    return min(countOdd, n - countOdd);
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 7, 9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << min_elimination(n, arr);
 
    return 0;
}

Java

// Java implementation of the above approach
class GFG
{
     
// Function to find minimum number of
// eliminations such that sum of all
// adjacent elements is even
static int min_elimination(int n, int arr[])
{
    int countOdd = 0;
 
    // Stores the new value
    for (int i = 0; i < n; i++)
 
        // Count odd numbers
        if (arr[i] % 2 == 1)
            countOdd++;
 
    // Return the minimum of even
    // and odd count
    return Math.min(countOdd, n - countOdd);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 7, 9 };
    int n = arr.length;
 
    System.out.println(min_elimination(n, arr));
}
}
 
// This code is contributed by Code_Mech

Python3

# Python 3 implementation of the
# above approach
 
# Function to find minimum number of
# eliminations such that sum of all
# adjacent elements is even
def min_elimination(n, arr):
    countOdd = 0
 
    # Stores the new value
    for i in range(n):
         
        # Count odd numbers
        if (arr[i] % 2):
            countOdd += 1
 
    # Return the minimum of even and
    # odd count
    return min(countOdd, n - countOdd)
 
# Driver code
if __name__ == '__main__':
    arr = [1, 2, 3, 7, 9]
    n = len(arr)
 
    print(min_elimination(n, arr))
 
# This code is contributed by
# Surendra_Gangwar

C#

// C# implementation of the above approach
using System;
 
class GFG
{
     
// Function to find minimum number of
// eliminations such that sum of all
// adjacent elements is even
static int min_elimination(int n, int[] arr)
{
    int countOdd = 0;
 
    // Stores the new value
    for (int i = 0; i < n; i++)
 
        // Count odd numbers
        if (arr[i] % 2 == 1)
            countOdd++;
 
    // Return the minimum of even
    // and odd count
    return Math.Min(countOdd, n - countOdd);
}
 
// Driver code
public static void Main()
{
    int[] arr = { 1, 2, 3, 7, 9 };
    int n = arr.Length;
 
    Console.WriteLine(min_elimination(n, arr));
}
}
 
// This code is contributed by Code_Mech

PHP

<?php
// PHP implementation of the above approach
 
// Function to find minimum number of
// eliminations such that sum of all
// adjacent elements is even
function min_elimination($n, $arr)
{
    $countOdd = 0;
 
    // Stores the new value
    for ($i = 0; $i < $n; $i++)
 
        // Count odd numbers
        if ($arr[$i] % 2 == 1)
            $countOdd++;
 
    // Return the minimum of even
    // and odd count
    return min($countOdd, $n - $countOdd);
}
 
// Driver code
$arr = array(1, 2, 3, 7, 9);
$n = sizeof($arr);
 
echo(min_elimination($n, $arr));
 
// This code is contributed by Code_Mech
?>

Javascript

<script>
 
 
// Function to find minimum number of eliminations
// such that sum of all adjacent elements is even
function min_elimination(n, arr)
{
    let countOdd = 0;
 
    // Stores the new value
    for (let i = 0; i < n; i++)
 
        // Count odd numbers
        if (arr[i] % 2)
            countOdd++;
 
    // Return the minimum of even and
    // odd count
    return Math.min(countOdd, n - countOdd);
}
 
// Driver code
 
let arr= [1, 2, 3, 7, 9];
let n = arr.length;
 
document.write(min_elimination(n, arr));
 
</script>
Producción: 

1

 

Complejidad de tiempo: O(N)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Shivam.Pradhan y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *