Find sum of XNOR of all unordered pairs from given Array
Last Updated :
15 Feb, 2023
Given an array arr[] of size N, the task is to find the sum of all XNOR values of all possible unordered pairs from the given array.
Examples:
Input: N = 5, arr[] = {2, 2, 2, 1, 1}
Output:10
Explanation: Here,
2 XNOR 2 = 3, 2 XNOR 2 = 3, 2 XNOR 2 = 3, 1 XNOR 1 = 1, 2 XNOR 1 = 0, 2 XNOR 1 = 0, 2 XNOR 1 = 0, 2 XNOR 1 = 0, 2 XNOR 1 = 0, 2 XNOR 1 = 0, Therefore sum is 3+3+3+1=10.
Input: N = 3, arr[] = {1, 2, 3}
Output: 3
Explanation: Here, 1 XNOR 2 = 0, 1 XNOR 3 = 1, 2 XNOR 3 = 2. Therefore sum is 0+1+2 = 3
Approach: The only possible case for a bit to be set after the XNOR operation is that both the bits must be the same. Follow the below steps to solve the problem:
- Maintain a bit array of size 30. 0th position from left means 2^29 is included in binary representation &, 29th position from left means 2^0 is included in binary representation.
- For each element, if the ith bit is set then increment the ith position of the bit array.
- After encountering the MSB containing ‘1’ calculate the ‘0’ and ‘1’ bits.
- Until a ‘1’ bit is encountered, all the ‘0’ bits will be wasted bits. Store them separately.
- Finally, let’s say for an ith position there is y number of ones. Therefore, there are y*(y-1)/2 pairs for an ith bit having (1, 1) bit combination which gives result 1 as XNOR. Also, let’s say till the same position there is x number of zeroes. Therefore, there are y*(y-1)/2 pairs for an ith bit having (0, 0) bit combination which gives result 1 as XNOR.
- Now for the case of leading zeroes, multiply the number of wasted bits with the number of 0 bits. Do this for each bit position and calculate the answer.
For better understanding, Consider the array {35, 42, 27, 69}. Here are the binary representations of all four elements of the array.
Binary representations of array elements.
- The one-pointed with the arrow is the first bit of that element which is 1. Hence it is the most significant bit for that element. Similarly, MSB for all other elements is colored green.
- The zeroes appearing before the MSB are wasted bits and colored in red. The size of these binary arrays are 30 each.
- In the first 23 positions of the bits array, the number of wasted bits are four, and the number of 1’s are zero, and the number of 0’s are zero, as seen in the picture.
- In the 24th position, wasted bits are 3, and the number of 1’s will be 1 as MSB is encountered at the 24th element of element 69.
23rd bit: No. of wasted bits: 4, No. of 1’s: 0, No.of 0’s: 0
24th bit: No. of wasted bits: 3, No. of 1’s: 1, No.of 0’s: 0
25th bit: No. of wasted bits: 1, No. of 1’s: 2, No.of 0’s: 1 ( since it occurs after MSB )
26th bit: No. of wasted bits: 0, No. of 1’s: 1, No.of 0’s: 3
27th bit: No. of wasted bits: 0, No. of 1’s: 2, No.of 0’s: 2
28th bit: No. of wasted bits: 0, No. of 1’s: 1, No.of 0’s: 3
29th bit: No. of wasted bits: 0, No. of 1’s: 3, No.of 0’s: 1
30th bit: No. of wasted bits: 0, No. of 1’s: 3, No.of 0’s: 1
- Now in each of these bits, for the XNOR value to be 1 in any of the pair of bits, either both should be 1, or both should be 0.
- For both should be 1, it has N[i]*(N[i]-1)/2 pairs, where N[i] is the number of 1’s in ith position in all the elements.
- Similarly, for both should be 0, it has M[i]*(M[i]-1)/2 pairs, where M[i] is the number of 0’s in ith position in all the elements. Since the required answer is the sum of all possibilities. So, for an ith bit, add 2^(30-i-1) for all possible pairs.
- In case both should be 0, also consider wasted bits which occur in where any of the elements have a 0 (non-wasted) in the same position of the wasted bits. Let the wasted bits be W[i].
- So total sum = (N[i]*(N[i]-1)/2)*(2^(30-i-1) + (M[i]*(M[i]-1)/2)*(2^(30-i-1) + (W[i]*M[i]*(2^(30-i-1))) where 0<= i <=30.
- So, for the above example, till 23rd bit, everything is 0. From 24th bit is listed here.
- total = ((1*0)/2)*(2^6) + ((0*(-1))/2)*(2^6) + 3*0*(2^6) + ((2*1)/2)*(2^5) + ((1*0)/2)*(2^5) + 1*1*(2^5) + ((1*0)/2)*(2^4) + ((3*2)/2)*(2^4) + 0*3*(2^4) + ((2*1)/2)*(2^3) + ((2*1)/2)*(2^3) + 0*2*(2^3) + ((1*0)/2)*(2^2) + ((3*2)/2)*(2^2) + 0*3*(2^2) + ((3*2)/2)*(2^1) + ((1*0)/2)*(2^1) + 0*1*(2^1) + ((3*2)/2)*(2^0) + ((1*0)/2)*(2^0) + 0*1*(2^0)
- total = 0 + 0 + 0 + 32 + 0 + 32 + 0 + 48 + 0 + 8 + 8 + 0 + 0 + 12 + 0 + 6 + 0 + 0 + 3 + 0 + 0
- Therefore, total sum = 149
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int n, int r[])
{
int result = 0;
int bits[30][3];
memset (bits, 0, sizeof (bits));
for ( int i = 0; i < n; i++) {
int num = r[i];
int flag = 0;
for ( int j = 29; j >= 0; j--) {
if (flag == 0) {
if (num >= pow (2, j)) {
flag = 1;
num -= pow (2, j);
bits[29 - j][0]++;
}
else {
bits[29 - j][2]++;
}
continue ;
}
if (num >= pow (2, j)) {
num -= pow (2, j);
bits[29 - j][0]++;
}
else {
bits[29 - j][1]++;
}
}
}
for ( int i = 0; i < 30; i++) {
int y = bits[i][0];
int x = bits[i][1];
int msboff = bits[i][2];
int onePairs = (y * (y - 1)) / 2;
result += onePairs * pow (2, 30 - i - 1);
int zeroPairs = (x * (x - 1)) / 2;
result += zeroPairs * pow (2, 30 - i - 1);
result += (msboff * x) * pow (2, 30 - i - 1);
}
return result;
}
int main()
{
int n = 5;
int r[5] = { 2, 2, 2, 1, 1 };
cout << findSum(n, r) << endl;
return 0;
}
|
Java
class Main {
static int findSum( int n, int [] r) {
int result = 0 ;
int [][] bits = new int [ 30 ][ 3 ];
for ( int i : r) {
String binary = Integer.toBinaryString(i);
binary = String.format( "%30s" , binary).replace( ' ' , '0' );
int flag = 0 ;
for ( int j = 0 ; j < 30 ; j++) {
if (flag == 0 ) {
if (binary.charAt(j) == '1' ) {
flag = 1 ;
bits[j][ 0 ] += 1 ;
} else {
bits[j][ 2 ] += 1 ;
}
continue ;
}
if (binary.charAt(j) == '1' ) {
bits[j][ 0 ] += 1 ;
} else {
bits[j][ 1 ] += 1 ;
}
}
}
for ( int i = 0 ; i < 30 ; i++) {
int y = bits[i][ 0 ];
int x = bits[i][ 1 ];
int msboff = bits[i][ 2 ];
int onePairs = (y * (y - 1 )) / 2 ;
result += onePairs * ( int ) Math.pow( 2 , 30 - i - 1 );
int zeroPairs = ((x*(x- 1 ))/ 2 );
result += zeroPairs * Math.pow( 2 , 30 -i- 1 );
result += (msboff * x)* Math.pow( 2 , 30 -i- 1 );
}
return result;
}
public static void main(String[] args) {
int n = 5 ;
int [] r = { 2 , 2 , 2 , 1 , 1 };
System.out.println(findSum(n, r));
}
}
|
Python3
def findSum(n, r):
result = 0
bits = [[ 0 , 0 , 0 ] for i in range ( 30 )]
for i in r:
binary = bin (i)[ 2 :]
binary = binary.zfill( 30 )
flag = 0
for j in range ( 30 ):
if (flag = = 0 ):
if (binary[j] = = '1' ):
flag = 1
bits[j][ 0 ] + = 1
else :
bits[j][ 2 ] + = 1
continue
if (binary[j] = = '1' ):
bits[j][ 0 ] + = 1
else :
bits[j][ 1 ] + = 1
for i in range ( 30 ):
y = bits[i][ 0 ]
x = bits[i][ 1 ]
msboff = bits[i][ 2 ]
onePairs = (y * (y - 1 )) / / 2
result + = onePairs * pow ( 2 , 30 - i - 1 )
zeroPairs = ((x * (x - 1 )) / / 2 )
result + = zeroPairs * pow ( 2 , 30 - i - 1 )
result + = (msboff * x) * pow ( 2 , 30 - i - 1 )
return result
if __name__ = = '__main__' :
n = 5
r = [ 2 , 2 , 2 , 1 , 1 ]
print (findSum(n, r))
|
C#
using System;
public class GFG {
static int findSum( int n, int [] r)
{
int result = 0;
int [, ] bits = new int [30, 3];
for ( int i = 0; i < r.Length; i++) {
string binary
= Convert.ToString(r[i], 2).PadLeft(30,
'0' );
int flag = 0;
for ( int j = 0; j < 30; j++) {
if (flag == 0) {
if (binary[j] == '1' ) {
flag = 1;
bits[j, 0] += 1;
}
else {
bits[j, 2] += 1;
}
continue ;
}
if (binary[j] == '1' ) {
bits[j, 0] += 1;
}
else {
bits[j, 1] += 1;
}
}
}
for ( int i = 0; i < 30; i++) {
int y = bits[i, 0];
int x = bits[i, 1];
int msboff = bits[i, 2];
int onePairs = (y * (y - 1)) / 2;
result
+= onePairs * ( int )Math.Pow(2, 30 - i - 1);
int zeroPairs = ((x * (x - 1)) / 2);
result
+= zeroPairs * ( int )Math.Pow(2, 30 - i - 1);
result += (msboff * x)
* ( int )Math.Pow(2, 30 - i - 1);
}
return result;
}
static public void Main()
{
int n = 5;
int [] r = { 2, 2, 2, 1, 1 };
Console.WriteLine(findSum(n, r));
}
}
|
Javascript
<script>
const findSum = (n, r) => {
let result = 0;
let bits = new Array(30).fill(0).map(() => new Array(3).fill(0));
for (let i in r) {
let binary = Number(r[i]).toString(2);
binary = binary.split( "" );
while (binary.length < 30) {
binary.unshift( '0' );
}
let flag = 0;
for (let j = 0; j < 30; j++) {
if (flag == 0) {
if (binary[j] == '1' ) {
flag = 1;
bits[j][0] += 1;
}
else
bits[j][2] += 1;
continue ;
}
if (binary[j] == '1' )
bits[j][0] += 1;
else
bits[j][1] += 1;
}
}
for (let i = 0; i < 30; i++) {
let y = bits[i][0];
let x = bits[i][1];
let msboff = bits[i][2];
let onePairs = (y * (y - 1)) / 2;
result += onePairs * Math.pow(2, 30 - i - 1);
let zeroPairs = (x * (x - 1)) / 2;
result += zeroPairs * Math.pow(2, 30 - i - 1);
result += (msboff * x) * Math.pow(2, 30 - i - 1);
}
return result;
}
let n = 5;
let r = [2, 2, 2, 1, 1];
document.write(findSum(n, r));
</script>
|
Time Complexity: O(30*N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...