Posted on by Kalkicode
Code Array

# Sum of xor of all pairs in an array

Here given code implementation process.

``````// C Program
// Sum of xor of all pairs in an array
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
printf("\n");
}
// Calculate the sum of all xor pairs in given array
void sumXorPair(int arr[], int size)
{
// Loop controlling variables
int i = 0;
int j = 0;
// Set the initial sum of zero
int sum = 0;
//Execute loop through by size
for (i = 0; i < size; ++i)
{
// Perform xor operation of pairs [i] and [j]
for (j = i + 1; j < size; ++j)
{
// Add the result of xor pair
sum += arr[i] ^ arr[j];
}
}
// Display array element
printf(" Array element \n");
printArray(arr, size);
// Display calculated result
printf(" Sum : %d\n", sum);
}
int main(int argc, char const *argv[])
{
// Define array of integer elements
int arr[] = {
1 , 8 , -2 , 3 , 2 , 4 , 5
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
/*
(1 ^ 8)  = 9
(1 ^ -2) = -1
(1 ^ 3)  = 2
(1 ^ 2)  = 3
(1 ^ 4)  = 5
(1 ^ 5)  = 4
(8 ^ -2) = -10
(8 ^ 3)  = 11
(8 ^ 2)  = 10
(8 ^ 4)  = 12
(8 ^ 5)  = 13
(-2 ^ 3) = -3
(-2 ^ 2) = -4
(-2 ^ 4) = -6
(-2 ^ 5) = -5
(3 ^ 2)  = 1
(3 ^ 4)  = 7
(3 ^ 5)  = 6
(2 ^ 4)  = 6
(2 ^ 5)  = 7
(4 ^ 5)  = 1
---------------
Sum   = 68
*/
sumXorPair(arr, size);
return 0;
}``````

#### Output

`````` Array element
1  8  -2  3  2  4  5
Sum : 68``````
``````/*
Java Program
Sum of xor of all pairs in an array
*/
public class SumOperation
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i]);
}
System.out.print("\n");
}
// Calculate the sum of all xor pairs in given array
public void sumXorPair(int[] arr, int size)
{
// Loop controlling variables
int i = 0;
int j = 0;
// Set the initial sum of zero
int sum = 0;
//Execute loop through by size
for (i = 0; i < size; ++i)
{
// Perform xor operation of pairs [i] and [j]
for (j = i + 1; j < size; ++j)
{
// Add the result of xor pair
sum += arr[i] ^ arr[j];
}
}
// Display array element
System.out.print(" Array element \n");
printArray(arr, size);
// Display calculated result
System.out.print(" Sum : " + sum + "\n");
}
public static void main(String[] args)
{
// Define array of integer elements
int[] arr = {
1 , 8 , -2 , 3 , 2 , 4 , 5
};
// Get the size
int size = arr.length;
/*
(1 ^ -2) = -1
(1 ^ 3)  = 2
(1 ^ 2)  = 3
(1 ^ 4)  = 5
(1 ^ 5)  = 4
(8 ^ -2) = -10
(8 ^ 3)  = 11
(8 ^ 2)  = 10
(8 ^ 4)  = 12
(8 ^ 5)  = 13
(-2 ^ 3) = -3
(-2 ^ 2) = -4
(-2 ^ 4) = -6
(-2 ^ 5) = -5
(3 ^ 2)  = 1
(3 ^ 4)  = 7
(3 ^ 5)  = 6
(2 ^ 4)  = 6
(2 ^ 5)  = 7
(4 ^ 5)  = 1
---------------
Sum   = 68

*/
}
}``````

#### Output

`````` Array element
1  8  -2  3  2  4  5
Sum : 68``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program
Sum of xor of all pairs in an array
*/
class SumOperation
{
public:
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
cout << "\n";
}
// Calculate the sum of all xor pairs in given array
void sumXorPair(int arr[], int size)
{
// Loop controlling variables
int i = 0;
int j = 0;
// Set the initial sum of zero
int sum = 0;
//Execute loop through by size
for (i = 0; i < size; ++i)
{
// Perform xor operation of pairs [i] and [j]
for (j = i + 1; j < size; ++j)
{
// Add the result of xor pair
sum += arr[i] ^ arr[j];
}
}
// Display array element
cout << " Array element \n";
this->printArray(arr, size);
// Display calculated result
cout << " Sum : " << sum << "\n";
}
};
int main()
{
// Define array of integer elements
int arr[] = {
1 , 8 , -2 , 3 , 2 , 4 , 5
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
/*
(1 ^ 3)  = 2
(1 ^ 2)  = 3
(1 ^ 4)  = 5
(1 ^ 5)  = 4
(8 ^ -2) = -10
(8 ^ 3)  = 11
(8 ^ 2)  = 10
(8 ^ 4)  = 12
(8 ^ 5)  = 13
(-2 ^ 3) = -3
(-2 ^ 2) = -4
(-2 ^ 4) = -6
(-2 ^ 5) = -5
(3 ^ 2)  = 1
(3 ^ 4)  = 7
(3 ^ 5)  = 6
(2 ^ 4)  = 6
(2 ^ 5)  = 7
(4 ^ 5)  = 1
---------------
Sum   = 68

*/
return 0;
}``````

#### Output

`````` Array element
1  8  -2  3  2  4  5
Sum : 68``````
``````// Include namespace system
using System;
/*
C# Program
Sum of xor of all pairs in an array
*/
public class SumOperation
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
Console.Write("\n");
}
// Calculate the sum of all xor pairs in given array
public void sumXorPair(int[] arr, int size)
{
// Loop controlling variables
int i = 0;
int j = 0;
// Set the initial sum of zero
int sum = 0;
//Execute loop through by size
for (i = 0; i < size; ++i)
{
// Perform xor operation of pairs [i] and [j]
for (j = i + 1; j < size; ++j)
{
// Add the result of xor pair
sum += arr[i] ^ arr[j];
}
}
// Display array element
Console.Write(" Array element \n");
printArray(arr, size);
// Display calculated result
Console.Write(" Sum : " + sum + "\n");
}
public static void Main(String[] args)
{
// Define array of integer elements
int[] arr = {
1 , 8 , -2 , 3 , 2 , 4 , 5
};
// Get the size
int size = arr.Length;
/*
(1 ^ 3)  = 2
(1 ^ 2)  = 3
(1 ^ 4)  = 5
(1 ^ 5)  = 4
(8 ^ -2) = -10
(8 ^ 3)  = 11
(8 ^ 2)  = 10
(8 ^ 4)  = 12
(8 ^ 5)  = 13
(-2 ^ 3) = -3
(-2 ^ 2) = -4
(-2 ^ 4) = -6
(-2 ^ 5) = -5
(3 ^ 2)  = 1
(3 ^ 4)  = 7
(3 ^ 5)  = 6
(2 ^ 4)  = 6
(2 ^ 5)  = 7
(4 ^ 5)  = 1
---------------
Sum   = 68
*/
}
}``````

#### Output

`````` Array element
1  8  -2  3  2  4  5
Sum : 68``````
``````<?php
/*
Php Program
Sum of xor of all pairs in an array
*/
class SumOperation
{
//Display elements of given array
public	function printArray( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
echo "\n";
}
// Calculate the sum of all xor pairs in given array
public	function sumXorPair( & \$arr, \$size)
{
// Loop controlling variables
\$i = 0;
\$j = 0;
// Set the initial sum of zero
\$sum = 0;
//Execute loop through by size
for (\$i = 0; \$i < \$size; ++\$i)
{
// Perform xor operation of pairs [i] and [j]
for (\$j = \$i + 1; \$j < \$size; ++\$j)
{
// Add the result of xor pair
\$sum += \$arr[\$i] ^ \$arr[\$j];
}
}
// Display array element
echo " Array element \n";
\$this->printArray(\$arr, \$size);
// Display calculated result
echo " Sum : ". \$sum ."\n";
}
}

