Cuente el número de elementos pares e impares en una array

Para la array dada de enteros, cuente los elementos pares e impares.

Ejemplos: 

Input: 
int arr[5] = {2, 3, 4, 5, 6}
Output: 
Number of even elements = 3    
Number of odd elements = 2  

Input:
int arr[5] = {22, 32, 42, 52, 62}
Output: 
Number of even elements = 5  
Number of odd elements = 0

Solución: También podemos comprobar si un número es par o impar

  • Al hacer AND de 1 y ese dígito, si el resultado es 1, entonces el número es impar, de lo contrario, es par.
  • Por su divisibilidad por 2. Se dice que un número es impar si no es divisible por 2, de lo contrario es par.

Aquí, verificaremos si un número es impar, luego incrementaremos el contador impar; de lo contrario, incrementaremos el contador par. 

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

C++

// CPP program to count number of even
// and odd elements in an array
#include <iostream>
using namespace std;
 
void CountingEvenOdd(int arr[], int arr_size)
{
    int even_count = 0;
    int odd_count = 0;
 
    // loop to read all the values in the array
    for (int i = 0; i < arr_size; i++) {
         
          // checking if a number is completely
        // divisible by 2
        if (arr[i] & 1 == 1)
            odd_count++;
        else
            even_count++;
    }
 
    cout << "Number of even elements = " << even_count
         << "\nNumber of odd elements = " << odd_count;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
       
      // Function Call
    CountingEvenOdd(arr, n);
}

Java

// JAVA program to count number of even
// and odd elements in an array
import java.io.*;
 
class GFG {
 
    static void CountingEvenOdd(int arr[], int arr_size)
    {
        int even_count = 0;
        int odd_count = 0;
 
        // loop to read all the values in
        // the array
        for (int i = 0; i < arr_size; i++) {
             
              // checking if a number is
            // completely divisible by 2
            if ((arr[i] & 1) == 1)
                odd_count++;
            else
                even_count++;
        }
 
        System.out.println("Number of even"
                           + " elements = " + even_count
                           + " Number of odd elements = "
                           + odd_count);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 2, 3, 4, 5, 6 };
        int n = arr.length;
           
          // Function Call
        CountingEvenOdd(arr, n);
    }
}
 
// This code is Contributed by anuj_67.

Python3

# Python3 program to count number of
# even and odd elements in an array
 
 
def CountingEvenOdd(arr, arr_size):
    even_count = 0
    odd_count = 0
 
    # loop to read all the values
    # in the array
    for i in range(arr_size):
 
        # checking if a number is
        # completely divisible by 2
        if (arr[i] & 1 == 1):
            odd_count += 1
        else:
            even_count += 1
 
    print("Number of even elements = ",
          even_count)
    print("Number of odd elements = ",
          odd_count)
 
 
# Driver Code
arr = [2, 3, 4, 5, 6]
n = len(arr)
 
# Function Call
CountingEvenOdd(arr, n)
 
# This code is contributed by sahishelangia

C#

// C# program to count number of even
// and odd elements in an array
using System;
 
class GFG {
 
    static void CountingEvenOdd(int[] arr, int arr_size)
    {
        int even_count = 0;
        int odd_count = 0;
 
        // loop to read all the values in
        // the array
        for (int i = 0; i < arr_size; i++) {
             
              // checking if a number is
            // completely divisible by 2
            if ((arr[i] & 1) == 1)
                odd_count++;
            else
                even_count++;
        }
 
        Console.WriteLine("Number of even"
                          + " elements = " + even_count
                          + " Number of odd elements = "
                          + odd_count);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 2, 3, 4, 5, 6 };
        int n = arr.Length;
       
          // Function Call
        CountingEvenOdd(arr, n);
    }
}
 
// This code is Contributed by anuj_67.

PHP

<?php
// PHP program to count number of even
// and odd elements in an array
 
function CountingEvenOdd( $arr, $arr_size)
{
    $even_count = 0;        
    $odd_count = 0;            
         
    // loop to read all the values in
    // the array
    for( $i = 0 ; $i < $arr_size ; $i++)
    {
        // checking if a number is
        // completely divisible by 2
        if ($arr[$i] & 1 == 1)
            $odd_count ++ ;    
        else               
            $even_count ++ ;        
    }
 
    echo "Number of even elements = " ,
        $even_count," Number of odd " ,
            "elements = " ,$odd_count ;    
}
 
// Driver Code
    $arr = array(2, 3, 4, 5, 6);
    $n = count($arr);
 
    // Function Call
    CountingEvenOdd($arr, $n);
 
// This code is Contributed by anuj_67.
?>

Javascript

<script>
 
// Javascript program to count number of even
// and odd elements in an array
 
function CountingEvenOdd(arr, arr_size)
{
    let even_count = 0;
    let odd_count = 0;
 
    // loop to read all the values in the array
    for (let i = 0; i < arr_size; i++) {
         
        // checking if a number is completely
        // divisible by 2
        if (arr[i] & 1 == 1)
            odd_count++;
        else
            even_count++;
    }
 
    document.write("Number of even elements = " + even_count
        + "<br>" + "Number of odd elements = " + odd_count);
}
 
// Driver Code
 
    let arr = [ 2, 3, 4, 5, 6 ];
    let n = arr.length;
     
    // Function Call
    CountingEvenOdd(arr, n);
 
// This code is contributed by Mayank Tyagi
 
</script>
Producción

Number of even elements = 3
Number of odd elements = 2

Complejidad de tiempo: O(n)

Publicación traducida automáticamente

Artículo escrito por mohitw16 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 *