📌  相关文章
📜  LSB的按位与,按位OR和按位XOR为1的数组中的总对

📅  最后修改于: 2021-05-25 09:15:43             🧑  作者: Mango

给定大小为N的数组arr [] 。任务是找到对数(arr [i],arr [j])cntANDcntORcntXOR ,以便:

  1. cntAND:最低有效位的按位与为1的对的计数。
  2. cntOR:最低有效位的按位或为1的对数。
  3. cntXOR:最低有效位的按位XOR为1的对的计数。

例子:

方法:

  1. 要获得数组元素的LSB ,首先我们要计算总的偶数和奇数元素。偶数元素的LSB为0,奇数元素的LSB为1。
  2. 为了
    • XOR为1时,两个元素的LSB必须不同。
    • AND为1时,两个元素的LSB必须为1。
    • OR为1时,至少一个元素的LSB应为1。
  3. 因此,所需对的总数
    • 对于XOR: cntXOR = cntOdd * cntEven
    • 对于AND: cntAND = cntOdd *(cntOdd – 1)/ 2
    • 对于OR: cntOR =(cntOdd * cntEven)+ cntOdd *(cntOdd – 1)/ 2

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the count of required pairs
void CalculatePairs(int a[], int n)
{
 
    // To store the count of elements which
    // give remainder 0 i.e. even values
    int cnt_zero = 0;
 
    // To store the count of elements which
    // give remainder 1 i.e. odd values
    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;
}
 
// Driver code
int main()
{
    int a[] = { 1, 3, 4, 2 };
    int n = sizeof(a) / sizeof(a[0]);
 
    CalculatePairs(a, n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to find the count of required pairs
    static void CalculatePairs(int a[], int n)
    {
 
        // To store the count of elements which
        // give remainder 0 i.e. even values
        int cnt_zero = 0;
 
        // To store the count of elements which
        // give remainder 1 i.e. odd values
        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);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 1, 3, 4, 2 };
        int n = a.length;
 
        CalculatePairs(a, n);
    }
}


Python3
# Python3 program to find number of pairs
 
# Function to find the count of required pairs
def CalculatePairs(a, n):
 
    # To store the count of elements which
    # give remainder 0 i.e. even values
    cnt_zero = 0
 
    # To store the count of elements which
    # give remainder 1 i.e. odd values
    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))
     
 
# Driver code
if __name__ == '__main__':
     
    a = [1, 3, 4, 2]
    n = len(a)
     
    # Print the count
    CalculatePairs(a, n)


C#
// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to find the count of required pairs
    static void CalculatePairs(int[] a, int n)
    {
 
        // To store the count of elements which
        // give remainder 0 i.e. even values
        int cnt_zero = 0;
 
        // To store the count of elements which
        // give remainder 1 i.e. odd values
        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);
    }
 
    // Driver code
    public static void Main()
    {
        int[] a = { 1, 3, 4, 2 };
        int n = a.Length;
 
        // Print the count
        CalculatePairs(a, n);
    }
}


PHP


Javascript


输出:
cntXOR = 4
cntAND = 1
cntOR = 5

时间复杂度: O(N)