Suma de elementos de un arreglo que tiene paridad par

Dada una array arr[] , la tarea es calcular la suma de los elementos de la array dada que tiene paridad par, es decir, el número de bits establecidos es par usando el operador bit a bit .

Ejemplos:  

Entrada: arr[] = {2, 4, 3, 5, 9} 
Salida: 17 
Solo 3(0011), 5(0101) y 9(1001) tienen paridad par 
Así que 3 + 5 + 9 = 17

Entrada: arr[] = {1, 5, 4, 16, 10} 
Salida: 15 
 

Enfoque: inicialice una variable sum = 0 y recorra la array de 0 a n – 1 mientras cuenta la cantidad de bits establecidos en arr[i] utilizando el algoritmo de Brian Kernighan . Si el conteo es par , actualice sum = sum + arr[i] . Imprime la suma al final.

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

C++

// C++ program to find the sum of the elements
// from an array which have even parity
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns true if x has even parity
bool checkEvenParity(int x)
{
    // We basically count set bits
    // https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
    int parity = 0;
    while (x != 0) {
        x = x & (x - 1);
        parity++;
    }
 
    if (parity % 2 == 0)
        return true;
    else
        return false;
}
 
// Function to return the sum of the elements
// from an array which have even parity
long sumlist(int a[], int n)
{
    long sum = 0;
 
    for (int i = 0; i < n; i++) {
 
        // If a[i] has even parity
        if (checkEvenParity(a[i]))
            sum += a[i];
    }
    return sum;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 4, 3, 5, 9 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << sumlist(arr, n);
    return 0;
}

Java

// Java program to find the sum of the elements
// from an array which have even parity
 
import java.io.*;
 
class GFG {
 
// Function that returns true if x has even parity
static boolean checkEvenParity(int x)
{
    // We basically count set bits
    // https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
    int parity = 0;
    while (x != 0) {
        x = x & (x - 1);
        parity++;
    }
 
    if (parity % 2 == 0)
        return true;
    else
        return false;
}
 
// Function to return the sum of the elements
// from an array which have even parity
static long sumlist(int a[], int n)
{
    long sum = 0;
 
    for (int i = 0; i < n; i++) {
 
        // If a[i] has even parity
        if (checkEvenParity(a[i]))
            sum += a[i];
    }
    return sum;
}
 
// Driver code
 
    public static void main (String[] args) {
            int arr[] = { 2, 4, 3, 5, 9 };
 
    int n =arr.length;
 
    System.out.println(sumlist(arr, n));
    }
}
// This code is contributed by  inder_verma..

Python3

# Python3 program to find the sum of the elements
# from an array which have even parity
 
# Function that returns true if x
# has even parity
def checkEvenParity(x):
     
    # We basically count set bits
    # https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
    parity = 0
    while (x != 0):
        x = x & (x - 1)
        parity += 1
 
    if (parity % 2 == 0):
        return True
    else:
        return False
 
# Function to return the sum of the elements
# from an array which have even parity
def sumlist(a, n):
    sum = 0
    for i in range(n):
         
        # If a[i] has even parity
        if (checkEvenParity(a[i])):
            sum += a[i]
    return sum
 
# Driver code
if __name__ == '__main__':
    arr = [ 2, 4, 3, 5, 9 ]
    n = len(arr)
    print(sumlist(arr, n))
 
# This code is contributed by 29AjayKumar.

C#

// C# program to find the sum of the elements
// from an array which have even parity
 
using System ;
 
class GFG {
 
// Function that returns true if x has even parity
static bool checkEvenParity(int x)
{
    // We basically count set bits
    // https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
    int parity = 0;
    while (x != 0) {
        x = x & (x - 1);
        parity++;
    }
 
    if (parity % 2 == 0)
        return true;
    else
        return false;
}
 
// Function to return the sum of the elements
// from an array which have even parity
static long sumlist(int []a, int n)
{
    long sum = 0;
 
    for (int i = 0; i < n; i++) {
 
        // If a[i] has even parity
        if (checkEvenParity(a[i]))
            sum += a[i];
    }
    return sum;
}
 
// Driver code
 
    public static void Main () {
            int []arr = { 2, 4, 3, 5, 9 };
 
    int n =arr.Length;
 
    Console.WriteLine(sumlist(arr, n));
    }
    // This code is contributed by Ryuga
}

PHP

<?php
// PHP program to find the sum of the
// elements from an array which have
// even parity
 
// Function that returns true
// if x has even parity
function checkEvenParity($x)
{
    // We basically count set bits
    // https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
    $parity = 0;
    while ($x != 0)
    {
        $x = ($x & ($x - 1));
        $parity++;
    }
 
    if ($parity % 2 == 0)
        return true;
    else
        return false;
}
 
// Function to return the sum of the elements
// from an array which have even parity
function sumlist($a, $n)
{
    $sum = 0;
 
    for ($i = 0; $i < $n; $i++)
    {
 
        // If a[i] has even parity
        if (checkEvenParity($a[$i]))
            $sum += $a[$i];
    }
    return $sum;
}
 
// Driver code
$arr = array( 2, 4, 3, 5, 9 );
$n = sizeof($arr);
echo sumlist($arr, $n);
     
// This code is contributed by ajit.
?>

Javascript

<script>
 
// Javascript program to find the sum of the elements
// from an array which have even parity
 
// Function that returns true if x has even parity
function checkEvenParity(x)
{
     
    // We basically count set bits
    // https://www.geeksforgeeks.org/count-set-bits-in-an-integer/
    let parity = 0;
    while (x != 0)
    {
        x = x & (x - 1);
        parity++;
    }
   
    if (parity % 2 == 0)
        return true;
    else
        return false;
}
 
// Function to return the sum of the elements
// from an array which have even parity
function sumlist(a, n)
{
    let sum = 0;
 
    for(let i = 0; i < n; i++)
    {
         
        // If a[i] has even parity
        if (checkEvenParity(a[i]))
            sum += a[i];
    }
    return sum;
}
 
// Driver code
let arr = [ 2, 4, 3, 5, 9 ];
let n = arr.length;
 
document.write(sumlist(arr, n));
 
// This code is contributed by unknown2108
 
</script>
Producción: 

17

 

Publicación traducida automáticamente

Artículo escrito por mohit kumar 29 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 *