Composite XOR and Coprime AND

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the count of
// odd numbers in the array
int countOdd(int arr[], int n)
{
  
    // Variable to count odd numbers
    int odd = 0;
  
    for (int i = 0; i < n; i++) {
  
        // Odd number
        if (arr[i] % 2 == 1)
            odd++;
    }
  
    return odd;
}
  
// Function to return the count of valid pairs
int countValidPairs(int arr[], int n)
{
    int odd = countOdd(arr, n);
  
    return (odd * (odd - 1)) / 2;
}
  
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countValidPairs(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
    // Function to return the count of
    // odd numbers in the array
    static int countOdd(int [] arr, int n)
    {
      
        // Variable to count odd numbers
        int odd = 0;
      
        for (int i = 0; i < n; i++) 
        {
      
            // Odd number
            if (arr[i] % 2 == 1)
                odd++;
        }
        return odd;
    }
      
    // Function to return the count of valid pairs
    static int countValidPairs(int [] arr, int n)
    {
        int odd = countOdd(arr, n);
      
        return (odd * (odd - 1)) / 2;
    }
      
    // Driver Code
    public static void main(String []args)
    {
        int [] arr = { 1, 2, 3, 4, 5 };
        int n = arr.length;
        System.out.println(countValidPairs(arr, n));
    }
}
  
// This code is contributed by ihritik            

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
  
# Function to return the count of
# odd numbers in the array
def countOdd(arr, n):
  
    # Variable to count odd numbers
    odd = 0;
  
    for i in range(0, n): 
  
        # Odd number
        if (arr[i] % 2 == 1):
            odd = odd + 1;
      
    return odd;
  
# Function to return the count 
# of valid pairs
def countValidPairs(arr, n):
  
    odd = countOdd(arr, n);
  
    return (odd * (odd - 1)) / 2;
  
# Driver Code
arr = [1, 2, 3, 4, 5 ];
n = len(arr);
print(int(countValidPairs(arr, n)));
      
# This code is contributed by
# Shivi_Aggarwal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
    // Function to return the count of
    // odd numbers in the array
    static int countOdd(int [] arr, int n)
    {
      
        // Variable to count odd numbers
        int odd = 0;
      
        for (int i = 0; i < n; i++)
        {
      
            // Odd number
            if (arr[i] % 2 == 1)
                odd++;
        }
        return odd;
    }
      
    // Function to return the count of valid pairs
    static int countValidPairs(int [] arr, int n)
    {
        int odd = countOdd(arr, n);
      
        return (odd * (odd - 1)) / 2;
    }
      
    // Driver Code
    public static void Main()
    {
        int [] arr = { 1, 2, 3, 4, 5 };
        int n = arr.Length;
        Console.WriteLine(countValidPairs(arr, n));
    }
}
          
// This code is contributed by ihritik
  
         

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
  
// Function to return the count of 
// odd numbers in the array 
function countOdd($arr, $n
  
    // Variable to count odd numbers 
    $odd = 0; 
  
    for ($i = 0; $i < $n; $i++)
    
  
        // Odd number 
        if ($arr[$i] % 2 == 1) 
            $odd++; 
    
  
    return $odd
  
// Function to return the count 
// of valid pairs 
function countValidPairs($arr, $n
    $odd = countOdd($arr, $n); 
  
    return ($odd * ($odd - 1)) / 2; 
  
// Driver Code 
$arr = array(1, 2, 3, 4, 5); 
$n = sizeof($arr);
echo countValidPairs($arr, $n); 
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

3

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.