Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Array containing power of 2 whose XOR and Sum of elements equals X
Next article icon

Check if Sum and XOR of all elements of array is equal

Last Updated : 21 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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) = 3

Input: arr[] = [6, 3, 7, 10] 
Output: NO 
Explanation: 
Sum = (6 + 3 + 7 + 10) = 26 
XOR = (6 ^ 3 ^ 7 ^ 10) = 8 
 

 

Approach: 
 

  1. Iterate over the Array and find sum of all elements.
  2. Similarly, XOR all the elements of the array.
  3. 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>
 
 
Output: 
NO

 

Time Complexity: O(n)
Auxiliary Space: O(1) because it is using constant space for variables
 



Next Article
Array containing power of 2 whose XOR and Sum of elements equals X
author
skylags
Improve
Article Tags :
  • Arrays
  • Competitive Programming
  • DSA
  • Mathematical
  • Bitwise-XOR
Practice Tags :
  • Arrays
  • Mathematical

Similar Reads

  • Sum of all odd frequency elements in an array
    Given an array of integers containing duplicate elements. The task is to find the sum of all odd occurring elements in the given array. That is the sum of all such elements whose frequency is odd in the array. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3} Output : 9 The odd occurring element is 3,
    8 min read
  • Check if the array has an element which is equal to sum of all the remaining elements
    Given an array of N elements, the task is to check if the array has an element that is equal to the sum of all the remaining elements. Examples: Input: a[] = {5, 1, 2, 2} Output: Yes we can write 5=(1+2+2) Input: a[] = {2, 1, 2, 4, 3} Output: No Approach: Suppose that the total elements in the array
    10 min read
  • Find XOR of all elements in an Array
    Given an array arr[] containing integers of size N, the task is to find the XOR of this array.Examples: Input: arr[] = {2, 4, 7} Output: 1 Explanation: XOR of the array = 2 ^ 4 ^ 7 = 1Input: arr[] = { 3, 9, 12, 13, 15 } Output: 4 Approach: In order to find the XOR of all elements in the array, we si
    5 min read
  • XOR of all elements of array with set bits equal to K
    Given an array of integers and a number K. The task is to find the XOR of only those elements of the array whose total set bits are equal to K. Examples: Input : arr[] = {1, 22, 3, 10}, K=1 Output : 1 Elements with set bits equal to 1 is 1. So, XOR is also 1. Input : arr[] = {3, 4, 10, 5, 8}, K=2 Ou
    4 min read
  • Array containing power of 2 whose XOR and Sum of elements equals X
    Given an integer X. The task is to find and return the array containing of powers of 2's and the xor of the array is X. Examples: Input: X = 20 Output: 16 4 Input: X = 15 Output: 1 2 4 8 Approach: The answer lies in the binary representation of the number X.Since in the power of 2, there is only one
    4 min read
  • Check if the array has an element which is equal to XOR of remaining elements
    Given an array arr[] of N elements, the task is to check if the array has an element which is equal to the XOR of all the remaining elements.Examples: Input: arr[] = { 8, 2, 4, 15, 1 } Output: Yes 8 is the required element as 2 ^ 4 ^ 15 ^ 1 = 8.Input: arr[] = {4, 2, 3} Output: No Approach: First, ta
    5 min read
  • Sum of XOR of all sub-arrays of length K
    Given an array of length n ( n > k), we have to find the sum of xor of all the elements of the sub-arrays which are of length k.Examples: Input : arr[]={1, 2, 3, 4}, k=2 Output :Sum= 11 Sum = 1^2 + 2^3 + 3^4 = 3 + 1 + 7 =11Input :arr[]={1, 2, 3, 4}, k=3 Output :Sum= 5 Sum = 1^2^3 + 2^3^4 = 0 + 5
    8 min read
  • Find array elements equal to sum of any subarray of at least size 2
    Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.Examples: Input: arr[] = {1, 2, 3, 4, 5, 6} Output: 3, 5, 6 Explanation: The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectivel
    6 min read
  • Sum of all even occurring element in an array
    Given an array of integers containing duplicate elements. The task is to find the sum of all even occurring elements in the given array. That is the sum of all such elements whose frequency is even in the array. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3}Output : 6The even occurring element are
    12 min read
  • Check if each element of an Array is the Sum of any two elements of another Array
    Given two arrays A[] and B[] consisting of N integers, the task is to check if each element of array B[] can be formed by adding any two elements of array A[]. If it is possible, then print “Yes”. Otherwise, print “No”. Examples: Input: A[] = {3, 5, 1, 4, 2}, B[] = {3, 4, 5, 6, 7} Output: Yes Explan
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences