sum of xor values of all pairs

pranay

We have an array A (say [1,2,3]) . We need to find the XOR(^)SUM of all pairs of integers in the array. Though this can easily be done in O(n^2) but how can i improve the complexity of the solution ? E.g for the above array , A, the answer would be (1^2)+(1^3)+(2^3) = 6 Thanks.

interjay

You can separate the calculation to do one bit at a time.

For example, look at the rightmost bit of all the numbers in the array. Suppose that a numbers have a rightmost 0-bit, and b numbers have a 1-bit. Then out of the pairs, a*b of them will have 1 in the rightmost bit of the XOR. This is because there are a*b ways to choose one number that has a 0-bit and one that has a 1-bit. These bits will therefore contribute a*b towards the total of all the XORs.

In general, when looking at the nth bit (where the rightmost bit is the 0th), count how many numbers have 0 (call this an) and how many have 1 (call this bn). The contribution towards the final sum will be an*bn*2n. You need to do this for each bit and sum all these contributions together.

This can be done in O(kn) time, where k is the number of bits in the given values.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

SQL sum all values in column

From Dev

Sum of all values in a class tag

From Dev

Correlation coefficients and p values for all pairs of rows of a matrix

From Dev

Turning values into percent of the sum of all the values

From Dev

Subtract all pairs of values from two arrays

From Dev

SQL: Computing sum of all values *and* a sum of only values matching condition

From Dev

Sum of all possible pairs of elements in an array

From Dev

Python, in a list of (a,b) pairs find the a value with the largest sum(b values)

From Dev

Or of all pairs formed by taking xor of all the numbers in a list.

From Dev

Multiply unique pairs of values in a vector and sum the result

From Dev

python: getting all pairs of values from list

From Dev

R - get all pairs of values with same ID

From Dev

How to sum all values in a hashmap?

From Dev

Given an XOR and SUM of two numbers, how to find the number of pairs that satisfy them?

From Dev

Efficient way to sum all possible pairs

From Dev

How generate all pairs of values, from the result of a groupby, in a pandas dataframe

From Dev

All pairs of pairs python

From Dev

Sum of all values except the first

From Dev

Sum of all values of a column in coredata

From Dev

Turning values into percent of the sum of all the values

From Dev

Sum all values in CSV

From Dev

How to compare pairs of values in column of SQL, for all possible pairs?

From Dev

Multiply unique pairs of values in a vector and sum the result

From Dev

SUM of all Values in a field SQL

From Dev

Sum of bit differences among all pairs

From Dev

sum the values of all occurrences parameters

From Dev

Generating all pairs of numbers which XOR is N

From Dev

Random pairs of all values in a pandas dataframe

From Dev

Count all pairs with given XOR

Related Related

  1. 1

    SQL sum all values in column

  2. 2

    Sum of all values in a class tag

  3. 3

    Correlation coefficients and p values for all pairs of rows of a matrix

  4. 4

    Turning values into percent of the sum of all the values

  5. 5

    Subtract all pairs of values from two arrays

  6. 6

    SQL: Computing sum of all values *and* a sum of only values matching condition

  7. 7

    Sum of all possible pairs of elements in an array

  8. 8

    Python, in a list of (a,b) pairs find the a value with the largest sum(b values)

  9. 9

    Or of all pairs formed by taking xor of all the numbers in a list.

  10. 10

    Multiply unique pairs of values in a vector and sum the result

  11. 11

    python: getting all pairs of values from list

  12. 12

    R - get all pairs of values with same ID

  13. 13

    How to sum all values in a hashmap?

  14. 14

    Given an XOR and SUM of two numbers, how to find the number of pairs that satisfy them?

  15. 15

    Efficient way to sum all possible pairs

  16. 16

    How generate all pairs of values, from the result of a groupby, in a pandas dataframe

  17. 17

    All pairs of pairs python

  18. 18

    Sum of all values except the first

  19. 19

    Sum of all values of a column in coredata

  20. 20

    Turning values into percent of the sum of all the values

  21. 21

    Sum all values in CSV

  22. 22

    How to compare pairs of values in column of SQL, for all possible pairs?

  23. 23

    Multiply unique pairs of values in a vector and sum the result

  24. 24

    SUM of all Values in a field SQL

  25. 25

    Sum of bit differences among all pairs

  26. 26

    sum the values of all occurrences parameters

  27. 27

    Generating all pairs of numbers which XOR is N

  28. 28

    Random pairs of all values in a pandas dataframe

  29. 29

    Count all pairs with given XOR

HotTag

Archive