Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
Last Updated :
12 Dec, 2022
Given an array arr[] of size N. The task is to find the number of pairs (arr[i], arr[j]) as cntAND, cntOR, and cntXOR such that:
- cntAND: Count of pairs where bitwise AND of least significant bits is 1.
- cntOR: Count of pairs where bitwise OR of least significant bits is 1.
- cntXOR: Count of pairs where bitwise XOR of least significant bits is 1.
Examples:
Input: arr[] = {1, 2, 3}
Output:
cntXOR = 2
cntAND = 1
cntOR = 3
Array elements in binary are {01, 10, 11}
Total XOR pairs: 2 i.e., (1, 2) and (2, 3)
Total AND pairs: 1 i.e., (1, 3)
Total OR pairs: 3 i.e., (1, 2), (2, 3) and (1, 3)
Input: arr[] = {1, 3, 4, 2}
Output:
cntXOR = 4
cntAND = 1
cntOR = 5
Approach:
- To get the LSB of the elements of the array, first, we calculate the total even and odd elements. Even elements have LSB as 0 and odd elements have LSB as 1.
- In order for
- XOR to be 1, LSB of both the elements have to be different.
- AND to be 1, LSB of both the elements have to be 1.
- OR to be 1, at least one of the elements should have it’s LSB as 1.
- Therefore, total number of required pairs
- For XOR: cntXOR = cntOdd * cntEven
- For AND: cntAND = cntOdd * (cntOdd – 1) / 2
- For OR: cntOR = (cntOdd * cntEven) + cntOdd * (cntOdd – 1) / 2
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void CalculatePairs( int a[], int n)
{
int cnt_zero = 0;
int cnt_one = 0;
for ( int i = 0; i < n; i++) {
if (a[i] % 2 == 0)
cnt_zero += 1;
else
cnt_one += 1;
}
long int total_XOR_pairs = cnt_zero * cnt_one;
long int total_AND_pairs = (cnt_one) * (cnt_one - 1) / 2;
long int total_OR_pairs = cnt_zero * cnt_one
+ (cnt_one) * (cnt_one - 1) / 2;
cout << "cntXOR = " << total_XOR_pairs << endl;
cout << "cntAND = " << total_AND_pairs << endl;
cout << "cntOR = " << total_OR_pairs << endl;
}
int main()
{
int a[] = { 1, 3, 4, 2 };
int n = sizeof (a) / sizeof (a[0]);
CalculatePairs(a, n);
return 0;
}
|
Java
import java.io.*;
public class GFG {
static void CalculatePairs( int a[], int n)
{
int cnt_zero = 0 ;
int cnt_one = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] % 2 == 0 )
cnt_zero += 1 ;
else
cnt_one += 1 ;
}
int total_XOR_pairs = cnt_zero * cnt_one;
int total_AND_pairs = (cnt_one) * (cnt_one - 1 ) / 2 ;
int total_OR_pairs = cnt_zero * cnt_one
+ (cnt_one) * (cnt_one - 1 ) / 2 ;
System.out.println( "cntXOR = " + total_XOR_pairs);
System.out.println( "cntAND = " + total_AND_pairs);
System.out.println( "cntOR = " + total_OR_pairs);
}
public static void main(String[] args)
{
int a[] = { 1 , 3 , 4 , 2 };
int n = a.length;
CalculatePairs(a, n);
}
}
|
Python3
def CalculatePairs(a, n):
cnt_zero = 0
cnt_one = 0
for i in range ( 0 , n):
if (a[i] % 2 = = 0 ):
cnt_zero + = 1
else :
cnt_one + = 1
total_XOR_pairs = cnt_zero * cnt_one
total_AND_pairs = (cnt_one) * (cnt_one - 1 ) / 2
total_OR_pairs = cnt_zero * cnt_one + (cnt_one) * (cnt_one - 1 ) / 2
print ( "cntXOR = " , int (total_XOR_pairs))
print ( "cntAND = " , int (total_AND_pairs))
print ( "cntOR = " , int (total_OR_pairs))
if __name__ = = '__main__' :
a = [ 1 , 3 , 4 , 2 ]
n = len (a)
CalculatePairs(a, n)
|
C#
using System;
class GFG {
static void CalculatePairs( int [] a, int n)
{
int cnt_zero = 0;
int cnt_one = 0;
for ( int i = 0; i < n; i++) {
if (a[i] % 2 == 0)
cnt_zero += 1;
else
cnt_one += 1;
}
int total_XOR_pairs = cnt_zero * cnt_one;
int total_AND_pairs = (cnt_one) * (cnt_one - 1) / 2;
int total_OR_pairs = cnt_zero * cnt_one
+ (cnt_one) * (cnt_one - 1) / 2;
Console.WriteLine( "cntXOR = " + total_XOR_pairs);
Console.WriteLine( "cntAND = " + total_AND_pairs);
Console.WriteLine( "cntOR = " + total_OR_pairs);
}
public static void Main()
{
int [] a = { 1, 3, 4, 2 };
int n = a.Length;
CalculatePairs(a, n);
}
}
|
PHP
<?php
function CalculatePairs( $a , $n )
{
$cnt_zero = 0;
$cnt_one = 0;
for ( $i = 0; $i < $n ; $i ++) {
if ( $a [ $i ] % 2 == 0)
$cnt_zero += 1;
else
$cnt_one += 1;
}
$total_XOR_pairs = $cnt_zero * $cnt_one ;
$total_AND_pairs = ( $cnt_one ) * ( $cnt_one - 1) / 2;
$total_OR_pairs = $cnt_zero * $cnt_one
+ ( $cnt_one ) * ( $cnt_one - 1) / 2;
echo ( "cntXOR = $total_XOR_pairs\n" );
echo ( "cntAND = $total_AND_pairs\n" );
echo ( "cntOR = $total_OR_pairs\n" );
}
$a = array (1, 3, 4, 2);
$n = count ( $a );
CalculatePairs( $a , $n );
?>
|
Javascript
<script>
function CalculatePairs(a, n)
{
let cnt_zero = 0;
let cnt_one = 0;
for (let i = 0; i < n; i++) {
if (a[i] % 2 == 0)
cnt_zero += 1;
else
cnt_one += 1;
}
let total_XOR_pairs = cnt_zero * cnt_one;
let total_AND_pairs = (cnt_one) * (cnt_one - 1) / 2;
let total_OR_pairs = cnt_zero * cnt_one
+ (cnt_one) * (cnt_one - 1) / 2;
document.write( "cntXOR = " + total_XOR_pairs + "<br>" );
document.write( "cntAND = " + total_AND_pairs + "<br>" );
document.write( "cntOR = " + total_OR_pairs + "<br>" );
}
let a = [ 1, 3, 4, 2 ];
let n = a.length;
CalculatePairs(a, n);
</script>
|
Output
cntXOR = 4
cntAND = 1
cntOR = 5
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...