📜  复合XOR和Coprime AND

📅  最后修改于: 2021-04-24 04:13:13             🧑  作者: Mango

给定数组arr [] ,任务是计算无序索引对(i,j)的数量,例如gcd(2,a [i] ^ a [j])> 1gcd(2,a [i] ]&a [j])<2 ,其中^分别是按位XOR按位AND运算。
例子:

方法:

  • 仅当a [i]a [j]均为偶数奇数时,gcd(2,a [i] ^ a [j])> 1才为true。
  • 缩小上一步中的对如果ab均为偶数,则对(a,b)将永远不会产生gcd(2,a [i]&a [j])<2 。因此,只有ab都为奇数的对才能满足给定条件。
  • 计算给定阵列中奇数元素sin的数目,并将其存储为奇数离子,结果将为(odd *(odd – 1))/ 2

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


Java
// 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


Python3
# 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


C#
// 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


PHP


Javascript


输出:
3

时间复杂度: O(N)