Given an array A, consisting of N non-negative integers, find the sum of xor of all unordered triplets of the array. For unordered triplets, the triplet (A[i], A[j], A[k]) is considered same as triplet (A[j], A[i], A[k]) and all the other permutations.
Since the answer can be large calculate its mod with 10037.
Input : A = [3, 5, 2, 18, 7] Output : 132 Input : A = [140, 1, 66] Output : 207
Iterate over all the unordered triplets and add xor of each to the sum.
- An important point to observe is that xor is independent over all the bits. So we can do the required computation over each bit individually.
- Let’s consider the k’th bit of all the array elements. If the number of unoredered triplets whose k’th bit xor to 1 be C, we can simply add C * 2k to the answer. Let the number of elements whose k’th bit is 1 be X and whose k’th bit is 0 be Y. Then to find the unordered triplets whose k’th bits xor to 1 can be formed using two cases:
- Only one of the three elements have k’th bit 1.
- All three of them have k’th bit 1.
So we simply need to find the number of ways to select and this can be done using Permutation and Combination principles.
Number of ways to select 3 element having k’th bit 1 =
Number of ways to select 1 element with k’th bit 1 and rest with 0 =
- We will use nCr mod p to compute the combinotorial function values.
Below is the implementation of the above approach.
Time Complexity : O(32 * N)
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Find triplets in an array whose AND is maximum
- XOR of pairwise sum of every unordered pairs in an array
- Find the missing number in unordered Arithmetic Progression
- Count of unordered pairs (x, y) of Array which satisfy given equation
- Maximum value of XOR among all triplets of an array
- Count of triplets in a given Array having GCD K
- Count of triplets in an Array (i, j, k) such that i < j < k and a[k] < a[i] < a[j]
- Number of triplets in array having subarray xor equal
- Count number of triplets in an array having sum in the range [a, b]
- Count unordered pairs (i,j) such that product of a[i] and a[j] is power of two
- Alexander Bogomolny’s UnOrdered Permutation Algorithm
- Count of triplets (a, b, c) in the Array such that a divides b and b divides c
- Queries to count the number of unordered co-prime pairs from 1 to N
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Find minimum value to assign all array elements so that array product becomes greater
- Find Kth number from sorted array formed by multiplying any two numbers in the array
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Find the Initial Array from given array after range sum queries
- Find numbers which are multiples of first array and factors of second array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohit kumar 29