📜  数组中所有对之和的XOR之和

📅  最后修改于: 2021-05-06 19:04:03             🧑  作者: Mango

给定一个数组,找到数组中所有对之和的异或。
例子:

Input  : arr[] = {1, 2, 3}
Output : 0
(1 + 1) ^ (1 + 2) ^ (1 + 3) ^ (2 + 1) ^ (2 + 2) ^ 
(2 + 3) ^ (3 + 1) ^ (3 + 2) ^ (3 + 3) = 0

Input  : arr[] = {1, 2, 3, 4}
Output : 8

一个幼稚的方法是一个一个地考虑所有对,然后一个接一个地计算它们的XOR。

C++
// CPP program to find XOR of pair
// sums.
#include 
 
using namespace std;
 
int xorPairSum(int ar[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum = sum ^ (ar[i] + ar[j]);
    return sum;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << xorPairSum(arr, n);
    return 0;
}


Java
// Java program to find XOR of pair sums.
import java.io.*;
  
class GFG {
 
// method to find XOR of pair sums
static int xorPairSum(int ar[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum = sum ^ (ar[i] + ar[j]);
    return sum;
}
  
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = {1, 2, 3};
        int n = arr.length;
        System.out.print( xorPairSum(arr, n));
    }
}
 
// This code is contributed by chandan_jnu.


Python3
# Python program to find
# XOR of pair sums.
 
def xor_pair_sum(ar, n):
    total = 0
    for i in range(n):
        for j in range(n):
            total = total ^ (ar[i] + ar[j])
 
    return total
 
 
# Driver program to test the above function
if __name__ == "__main__":
    data = [1, 2, 3]
    print(xor_pair_sum(data, len(data)))
 
# This code is contributed
# by Kanav Malhotra


C#
// C# program to find
// XOR of pair sums.
using System;
 
class GFG
{
static int xorPairSum(int []ar,
                    int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            sum = sum ^ (ar[i] + ar[j]);
    return sum;
}
 
// Driver code
static public void Main(String []args)
{
    int []arr = { 1, 2, 3 };
    int n = arr.Length;
    Console.WriteLine(xorPairSum(arr, n));
}
}
 
// This code is contributed
// by Arnab Kundu


PHP


Javascript


C++
// CPP program to find XOR of pair
// sums.
#include 
 
using namespace std;
 
int xorPairSum(int ar[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
       sum = sum ^ ar[i];
    return 2*sum;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << xorPairSum(arr, n);
    return 0;
}


Java
// Java program to find
// XOR of pair sums.
class GFG
{
     
static int xorPairSum(int ar[],
                      int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    sum = sum ^ ar[i];
    return 2 * sum;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 2, 3 };
    int n = arr.length;
    System.out.println( xorPairSum(arr, n));
}
}
 
// This code is contributed
// by Arnab Kundu


Python3
# Python3 program to find
# XOR of pair sums.
 
def xor_pair_sum(ar, n):
    total = 0
    for i in range(n):
        total = total ^ ar[i]
 
    return 2 * total
 
 
# Driver program to test the above function
if __name__ == "__main__":
    data = [1, 2, 3]
    print(xor_pair_sum(data, len(data)))
 
# This code is contributed
# by Kanav Malhotra


C#
// C# program to find
// XOR of pair sums.
using System;
 
class GFG
{
     
static int xorPairSum(int []ar,
                    int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    sum = sum ^ ar[i];
    return 2 * sum;
}
 
// Driver code
static public void Main(String []args)
{
    int []arr = { 1, 2, 3 };
    int n = arr.Length;
    Console.WriteLine( xorPairSum(arr, n));
}
}
 
// This code is contributed
// by Arnab Kundu


PHP


Javascript


输出:
0

时间复杂度: O(N 2 )
一个有效的解决方案基于XOR属性。我们只需计算每个元素的XOR,然后将其乘以2。

C++

// CPP program to find XOR of pair
// sums.
#include 
 
using namespace std;
 
int xorPairSum(int ar[], int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
       sum = sum ^ ar[i];
    return 2*sum;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << xorPairSum(arr, n);
    return 0;
}

Java

// Java program to find
// XOR of pair sums.
class GFG
{
     
static int xorPairSum(int ar[],
                      int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    sum = sum ^ ar[i];
    return 2 * sum;
}
 
// Driver code
public static void main(String args[])
{
    int arr[] = { 1, 2, 3 };
    int n = arr.length;
    System.out.println( xorPairSum(arr, n));
}
}
 
// This code is contributed
// by Arnab Kundu

Python3

# Python3 program to find
# XOR of pair sums.
 
def xor_pair_sum(ar, n):
    total = 0
    for i in range(n):
        total = total ^ ar[i]
 
    return 2 * total
 
 
# Driver program to test the above function
if __name__ == "__main__":
    data = [1, 2, 3]
    print(xor_pair_sum(data, len(data)))
 
# This code is contributed
# by Kanav Malhotra

C#

// C# program to find
// XOR of pair sums.
using System;
 
class GFG
{
     
static int xorPairSum(int []ar,
                    int n)
{
    int sum = 0;
    for (int i = 0; i < n; i++)
    sum = sum ^ ar[i];
    return 2 * sum;
}
 
// Driver code
static public void Main(String []args)
{
    int []arr = { 1, 2, 3 };
    int n = arr.Length;
    Console.WriteLine( xorPairSum(arr, n));
}
}
 
// This code is contributed
// by Arnab Kundu

的PHP


Java脚本


输出:
0

时间复杂度: O(N)