📌  相关文章
📜  计算给定数组中按位异或等于 K 的所有三元组

📅  最后修改于: 2022-05-13 01:56:07.703000             🧑  作者: Mango

计算给定数组中按位异或等于 K 的所有三元组

给定一个数组arr[] ,其中包含N个正整数和一个整数K 。任务是计算所有 XOR 等于K的三元组。即arr[ i ] ^ arr[ j ] ^ arr[ k ] = X其中 0 ≤ i < j < k < N (基于 0 的索引)

例子:

朴素方法:一个简单的方法是检查每个三元组,如果它按位异或等于K ,则将计数增加 1。
最后,返回计数。

下面是上述方法的实现:

C++
// C++ code to implement the above approach
  
#include 
using namespace std;
  
// Count of all valid triplets
int count_triplets(int arr[], int N, int K)
{
    int cnt = 0;
  
    // Fixed first element as arr[i]
    for (int i = 0; i < N; i++) {
  
        // Fixed second element as arr[j]
        for (int j = i + 1; j < N; j++) {
  
            // Now look for third element
            for (int k = j + 1; k < N; k++) {
                int triplet_xor = arr[i] ^ arr[j] ^ arr[k];
  
                // If xor is equal to K
                // increase cnt by 1.
                if (triplet_xor == K) {
                    cnt++;
                }
            }
        }
    }
    return cnt;
}
  
// Driver code
int main()
{
    int N = 6;
    int arr[] = { 2, 1, 3, 7, 5, 4 };
    int K = 5;
  
    // Function call
    cout << count_triplets(arr, N, K);
    return 0;
}


C
// C code to implement the approach
  
#include 
  
// Function to count of all valid triplets
int count_triplets(int arr[], int N, int K)
{
    int cnt = 0;
  
    // Fixed first element as arr[i]
    for (int i = 0; i < N; i++) {
  
        // Fixed second element as arr[j]
        for (int j = i + 1; j < N; j++) {
  
            // Now look for third element
            for (int k = j + 1; k < N; k++) {
                int triplet_xor
                    = arr[i] ^ arr[j] ^ arr[k];
  
                // If xor is equal to X
                // increase cnt by 1.
                if (triplet_xor == K) {
                    cnt++;
                }
            }
        }
    }
    return cnt;
}
  
// Drvier code
int main()
{
    int N = 6;
    int arr[] = { 2, 1, 3, 7, 5, 4 };
    int K = 5;
  
    // Function call
    printf("%d", count_triplets(arr, N, K));
    return 0;
}


Java
// Java code to implement the apprroach
  
import java.util.*;
  
class FindTriplet {
  
    // Function to count all triplets
    int count_triplets(int arr[], int N, int K)
    {
        int cnt = 0;
  
        // Fix the first element as arr[i]
        for (int i = 0; i < N; i++) {
  
            // Fix the second element as arr[j]
            for (int j = i + 1; j < N; j++) {
  
                // Now look for the third number
                for (int k = j + 1; k < N;
                     k++) {
                    int triplet_xor
                        = arr[i] ^ arr[j]
                          ^ arr[k];
  
                    // If xor is equal to X
                    // increase cnt by 1.
                    if (triplet_xor == K) {
                        cnt++;
                    }
                }
            }
        }
  
        return cnt;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        FindTriplet triplet = new FindTriplet();
        int N = 6;
        int arr[] = { 2, 1, 3, 7, 5, 4 };
        int K = 5;
  
        // Function call
        System.out.print(triplet.count_triplets(arr, N, K));
    }
}


C++
// C++ code to implement the approach
  
#include 
using namespace std;
  
// Function to count all valid triplets
int count_triplets(int arr[], int N, int K)
{
    // Sort array to perform lower_bound
    sort(arr, arr + N);
    int cnt = 0;
  
    // Fixed arr[i] as a first element
    for (int i = 0; i < N; i++) {
  
        // Fixed arr[j] as a second element
        for (int j = i + 1; j < N; j++) {
            int third_ele = K ^ arr[i] ^ arr[j];
  
            // Find third element
            auto it = lower_bound(arr + j + 1,
                                  arr + N, third_ele)
                      - arr;
            // If third element is present
            // then increase cnt by 1
            if (it != N && arr[it] == third_ele)
                cnt++;
        }
    }
  
    return cnt;
}
  
// Driver code
int main()
{
    int N = 6;
    int arr[] = { 2, 1, 3, 7, 5, 4 };
    int K = 5;
  
    // Function call
    cout << count_triplets(arr, N, K);
    return 0;
}


输出
2

时间复杂度: O(N 3)
辅助空间: O(1)

Efficient Approach:基于以下思想,使用排序二分搜索和异或属性可以有效地解决问题:

请按照以下步骤实施该想法:

  • 以非递减顺序对给定数组进行排序
  • 制作一个指向第一个三元组的 for 循环。
    • 制作嵌套的 for 循环,它将指向三元组的第二个数字
      • 第三个数字是: third_ele = X ^ arr[ i ] ^ arr[ j ] ,因为如果 a^b^c = d 那么 c = d^a^b (异或属性)。
      • 在 [ j+1, N-1 ] 中进行二分查找。如果third_ele在此范围内,则将计数增加 1。
  • 最后返回count

下面是上述方法的实现:

C++

// C++ code to implement the approach
  
#include 
using namespace std;
  
// Function to count all valid triplets
int count_triplets(int arr[], int N, int K)
{
    // Sort array to perform lower_bound
    sort(arr, arr + N);
    int cnt = 0;
  
    // Fixed arr[i] as a first element
    for (int i = 0; i < N; i++) {
  
        // Fixed arr[j] as a second element
        for (int j = i + 1; j < N; j++) {
            int third_ele = K ^ arr[i] ^ arr[j];
  
            // Find third element
            auto it = lower_bound(arr + j + 1,
                                  arr + N, third_ele)
                      - arr;
            // If third element is present
            // then increase cnt by 1
            if (it != N && arr[it] == third_ele)
                cnt++;
        }
    }
  
    return cnt;
}
  
// Driver code
int main()
{
    int N = 6;
    int arr[] = { 2, 1, 3, 7, 5, 4 };
    int K = 5;
  
    // Function call
    cout << count_triplets(arr, N, K);
    return 0;
}
输出
2

时间复杂度: O( N 2 * logN)
辅助空间: O(1)