📜  用按位与作为奇数对进行计数

📅  最后修改于: 2021-04-26 19:35:44             🧑  作者: Mango

给定一个由N个整数组成的数组。任务是找到对(i,j)的对数,以使A [i]和A [j]为奇数。
例子:

天真的方法是检查每对并打印对数。
下面是上述方法的实现:

C++
// C++ program to count pairs
// with AND giving a odd number
#include 
using namespace std;
 
// Function to count number of odd pairs
int findOddPair(int A[], int N)
{
    int i, j;
 
    // variable for counting odd pairs
    int oddPair = 0;
 
    // find all pairs
    for (i = 0; i < N; i++) {
        for (j = i + 1; j < N; j++) {
 
            // find AND operation
            // check odd or even
            if ((A[i] & A[j]) % 2 != 0)
                oddPair++;
        }
    }
    // return number of odd pair
    return oddPair;
}
// Driver Code
int main()
{
 
    int a[] = { 5, 1, 3, 2 };
    int n = sizeof(a) / sizeof(a[0]);
 
    // calling function findOddPair
    // and print number of odd pair
    cout << findOddPair(a, n) << endl;
 
    return 0;
}


Java
// Java program to count pairs
// with AND giving a odd number
class solution_1
{
     
// Function to count
// number of odd pairs
static int findOddPair(int A[],
                       int N)
{
    int i, j;
 
    // variable for counting
    // odd pairs
    int oddPair = 0;
 
    // find all pairs
    for (i = 0; i < N; i++)
    {
        for (j = i + 1; j < N; j++)
        {
 
            // find AND operation
            // check odd or even
            if ((A[i] & A[j]) % 2 != 0)
                oddPair++;
        }
    }
     
    // return number
    // of odd pair
    return oddPair;
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 5, 1, 3, 2 };
    int n = a.length;
 
    // calling function findOddPair
    // and print number of odd pair
    System.out.println(findOddPair(a, n));
}
}
 
// This code is contributed
// by Arnab Kundu


Python
# Python program to count pairs
# with AND giving a odd number
 
# Function to count number
# of odd pairs
def findOddPair(A, N):
 
    # variable for counting odd pairs
    oddPair = 0
 
    # find all pairs
    for i in range(0, N - 1):
        for j in range(i + 1, N - 1):
 
            # find AND operation
            # check odd or even
            if ((A[i] & A[j]) % 2 != 0):
                oddPair = oddPair + 1
         
    # return number of odd pair
    return oddPair
 
# Driver Code
a = [5, 1, 3, 2]
n = len(a)
 
# calling function findOddPair
# and print number of odd pair
print(findOddPair(a, n))
 
# This code is contributed
# by Shivi_Aggarwal


C#
// C# program to count pairs
// with AND giving a odd number
using System;
class GFG
{
     
// Function to count
// number of odd pairs
static int findOddPair(int []A,
                       int N)
{
    int i, j;
 
    // variable for counting
    // odd pairs
    int oddPair = 0;
 
    // find all pairs
    for (i = 0; i < N; i++)
    {
        for (j = i + 1; j < N; j++)
        {
 
            // find AND operation
            // check odd or even
            if ((A[i] & A[j]) % 2 != 0)
                oddPair++;
        }
    }
     
    // return number
    // of odd pair
    return oddPair;
}
 
// Driver Code
public static void Main()
{
    int []a = { 5, 1, 3, 2 };
    int n = a.Length;
 
    // calling function findOddPair
    // and print number of odd pair
    Console.WriteLine(findOddPair(a, n));
}
}
 
// This code is contributed
// inder_verma.


PHP


Javascript


C++
// C++ program to count pairs with Odd AND
#include 
using namespace std;
 
int findOddPair(int A[], int N)
{
    // Count total odd numbers in
    int count = 0;
    for (int i = 0; i < N; i++)
        if ((A[i] % 2 == 1))
            count++;
 
    // return count of even pair
    return count * (count - 1) / 2;
}
 
// Driver main
int main()
{
    int a[] = { 5, 1, 3, 2 };
    int n = sizeof(a) / sizeof(a[0]);
 
    // calling function findOddPair
    // and print number of odd pair
    cout << findOddPair(a, n) << endl;
    return 0;
}


