每个元素 x 后的数组元素之和本身 XORed x 次
给定一个整数数组,任务是在每个元素 x 与自身 x 次 XOR 之后计算所有数组元素的总和。例如,如果元素是 4,那么我们将这个数字与自身进行 XOR 4 次 Like:= 4^4^4^4
例子:
Input : arr[] = { 1, 2, 3, 5 }
Output : 9
explanation: 1 + 2^2 + 3^3^3 + 5^5^5^5^5 : 9
Input : arr[] ={ 5, 6, 7, 9 }
Output : 21
一个简单的解决方案是一个一个地选择每个数组元素,并根据其值与自身进行异或。最后将 XOR 值添加到结果中。
下面是上述想法的实现。
C++
// C++ program to compute sum of all element after
// doing Xor with itself ( element_time)
#include
using namespace std;
// function return sum of all XOR element of array
int XorSum(int arr[], int n)
{
// store result
int result = 0;
// Traverse array element and apply XOR
// operation on it
for (int i = 0; i < n; i++) {
// XOR of current element with itself
// according to value.
int k = 0;
for (int j = 1; j <= arr[i]; j++)
k ^= arr[i];
result += k;
}
return result;
}
// Driver program
int main()
{
int arr[] = { 1, 2, 6, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << XorSum(arr, n) << endl;
return 0;
}
Java
// Java program to compute sum of all
// element after doing Xor with itself
// ( element_time)
import java.io.*;
class GFG {
// function return sum of all XOR
// element of array
static int XorSum(int arr[], int n)
{
// store result
int result = 0;
// Traverse array element and apply
// XOR operation on it
for (int i = 0; i < n; i++) {
// XOR of current element with
// itself according to value.
int k = 0;
for (int j = 1; j <= arr[i]; j++)
k ^= arr[i];
result += k;
}
return result;
}
// Driver program
public static void main(String args[])
{
int arr[] = { 1, 2, 6, 3, 4, 5 };
int n = arr.length;
System.out.println(XorSum(arr, n));
}
}
/*This code is contributed by Nikita Tiwari.*/
Python
# Python 3 program to compute sum of
# all element after doing Xor with
# itself ( element_time)
# function return sum of all XOR
# element of array
def XorSum(arr, n) :
# store result
result = 0
# Traverse array element and
# apply XOR operation on it
for i in range(0, n) :
# XOR of current element
# with itself according to
# value.
k = 0
for j in range(1, arr[i]+1) :
k = k ^ arr[i]
result = result + k
return result
# Driver program
arr = [ 1, 2, 6, 3, 4, 5 ]
n = len(arr)
print(XorSum(arr, n))
# This code is contributed by Nikita Tiwari.
C#
// C# program to compute sum of all
// element after doing Xor with itself
// ( element_time)
using System;
class GFG {
// function return sum of all XOR
// element of array
static int XorSum(int []arr, int n)
{
// store result
int result = 0;
// Traverse array element and apply
// XOR operation on it
for (int i = 0; i < n; i++) {
// XOR of current element with
// itself according to value.
int k = 0;
for (int j = 1; j <= arr[i]; j++)
k ^= arr[i];
result += k;
}
return result;
}
// Driver program
public static void Main()
{
int []arr = { 1, 2, 6, 3, 4, 5 };
int n = arr.Length;
Console.WriteLine(XorSum(arr, n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
C++
// C++ program to compute sum of all element after
// doing XOR with itself ( element_time)
#include
using namespace std;
// function return sum of all XOR element of array
int XorSum(int arr[], int n)
{
int result = 0;
for (int i = 0; i < n; i++) {
// if number is odd then add it to the
// result else not
if (arr[i] % 2 != 0)
result += arr[i];
}
return result;
}
// Driver program
int main()
{
int arr[] = { 1, 2, 6, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << XorSum(arr, n) << endl;
return 0;
}
Java
// Java program to compute sum of
// all element after doing XOR
// with itself ( element_time)
class GFG {
// function return sum of all
// XOR element of array
static int XorSum(int arr[], int n) {
int result = 0;
for (int i = 0; i < n; i++) {
// if number is odd then add it to the
// result else not
if (arr[i] % 2 != 0)
result += arr[i];
}
return result;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {1, 2, 6, 3, 4, 5};
int n = arr.length;
System.out.println(XorSum(arr, n));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to compute
# sum of all element after
# doing XOR with itself
# ( element_time)
# function return sum of
# all XOR element of array
def XorSum(arr,n):
result = 0
for i in range(n):
# if number is odd then add it to the
# result else not
if (arr[i] % 2 != 0):
result += arr[i]
return result
# Driver program
arr = [ 1, 2, 6, 3, 4, 5 ]
n = len(arr)
print(XorSum(arr, n))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to compute sum of
// all element after doing XOR
// with itself ( element_time)
using System;
class GFG {
// function return sum of all
// XOR element of array
static int XorSum(int []arr, int n)
{
int result = 0;
for (int i = 0; i < n; i++) {
// if number is odd then add it to the
// result else not
if (arr[i] % 2 != 0)
result += arr[i];
}
return result;
}
// Driver code
public static void Main()
{
int []arr = {1, 2, 6, 3, 4, 5};
int n = arr.Length;
Console.WriteLine(XorSum(arr, n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
9
时间复杂度:O(n*m)(这里 m 是数组中的最大元素)
辅助空间:O(1)
这个问题的有效解决方案是基于这样一个事实:如果我们对任何数字进行 XOR(偶数次)它会产生 0,如果我们进行 XOR 奇数次它会产生相同的数字。
例如
let number be : 3 do XOR with itself 3 time
3^3^3 = 3
let number be : 4 do XOR with itself 4 time
4^4^4^4 = 0
so if number is odd it's mean output is number
itself. Else zero
以下是上述想法的实现:
C++
// C++ program to compute sum of all element after
// doing XOR with itself ( element_time)
#include
using namespace std;
// function return sum of all XOR element of array
int XorSum(int arr[], int n)
{
int result = 0;
for (int i = 0; i < n; i++) {
// if number is odd then add it to the
// result else not
if (arr[i] % 2 != 0)
result += arr[i];
}
return result;
}
// Driver program
int main()
{
int arr[] = { 1, 2, 6, 3, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << XorSum(arr, n) << endl;
return 0;
}
Java
// Java program to compute sum of
// all element after doing XOR
// with itself ( element_time)
class GFG {
// function return sum of all
// XOR element of array
static int XorSum(int arr[], int n) {
int result = 0;
for (int i = 0; i < n; i++) {
// if number is odd then add it to the
// result else not
if (arr[i] % 2 != 0)
result += arr[i];
}
return result;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {1, 2, 6, 3, 4, 5};
int n = arr.length;
System.out.println(XorSum(arr, n));
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python program to compute
# sum of all element after
# doing XOR with itself
# ( element_time)
# function return sum of
# all XOR element of array
def XorSum(arr,n):
result = 0
for i in range(n):
# if number is odd then add it to the
# result else not
if (arr[i] % 2 != 0):
result += arr[i]
return result
# Driver program
arr = [ 1, 2, 6, 3, 4, 5 ]
n = len(arr)
print(XorSum(arr, n))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to compute sum of
// all element after doing XOR
// with itself ( element_time)
using System;
class GFG {
// function return sum of all
// XOR element of array
static int XorSum(int []arr, int n)
{
int result = 0;
for (int i = 0; i < n; i++) {
// if number is odd then add it to the
// result else not
if (arr[i] % 2 != 0)
result += arr[i];
}
return result;
}
// Driver code
public static void Main()
{
int []arr = {1, 2, 6, 3, 4, 5};
int n = arr.Length;
Console.WriteLine(XorSum(arr, n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出:
9