function main()
{
// Define array of integer elements
\$arr = array(1, 8, -2, 3, 2, 4, 5);
// Get the size
\$size = count(\$arr);
/*
(1 ^ 3)  = 2
(1 ^ 2)  = 3
(1 ^ 4)  = 5
(1 ^ 5)  = 4
(8 ^ -2) = -10
(8 ^ 3)  = 11
(8 ^ 2)  = 10
(8 ^ 4)  = 12
(8 ^ 5)  = 13
(-2 ^ 3) = -3
(-2 ^ 2) = -4
(-2 ^ 4) = -6
(-2 ^ 5) = -5
(3 ^ 2)  = 1
(3 ^ 4)  = 7
(3 ^ 5)  = 6
(2 ^ 4)  = 6
(2 ^ 5)  = 7
(4 ^ 5)  = 1
---------------
Sum   = 68
*/
}
main();``````

#### Output

`````` Array element
1  8  -2  3  2  4  5
Sum : 68``````
``````/*
Node Js Program
Sum of xor of all pairs in an array
*/
class SumOperation
{
//Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
process.stdout.write("\n");
}
// Calculate the sum of all xor pairs in given array
sumXorPair(arr, size)
{
// Loop controlling variables
var i = 0;
var j = 0;
// Set the initial sum of zero
var sum = 0;
//Execute loop through by size
for (i = 0; i < size; ++i)
{
// Perform xor operation of pairs [i] and [j]
for (j = i + 1; j < size; ++j)
{
// Add the result of xor pair
sum += arr[i] ^ arr[j];
}
}
// Display array element
process.stdout.write(" Array element \n");
this.printArray(arr, size);
// Display calculated result
process.stdout.write(" Sum : " + sum + "\n");
}
}

function main()
{
// Define array of integer elements
var arr = [1, 8, -2, 3, 2, 4, 5];
// Get the size
var size = arr.length;
/*
(1 ^ 3)  = 2
(1 ^ 2)  = 3
(1 ^ 4)  = 5
(1 ^ 5)  = 4
(8 ^ -2) = -10
(8 ^ 3)  = 11
(8 ^ 2)  = 10
(8 ^ 4)  = 12
(8 ^ 5)  = 13
(-2 ^ 3) = -3
(-2 ^ 2) = -4
(-2 ^ 4) = -6
(-2 ^ 5) = -5
(3 ^ 2)  = 1
(3 ^ 4)  = 7
(3 ^ 5)  = 6
(2 ^ 4)  = 6
(2 ^ 5)  = 7
(4 ^ 5)  = 1
---------------
Sum   = 68
*/
}
main();``````

#### Output

`````` Array element
1  8  -2  3  2  4  5
Sum : 68``````
``````#  Python 3 Program
#  Sum of xor of all pairs in an array

class SumOperation :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

print(end = "\n")

#  Calculate the sum of all xor pairs in given array
def sumXorPair(self, arr, size) :
#  Loop controlling variables
i = 0
j = 0
#  Set the initial sum of zero
sum = 0
# Execute loop through by size
while (i < size) :
#  Perform xor operation of pairs [i] and [j]
j = i + 1
while (j < size) :
#  Add the result of xor pair
sum += arr[i] ^ arr[j]
j += 1

i += 1

#  Display array element
print(" Array element ")
self.printArray(arr, size)
#  Display calculated result
print(" Sum : ", sum )

def main() :
#  Define array of integer elements
arr = [1, 8, -2, 3, 2, 4, 5]
#  Get the size
size = len(arr)

