数组中每对可能的和的异或

给定一个大小为n的数组A。任务是生成一个大小为n^2的新序列B,其中每个数组A对的元素和都是相同的,并找到所形成的所有数组对和的异或值。 注:此处(A[i]、A[i]、(A[i]、A[j]、(A[j]、A[i])都被视为不同的对。

null

例如:

Input: arr = {1, 5, 6}Output: 4B[3*3] = { 1+1, 1+5, 1+6, 5+1, 5+5, 5+6, 6+1, 6+5, 6+6}B[9] = { 2, 6, 7, 6, 10, 11, 7, 11, 12}So, 2 ^ 6 ^ 7 ^ 6 ^ 10 ^ 11 ^ 7 ^ 6 ^ 11 ^ 12 = 4Input :1, 2Output :6

A. 缺乏经验的 方法是运行两个循环。考虑每一对,取它们的和,并计算所有对的和的XOR值。 一 有效率的 该方法基于相同值的xor为0这一事实。 所有像(a[i],a[j])和(a[j],a[i])这样的对都有相同的和。所以,它们的xor值将为0。只有像(a[i],a[i])这样的配对才会给出不同的结果。所以,取给定数组中所有元素的异或,乘以2。

C++

// C++ program to find XOR of
// sum of every possible pairs
// in an array
#include <bits/stdc++.h>
using namespace std;
// Function to find XOR of sum
// of all pairs
int findXor( int arr[], int n)
{
// Calculate xor of all the elements
int xoR = 0;
for ( int i = 0; i < n; i++) {
xoR = xoR ^ arr[i];
}
// Return twice of xor value
return xoR * 2;
}
// Drivers code
int main()
{
int arr[3] = { 1, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findXor(arr, n);
return 0;
}


JAVA

// Java program to find XOR of
// sum of every possible pairs
// in an array
import java.io.*;
class GFG {
// Function to find XOR of sum
// of all pairs
static int findXor( int arr[], int n)
{
// Calculate xor of all the
// elements
int xoR = 0 ;
for ( int i = 0 ; i < n; i++) {
xoR = xoR ^ arr[i];
}
// Return twice of xor value
return xoR * 2 ;
}
// Drivers code
public static void main (String[] args)
{
int arr[] = { 1 , 5 , 6 };
int n = arr.length;
System.out.println( findXor(arr, n));
}
}
// This code is contributed by anuj_67.


Python3

# Python3 program to find
# XOR of sum of every
# possible pairs in an array
# Function to find XOR
# of sum of all pairs
def findXor(arr,n):
# Calculate xor of
# all the elements
xoR = 0 ;
for i in range ( 0 , n ) :
xoR = xoR ^ arr[i]
# Return twice of
# xor value
return xoR * 2
# Driver code
arr = [ 1 , 5 , 6 ]
n = len (arr)
print (findXor(arr, n))
# This code is contributed
# by ihritik


C#

// C# program to find XOR of
// sum of every possible pairs
// in an array
using System;
class GFG {
// Function to find XOR of sum
// of all pairs
static int findXor( int []arr, int n)
{
// Calculate xor of all the
// elements
int xoR = 0;
for ( int i = 0; i < n; i++) {
xoR = xoR ^ arr[i];
}
// Return twice of xor value
return xoR * 2;
}
// Drivers code
public static void Main ()
{
int []arr = { 1, 5, 6 };
int n = arr.Length;
Console.WriteLine( findXor(arr, n));
}
}
// This code is contributed by anuj_67.


PHP

<?php
// PHP program to find XOR
// of sum of every possible
// pairs in an array
// Function to find XOR
// of sum of all pairs
function findXor( $arr , $n )
{
// Calculate xor of
// all the elements
$xoR = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$xoR = $xoR ^ $arr [ $i ];
}
// Return twice
// of xor value
return $xoR * 2;
}
// Driver code
$arr = array (1, 5, 6);
$n = count ( $arr );
echo findXor( $arr , $n );
// This code is contributed
// by anuj_67.
?>


Javascript

<script>
// Javascript program to find XOR of
// sum of every possible pairs
// in an array
// Function to find XOR of sum
// of all pairs
function findXor(arr, n)
{
// Calculate xor of all the elements
let xoR = 0;
for (let i = 0; i < n; i++) {
xoR = xoR ^ arr[i];
}
// Return twice of xor value
return xoR * 2;
}
// Drivers code
let arr = [ 1, 5, 6 ];
let n = arr.length;
document.write(findXor(arr, n));
</script>


输出:

4

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享