Given an array arr[], the task is to check if sum of all elements of an array is equal to XOR of all elements of array.
Example:
Input: arr[] = [1, 2]
Output: YES
Explanation:
Sum = (1+2) = 3
XOR = (1^2) = 3Input: arr[] = [6, 3, 7, 10]
Output: NO
Explanation:
Sum = (6 + 3 + 7 + 10) = 26
XOR = (6 ^ 3 ^ 7 ^ 10) = 8
Approach:
- Iterate over the Array and find sum of all elements.
- Similarly, XOR all the elements of the array.
- Check if sum and xor value is equal.
Below is the implementation of the above approach:
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> |
NO
Time Complexity: O(n)
Auxiliary Space: O(1) because it is using constant space for variables
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!