Java
// Java program to count
// pairs with Odd AND
class solution_1
{
static int findOddPair(int A[],
                       int N)
{
    // Count total odd numbers in
    int count = 0;
    for (int i = 0; i < N; i++)
        if ((A[i] % 2 == 1))
            count++;
 
    // return count of even pair
    return count * (count - 1) / 2;
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 5, 1, 3, 2 };
    int n = a.length;
 
    // calling function findOddPair
    // and print number of odd pair
    System.out.println(findOddPair(a, n));
 
}
}
 
// This code is contributed
// by Arnab Kundu


Python
# Python program to count
# pairs with Odd AND
def findOddPair(A, N):
 
    # Count total odd numbers
    count = 0;
    for i in range(0, N - 1):
        if ((A[i] % 2 == 1)):
            count = count+1
 
    # return count of even pair
    return count * (count - 1) / 2
 
# Driver Code
a = [5, 1, 3, 2]
n = len(a)
 
# calling function findOddPair
# and print number of odd pair
print(int(findOddPair(a, n)))
     
# This code is contributed
# by Shivi_Aggarwal


C#
// C# program to count
// pairs with Odd AND
using System;
 
class GFG
{
public static int findOddPair(int[] A,
                              int N)
{
    // Count total odd numbers in
    int count = 0;
    for (int i = 0; i < N; i++)
    {
        if ((A[i] % 2 == 1))
        {
            count++;
        }
    }
 
    // return count of even pair
    return count * (count - 1) / 2;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] a = new int[] {5, 1, 3, 2};
    int n = a.Length;
 
    // calling function findOddPair
    // and print number of odd pair
    Console.WriteLine(findOddPair(a, n));
}
}
 
// This code is contributed
// by Shrikant13


PHP


Javascript


输出:
3

时间复杂度: O(N ^ 2)
一种有效的解决方案是对奇数进行计数。然后返回count *(count – 1)/ 2,因为只有当两个数字中的一对都为奇数时,两个数字的AND才能为奇数。

C++

// C++ program to count pairs with Odd AND
#include 
using namespace std;
 
int findOddPair(int A[], int N)
{
    // Count total odd numbers in
    int count = 0;
    for (int i = 0; i < N; i++)
        if ((A[i] % 2 == 1))
            count++;
 
    // return count of even pair
    return count * (count - 1) / 2;
}
 
// Driver main
int main()
{
    int a[] = { 5, 1, 3, 2 };
    int n = sizeof(a) / sizeof(a[0]);
 
    // calling function findOddPair
    // and print number of odd pair
    cout << findOddPair(a, n) << endl;
    return 0;
}

Java

// Java program to count
// pairs with Odd AND
class solution_1
{
static int findOddPair(int A[],
                       int N)
{
    // Count total odd numbers in
    int count = 0;
    for (int i = 0; i < N; i++)
        if ((A[i] % 2 == 1))
            count++;
 
    // return count of even pair
    return count * (count - 1) / 2;
}
 
// Driver Code
public static void main(String args[])
{
    int a[] = { 5, 1, 3, 2 };
    int n = a.length;
 
    // calling function findOddPair
    // and print number of odd pair
    System.out.println(findOddPair(a, n));
 
}
}
 
// This code is contributed
// by Arnab Kundu

Python

# Python program to count
# pairs with Odd AND
def findOddPair(A, N):
 
    # Count total odd numbers
    count = 0;
    for i in range(0, N - 1):
        if ((A[i] % 2 == 1)):
            count = count+1
 
    # return count of even pair
    return count * (count - 1) / 2
 
# Driver Code
a = [5, 1, 3, 2]
n = len(a)
 
# calling function findOddPair
# and print number of odd pair
print(int(findOddPair(a, n)))
     
# This code is contributed
# by Shivi_Aggarwal

C#

// C# program to count
// pairs with Odd AND
using System;
 
class GFG
{
public static int findOddPair(int[] A,
                              int N)
{
    // Count total odd numbers in
    int count = 0;
    for (int i = 0; i < N; i++)
    {
        if ((A[i] % 2 == 1))
        {
            count++;
        }
    }
 
    // return count of even pair
    return count * (count - 1) / 2;
}
 
// Driver Code
public static void Main(string[] args)
{
    int[] a = new int[] {5, 1, 3, 2};
    int n = a.Length;
 
    // calling function findOddPair
    // and print number of odd pair
    Console.WriteLine(findOddPair(a, n));
}
}
 
// This code is contributed
// by Shrikant13

的PHP


Java脚本


输出:
3

时间复杂度:O(N)