#     (1 ^ 3)  = 2
#     (1 ^ 2)  = 3
#     (1 ^ 4)  = 5
#     (1 ^ 5)  = 4
#     (8 ^ -2) = -10
#     (8 ^ 3)  = 11
#     (8 ^ 2)  = 10
#     (8 ^ 4)  = 12
#     (8 ^ 5)  = 13
#     (-2 ^ 3) = -3
#     (-2 ^ 2) = -4
#     (-2 ^ 4) = -6
#     (-2 ^ 5) = -5
#     (3 ^ 2)  = 1
#     (3 ^ 4)  = 7
#     (3 ^ 5)  = 6
#     (2 ^ 4)  = 6
#     (2 ^ 5)  = 7
#     (4 ^ 5)  = 1
#   ---------------
#        Sum   = 68

if __name__ == "__main__": main()``````

#### Output

`````` Array element
1   8   -2   3   2   4   5
Sum :  68``````
``````#   Ruby Program
#   Sum of xor of all pairs in an array

class SumOperation
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

print("\n")
end

#  Calculate the sum of all xor pairs in given array
def sumXorPair(arr, size)
#  Loop controlling variables
i = 0
j = 0
#  Set the initial sum of zero
sum = 0
# Execute loop through by size
while (i < size)
#  Perform xor operation of pairs [i] and [j]
j = i + 1
while (j < size)
#  Add the result of xor pair
sum += arr[i] ^ arr[j]
j += 1
end

i += 1
end

#  Display array element
print(" Array element \n")
self.printArray(arr, size)
#  Display calculated result
print(" Sum : ", sum ,"\n")
end

end

def main()
#  Define array of integer elements
arr = [1, 8, -2, 3, 2, 4, 5]
#  Get the size
size = arr.length
#
#     (1 ^ 3)  = 2
#     (1 ^ 2)  = 3
#     (1 ^ 4)  = 5
#     (1 ^ 5)  = 4
#     (8 ^ -2) = -10
#     (8 ^ 3)  = 11
#     (8 ^ 2)  = 10
#     (8 ^ 4)  = 12
#     (8 ^ 5)  = 13
#     (-2 ^ 3) = -3
#     (-2 ^ 2) = -4
#     (-2 ^ 4) = -6
#     (-2 ^ 5) = -5
#     (3 ^ 2)  = 1
#     (3 ^ 4)  = 7
#     (3 ^ 5)  = 6
#     (2 ^ 4)  = 6
#     (2 ^ 5)  = 7
#     (4 ^ 5)  = 1
#   ---------------
#        Sum   = 68

end

main()``````

#### Output

`````` Array element
1  8  -2  3  2  4  5
Sum : 68
``````
``````/*
Scala Program
Sum of xor of all pairs in an array
*/
class SumOperation
{
//Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
print("\n");
}
// Calculate the sum of all xor pairs in given array
def sumXorPair(arr: Array[Int], size: Int): Unit = {
// Loop controlling variables
var i: Int = 0;
var j: Int = 0;
// Set the initial sum of zero
var sum: Int = 0;
//Execute loop through by size
while (i < size)
{
// Perform xor operation of pairs [i] and [j]
j = i + 1;
while (j < size)
{
// Add the result of xor pair
sum += arr(i) ^ arr(j);
j += 1;
}
i += 1;
}
// Display array element
print(" Array element \n");
this.printArray(arr, size);
// Display calculated result
print(" Sum : " + sum + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: SumOperation = new SumOperation();
// Define array of integer elements
var arr: Array[Int] = Array(1, 8, -2, 3, 2, 4, 5);
// Get the size
var size: Int = arr.length;
/*
(1 ^ 3)  = 2
(1 ^ 2)  = 3
(1 ^ 4)  = 5
(1 ^ 5)  = 4
(8 ^ -2) = -10
(8 ^ 3)  = 11
(8 ^ 2)  = 10
(8 ^ 4)  = 12
(8 ^ 5)  = 13
(-2 ^ 3) = -3
(-2 ^ 2) = -4
(-2 ^ 4) = -6
(-2 ^ 5) = -5
(3 ^ 2)  = 1
(3 ^ 4)  = 7
(3 ^ 5)  = 6
(2 ^ 4)  = 6
(2 ^ 5)  = 7
(4 ^ 5)  = 1
---------------
Sum   = 68
*/
}
}``````

