Compruebe si Sum y XOR de todos los elementos de la array son iguales

Dada una array arr[] , la tarea es comprobar si la suma de todos los elementos de una array es igual a XOR de todos los elementos de la array. 
Ejemplo: 
 

Entrada: arr[] = [1, 2] 
Salida: SÍ 
Explicación: 
Suma = (1+2) = 3 
XOR = (1^2) = 3
Entrada: arr[] = [6, 3, 7, 10] 
Salida : NO 
Explicación: 
Suma = (6 + 3 + 7 + 10) = 26 
XOR = (6 ^ 3 ^ 7 ^ 10) = 8 
 

Acercarse: 
 

  1. Iterar sobre la array y encontrar la suma de todos los elementos.
  2. Del mismo modo, XOR todos los elementos de la array.
  3. Compruebe si la suma y el valor de xor son iguales.

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

C++

// C++ Implementation to Check
// if Sum and XOR of all Elements
// of Array is equal
#include <iostream>
using namespace std;
 
// Function to Check if Sum and XOR
// of all elements of array is equal
int equal_xor_sum(int arr[], int n)
{
    int Sum = 0;
    int Xor = 0;
 
    // Sum and XOR of all elements
    for (int i = 0; i < n; i++) {
        Sum = Sum + arr[i];
        Xor = Xor ^ arr[i];
    }
 
    // Checking Sum and XOR to be equal
    if (Sum == Xor)
        cout << "YES";
    else
        cout << "NO";
 
    return 0;
}
 
// Driver Function
int main()
{
    int arr[] = { 6, 3, 7, 10 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Check Sum and XOR is equal
    equal_xor_sum(arr, n);
 
    return 0;
}

Java

// Java Implementation to Check
// if Sum and XOR of all Elements
// of Array is equal
class GFG
{
     
    // Function to Check if Sum and XOR
    // of all elements of array is equal
    static void equal_xor_sum(int arr[], int n)
    {
        int Sum = 0;
        int Xor = 0;
     
        // Sum and XOR of all elements
        for (int i = 0; i < n; i++)
        {
            Sum = Sum + arr[i];
            Xor = Xor ^ arr[i];
        }
     
        // Checking Sum and XOR to be equal
        if (Sum == Xor)
            System.out.println("YES");
        else
            System.out.println("NO");
 
    }
     
    // Driver Function
    public static void main (String[] args)
    {
        int arr[] = { 6, 3, 7, 10 };
        int n = arr.length;
     
        // Check Sum and XOR is equal
        equal_xor_sum(arr, n);
    }
}
 
// This code is contributed by AnkitRai01

Python3

# Python3 Implementation to Check
# if Sum and XOR of all Elements
# of Array is equal
 
# Function to Check if Sum and XOR
# of all elements of array is equal
def equal_xor_sum(arr, n) :
 
    Sum = 0;
    Xor = 0;
 
    # Sum and XOR of all elements
    for i in range(n) :
        Sum = Sum + arr[i];
        Xor = Xor ^ arr[i];
 
    # Checking Sum and XOR to be equal
    if (Sum == Xor) :
        print("YES");
    else :
        print("NO");
 
# Driver Function
if __name__ == "__main__" :
 
    arr = [ 6, 3, 7, 10 ];
    n = len(arr);
 
    # Check Sum and XOR is equal
    equal_xor_sum(arr, n);
 
# This code is contributed by AnkitRai01

C#

// C# Implementation to Check
// if Sum and XOR of all Elements
// of Array is equal
using System;
 
class GFG
{
     
    // Function to Check if Sum and XOR
    // of all elements of array is equal
    static void equal_xor_sum(int []arr, int n)
    {
        int Sum = 0;
        int Xor = 0;
     
        // Sum and XOR of all elements
        for (int i = 0; i < n; i++)
        {
            Sum = Sum + arr[i];
            Xor = Xor ^ arr[i];
        }
     
        // Checking Sum and XOR to be equal
        if (Sum == Xor)
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
 
    }
     
    // Driver Function
    public static void Main()
    {
        int []arr = { 6, 3, 7, 10 };
        int n = arr.Length;
     
        // Check Sum and XOR is equal
        equal_xor_sum(arr, n);
    }
}
 
// This code is contributed by AnkitRai01

Javascript

<script>
 
// JavaScript Implementation to Check
// if Sum and XOR of all Elements
// of Array is equal
 
 
// Function to Check if Sum and XOR
// of all elements of array is equal
function equal_xor_sum(arr, n)
{
    let Sum = 0;
    let Xor = 0;
 
    // Sum and XOR of all elements
    for (let i = 0; i < n; i++) {
        Sum = Sum + arr[i];
        Xor = Xor ^ arr[i];
    }
 
    // Checking Sum and XOR to be equal
    if (Sum === Xor)
        document.write("YES");
    else
        document.write("NO");
 
}
 
// Driver Function
 
    let arr = [ 6, 3, 7, 10 ];
    let n = arr.length;
 
    // Check Sum and XOR is equal
    equal_xor_sum(arr, n);
 
// This code is contributed by Surbhi Tyagi.
 
</script>
Producción: 

NO

 

Complejidad de tiempo: O(n)
 

Publicación traducida automáticamente

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