Cuente el número de elementos de longitud par e impar en una array

Dada una array arr[] de enteros de tamaño N , la tarea es encontrar los elementos numéricos de la array que tienen longitudes pares e impares.
Ejemplos: 
 

Entrada: arr[] = {14, 735, 3333, 223222} 
Salida: Número de elementos de longitud par = 3 
Número de elementos de longitud impar = 1 
Entrada: arr[] = {1121, 322, 32, 14783, 44} 
Salida: Número de elementos de longitud par = 3 
Número de elementos de longitud impar = 2 
 

Enfoque: para calcular la cantidad de dígitos que tienen una longitud par o impar, convierta cada número en una string . Luego verifica si la longitud es par o impar. Finalmente, imprima el conteo de números que tienen longitudes pares e impares por separado.
A continuación se muestra la implementación del enfoque anterior: 
 

CPP

// C++ program to find the count
// number of even and odd
// length elements in an Array
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the number elements of
// the array having even length and odd.
void EvenOddLength(int arr[], int n)
{
    // Store numbers with even length
    int even = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Conversion of integer to string
        string x = to_string(arr[i]);
 
        if (x.length() % 2 == 0)
            even++;
    }
 
    cout << "Number of even "
         << "length elements = "
         << even << endl;
    cout << "Number of odd "
         << "length elements = "
         << n - even << endl;
}
 
// Driver code
int main()
{
    int arr[] = { 12, 44, 213, 232, 3433 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    EvenOddLength(arr, n);
 
    return 0;
}

Java

// Java program to find the count
// number of even and odd
// length elements in an Array
import java.util.*;
class GFG{
 
// Function to find the number elements of
// the array having even length and odd.
    static void EvenOddLength(int arr[], int n)
    {
        // Store numbers with even length
        int even = 0;
     
        for (int i = 0; i < n; i++) {
     
            // Conversion of integer to string
            String x = Integer.toString(arr[i]);
     
            if (x.length() % 2 == 0)
                even++;
        }
     
        System.out.println("Number of even length elements = "+even);
        System.out.println("Number of odd length elements = "+(n - even));
    }
     
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 12, 44, 213, 232, 3433 };
        int n = arr.length;
     
        // Function call
        EvenOddLength(arr, n);
     
    }
}
 
// This code is contributed by AbhiThakur

Python3

# Python3 program to find the count
# number of even and odd
# length elements in an Array
 
# Function to find the number elements of
# the array having even length and odd.
def EvenOddLength(arr, n):
     
    # Store numbers with even length
    even = 0
 
    for i in range(n):
 
        # Conversion of integer to string
        x = str(arr[i])
 
        if (len(x) % 2 == 0):
            even += 1
 
    print( "Number of even length elements = ", even)
    print( "Number of odd length elements = ", n - even)
 
# Driver code
if __name__ == '__main__':
    arr= [12, 44, 213, 232, 3433]
    n = len(arr)
 
    # Function call
    EvenOddLength(arr, n)
 
# This code is contributed by mohit kumar 29

C#

// C# program to find the count
// number of even and odd
// length elements in an Array
using System;
 
class GFG{
  
// Function to find the number elements of
// the array having even length and odd.
    static void EvenOddLength(int []arr, int n)
    {
        // Store numbers with even length
        int even = 0;
      
        for (int i = 0; i < n; i++) {
      
            // Conversion of integer to string
            String x = arr[i].ToString();
      
            if (x.Length % 2 == 0)
                even++;
        }
      
        Console.WriteLine("Number of even length elements = "+even);
        Console.WriteLine("Number of odd length elements = "+(n - even));
    }
      
    // Driver code
    public static void Main(String []args)
    {
        int []arr = { 12, 44, 213, 232, 3433 };
        int n = arr.Length;
      
        // Function call
        EvenOddLength(arr, n);
      
    }
}
 
// This code is contributed by sapnasingh4991

Javascript

<script>
 
// Javascript program to find the count
// number of even and odd
// length elements in an Array
 
// Function to find the number elements of
// the array having even length and odd.
function EvenOddLength(arr, n)
{
 
    // Store numbers with even length
    let even = 0;
 
    for (let i = 0; i < n; i++)
    {
 
        // Conversion of integer to string
        let x = arr[i].toString();
 
        if ((x.length) % 2 == 0)
            even++;
    }
 
    document.write("Number of even "
        + "length elements = "
        + even + "<br>");
    document.write("Number of odd "
        + "length elements = ");
    document.write(n - even + "<br>");
}
 
// Driver code
    let arr = [ 12, 44, 213, 232, 3433 ];
    let n = arr.length;
 
    // Function call
    EvenOddLength(arr, n);
 
// This code is contributed by Mayank Tyagi
</script>
Producción: 

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

 

Publicación traducida automáticamente

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