Composite XOR and Coprime AND
Last Updated :
23 Jun, 2022
Given an array arr[], the task is to count the number of unordered pairs of indices (i, j) such the gcd(2, a[i]^a[j]) > 1 and gcd(2, a[i] & a[j]) < 2 where ^ and & are bitwise XOR and bitwise AND operations respectively.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 3
All valid pairs are (1, 3), (1, 5) and (3, 5)
Input: arr[] = {21, 121, 13, 44, 65}
Output: 6
Approach:
- gcd(2, a[i]^a[j]) > 1 will only be true if both a[i] and a[j] are either even or odd.
- Narrowing down the pairs from the previous step, a pair (a, b) will never yield gcd(2, a[i] & a[j]) < 2 if both a and b are even. So, only pairs where a and b are both odd will satisfy both the given conditions.
- Count the number of odd element sin the given array and store it ion odd and the result will be (odd * (odd – 1)) / 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countOdd( int arr[], int n)
{
int odd = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 1)
odd++;
}
return odd;
}
int countValidPairs( int arr[], int n)
{
int odd = countOdd(arr, n);
return (odd * (odd - 1)) / 2;
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << countValidPairs(arr, n);
return 0;
}
|
Java
class GFG
{
static int countOdd( int [] arr, int n)
{
int odd = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] % 2 == 1 )
odd++;
}
return odd;
}
static int countValidPairs( int [] arr, int n)
{
int odd = countOdd(arr, n);
return (odd * (odd - 1 )) / 2 ;
}
public static void main(String []args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int n = arr.length;
System.out.println(countValidPairs(arr, n));
}
}
|
Python3
def countOdd(arr, n):
odd = 0 ;
for i in range ( 0 , n):
if (arr[i] % 2 = = 1 ):
odd = odd + 1 ;
return odd;
def countValidPairs(arr, n):
odd = countOdd(arr, n);
return (odd * (odd - 1 )) / 2 ;
arr = [ 1 , 2 , 3 , 4 , 5 ];
n = len (arr);
print ( int (countValidPairs(arr, n)));
|
C#
using System;
class GFG
{
static int countOdd( int [] arr, int n)
{
int odd = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] % 2 == 1)
odd++;
}
return odd;
}
static int countValidPairs( int [] arr, int n)
{
int odd = countOdd(arr, n);
return (odd * (odd - 1)) / 2;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int n = arr.Length;
Console.WriteLine(countValidPairs(arr, n));
}
}
|
PHP
<?php
function countOdd( $arr , $n )
{
$odd = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] % 2 == 1)
$odd ++;
}
return $odd ;
}
function countValidPairs( $arr , $n )
{
$odd = countOdd( $arr , $n );
return ( $odd * ( $odd - 1)) / 2;
}
$arr = array (1, 2, 3, 4, 5);
$n = sizeof( $arr );
echo countValidPairs( $arr , $n );
?>
|
Javascript
<script>
function countOdd(arr, n)
{
var odd = 0;
for ( var i = 0; i < n; i++)
{
if (arr[i] % 2 == 1)
odd++;
}
return odd;
}
function countValidPairs(arr, n)
{
var odd = countOdd(arr, n);
return (odd * (odd - 1)) / 2;
}
var arr = [ 1, 2, 3, 4, 5 ];
var n = arr.length;
document.write(countValidPairs(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...