Sum of array elements after every element x is XORed itself x times
Last Updated :
21 Jul, 2022
Given an array of integers, the task is to compute sum of all array elements after doing XOR of each element x with itself x times. For example, if element is 4 so we do XOR of this number with itself 4 time Like:= 4^4^4^4
Examples:
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
A Simple solution is to pick each array element one by one and do its XOR with itself according to the its value. Finally add XOR values to the result.
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
int XorSum( int arr[], int n)
{
int result = 0;
for ( int i = 0; i < n; i++) {
int k = 0;
for ( int j = 1; j <= arr[i]; j++)
k ^= arr[i];
result += k;
}
return result;
}
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
import java.io.*;
class GFG {
static int XorSum( int arr[], int n)
{
int result = 0 ;
for ( int i = 0 ; i < n; i++) {
int k = 0 ;
for ( int j = 1 ; j <= arr[i]; j++)
k ^= arr[i];
result += k;
}
return result;
}
public static void main(String args[])
{
int arr[] = { 1 , 2 , 6 , 3 , 4 , 5 };
int n = arr.length;
System.out.println(XorSum(arr, n));
}
}
|
Python
def XorSum(arr, n) :
result = 0
for i in range ( 0 , n) :
k = 0
for j in range ( 1 , arr[i] + 1 ) :
k = k ^ arr[i]
result = result + k
return result
arr = [ 1 , 2 , 6 , 3 , 4 , 5 ]
n = len (arr)
print (XorSum(arr, n))
|
C#
using System;
class GFG {
static int XorSum( int []arr, int n)
{
int result = 0;
for ( int i = 0; i < n; i++) {
int k = 0;
for ( int j = 1; j <= arr[i]; j++)
k ^= arr[i];
result += k;
}
return result;
}
public static void Main()
{
int []arr = { 1, 2, 6, 3, 4, 5 };
int n = arr.Length;
Console.WriteLine(XorSum(arr, n));
}
}
|
PHP
<?php
function XorSum( $arr , $n )
{
$result = 0;
for ( $i = 0; $i < $n ; $i ++)
{
$k = 0;
for ( $j = 1; $j <= $arr [ $i ]; $j ++)
$k ^= $arr [ $i ];
$result += $k ;
}
return $result ;
}
$arr = array (1, 2, 6, 3, 4, 5);
$n = count ( $arr );
echo XorSum( $arr , $n );
?>
|
Javascript
<script>
function XorSum(arr, n)
{
let result = 0;
for (let i = 0; i < n; i++) {
let k = 0;
for (let j = 1; j <= arr[i]; j++)
k ^= arr[i];
result += k;
}
return result;
}
let arr = [ 1, 2, 6, 3, 4, 5 ];
let n = arr.length;
document.write(XorSum(arr, n));
</script>
|
Time Complexity: O(n*m) (here m is the maximum element in array )
Auxiliary Space: O(1)
Efficient solution of this problem is based on the fact that if we do a XOR of any number with itself( even number of times) it produces 0 and if we do a XOR odd number of time it produces same number.
For Example
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
Below is the implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
int XorSum( int arr[], int n)
{
int result = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 != 0)
result += arr[i];
}
return result;
}
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
class GFG {
static int XorSum( int arr[], int n) {
int result = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 != 0 )
result += arr[i];
}
return result;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 6 , 3 , 4 , 5 };
int n = arr.length;
System.out.println(XorSum(arr, n));
}
}
|
Python3
def XorSum(arr,n):
result = 0
for i in range (n):
if (arr[i] % 2 ! = 0 ):
result + = arr[i]
return result
arr = [ 1 , 2 , 6 , 3 , 4 , 5 ]
n = len (arr)
print (XorSum(arr, n))
|
C#
using System;
class GFG {
static int XorSum( int []arr, int n)
{
int result = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 != 0)
result += arr[i];
}
return result;
}
public static void Main()
{
int []arr = {1, 2, 6, 3, 4, 5};
int n = arr.Length;
Console.WriteLine(XorSum(arr, n));
}
}
|
PHP
<?php
function XorSum( $arr , $n )
{
$result = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] % 2 != 0)
$result += $arr [ $i ];
}
return $result ;
}
$arr = array (1, 2, 6, 3, 4, 5);
$n = count ( $arr );
echo XorSum( $arr , $n );
?>
|
Javascript
<script>
function XorSum(arr, n)
{
let result = 0;
for (let i = 0; i < n; i++) {
if (arr[i] % 2 != 0)
result += arr[i];
}
return result;
}
let arr = [ 1, 2, 6, 3, 4, 5 ];
let n = arr.length;
document.write(XorSum(arr, n));
</script>
|
Time Complexity : O(n)
Auxiliary Space : O(1)
Share your thoughts in the comments
Please Login to comment...