#### Output

`````` Array element
1  8  -2  3  2  4  5
Sum : 68``````
``````/*
Swift 4 Program
Sum of xor of all pairs in an array
*/
class SumOperation
{
//Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Calculate the sum of all xor pairs in given array
func sumXorPair(_ arr: [Int], _ size: Int)
{
// Loop controlling variables
var i: Int = 0;
var j: Int = 0;
// Set the initial sum of zero
var sum: Int = 0;
//Execute loop through by size
while (i < size)
{
// Perform xor operation of pairs [i] and [j]
j = i + 1;
while (j < size)
{
// Add the result of xor pair
sum += arr[i] ^ arr[j];
j += 1;
}
i += 1;
}
// Display array element
print(" Array element ");
self.printArray(arr, size);
// Display calculated result
print(" Sum : ", sum );
}
}
func main()
{
// Define array of integer elements
let arr: [Int] = [1, 8, -2, 3, 2, 4, 5];
// Get the size
let size: Int = arr.count;
/*
(1 ^ 3)  = 2
(1 ^ 2)  = 3
(1 ^ 4)  = 5
(1 ^ 5)  = 4
(8 ^ -2) = -10
(8 ^ 3)  = 11
(8 ^ 2)  = 10
(8 ^ 4)  = 12
(8 ^ 5)  = 13
(-2 ^ 3) = -3
(-2 ^ 2) = -4
(-2 ^ 4) = -6
(-2 ^ 5) = -5
(3 ^ 2)  = 1
(3 ^ 4)  = 7
(3 ^ 5)  = 6
(2 ^ 4)  = 6
(2 ^ 5)  = 7
(4 ^ 5)  = 1
---------------
Sum   = 68
*/
}
main();``````

#### Output

`````` Array element
1   8   -2   3   2   4   5
Sum :  68``````
``````/*
Kotlin Program
Sum of xor of all pairs in an array
*/
class SumOperation
{
//Display elements of given array
fun printArray(arr: Array < Int > , size: Int): Unit
{
var i: Int = 0;
while (i < size)
{
print("  " + arr[i]);
i += 1;
}
print("\n");
}
// Calculate the sum of all xor pairs in given array
fun sumXorPair(arr: Array < Int > , size: Int): Unit
{
// Loop controlling variables
var i: Int = 0;
var j: Int ;
// Set the initial sum of zero
var sum: Int = 0;
//Execute loop through by size
while (i < size)
{
// Perform xor operation of pairs [i] and [j]
j = i + 1;
while (j < size)
{
// Add the result of xor pair
sum += arr[i] xor arr[j];
j += 1;
}
i += 1;
}
// Display array element
print(" Array element \n");
this.printArray(arr, size);
// Display calculated result
print(" Sum : " + sum + "\n");
}
}
fun main(args: Array < String > ): Unit
{
// Define array of integer elements
var arr: Array < Int > = arrayOf(1, 8, -2, 3, 2, 4, 5);
// Get the size
var size: Int = arr.count();
/*
(1 ^ 3)  = 2
(1 ^ 2)  = 3
(1 ^ 4)  = 5
(1 ^ 5)  = 4
(8 ^ -2) = -10
(8 ^ 3)  = 11
(8 ^ 2)  = 10
(8 ^ 4)  = 12
(8 ^ 5)  = 13
(-2 ^ 3) = -3
(-2 ^ 2) = -4
(-2 ^ 4) = -6
(-2 ^ 5) = -5
(3 ^ 2)  = 1
(3 ^ 4)  = 7
(3 ^ 5)  = 6
(2 ^ 4)  = 6
(2 ^ 5)  = 7
(4 ^ 5)  = 1
---------------
Sum   = 68
*/
}``````

#### Output

`````` Array element
1  8  -2  3  2  4  5
Sum : 68``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post