📜  给定数组中XOR为零的四对子

📅  最后修改于: 2021-05-07 06:50:16             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,使得该数组中的任何两个相邻元素在其二进制表示形式中仅在一个位置不同。任务是查找是否存在四元组(arr [i],arr [j],arr [k],arr [l]) ,使得arr [i] ^ arr [j] ^ arr [k] ^ arr [ l] = 0^表示按位异或运算, 1≤i

例子:

  • 幼稚的方法:检查所有可能的四元组的异或是否为零。但是,对于所有N ,这种解决方案的时间复杂度将为N 4

    时间复杂度:

  • 高效方法( O(N 4 ),对于N&leq; 130 ):我们可以说,对于数组长度大于或等于130的数组,我们至少可以有65个相邻对,每个对表示两个元素的异或。在此假设所有相邻元素在其二进制形式的仅一个位置处不同,因此将仅产生一个置位。由于我们只有64个可能的位置,因此可以说至少有两对将具有相同的异或。因此,这4个整数的xor为0 。对于N <130,我们可以使用幼稚的方法。

    下面是上述方法的实现:

    C++
    // C++ implementation of the approach
    #include 
    using namespace std;
      
    const int MAX = 130;
      
    // Function that returns true if the array
    // contains a valid quadruplet pair
    bool validQuadruple(int arr[], int n)
    {
      
        // We can always find a valid quadruplet pair
        // for array size greater than MAX
        if (n >= MAX)
            return true;
      
        // For smaller size arrays, perform brute force
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                for (int k = j + 1; k < n; k++)
                    for (int l = k + 1; l < n; l++) {
                        if ((arr[i] ^ arr[j] ^ arr[k] ^ arr[l]) == 0) {
                            return true;
                        }
                    }
        return false;
    }
      
    // Driver code
    int main()
    {
        int arr[] = { 1, 0, 2, 3, 7 };
        int n = sizeof(arr) / sizeof(arr[0]);
      
        if (validQuadruple(arr, n))
            cout << "Yes";
        else
            cout << "No";
      
        return 0;
    }


    Java
    // Java implementation of the approach
    import java.util.*;
    import java.lang.*;
    import java.io.*;
      
    class GFG
    {
      
    static int MAX = 130; 
      
    // Function that returns true if the array 
    // contains a valid quadruplet pair 
    static boolean validQuadruple(int arr[], int n) 
    { 
      
        // We can always find a valid quadruplet pair 
        // for array size greater than MAX 
        if (n >= MAX) 
            return true; 
      
        // For smaller size arrays, perform brute force 
        for (int i = 0; i < n; i++) 
            for (int j = i + 1; j < n; j++) 
                for (int k = j + 1; k < n; k++) 
                    for (int l = k + 1; l < n; l++)
                    { 
                        if ((arr[i] ^ arr[j] ^ 
                             arr[k] ^ arr[l]) == 0) 
                        { 
                            return true; 
                        } 
                    } 
        return false; 
    } 
      
    // Driver code
    public static void main (String[] args) 
                  throws java.lang.Exception
    {
        int arr[] = { 1, 0, 2, 3, 7 }; 
        int n = arr.length;
      
        if (validQuadruple(arr, n)) 
            System.out.println("Yes"); 
        else
            System.out.println("No"); 
    }
    }
      
    // This code is contributed by nidhiva


    Python3
    # Python3 implementation of the approach
    MAX = 130
      
    # Function that returns true if the array
    # contains a valid quadruplet pair
    def validQuadruple(arr, n):
      
        # We can always find a valid quadruplet pair
        # for array size greater than MAX
        if (n >= MAX):
            return True
      
        # For smaller size arrays, 
        # perform brute force
        for i in range(n):
            for j in range(i + 1, n):
                for k in range(j + 1, n):
                    for l in range(k + 1, n):
                        if ((arr[i] ^ arr[j] ^ 
                             arr[k] ^ arr[l]) == 0):
                            return True
      
        return False
      
    # Driver code
    arr = [1, 0, 2, 3, 7]
    n = len(arr)
      
    if (validQuadruple(arr, n)):
        print("Yes")
    else:
        print("No")
      
    #  This code is contributed
    # by Mohit Kumar


    C#
    // C# implementation of the approach
    using System;
          
    class GFG
    {
      
    static int MAX = 130; 
      
    // Function that returns true if the array 
    // contains a valid quadruplet pair 
    static Boolean validQuadruple(int []arr, int n) 
    { 
      
        // We can always find a valid quadruplet pair 
        // for array size greater than MAX 
        if (n >= MAX) 
            return true; 
      
        // For smaller size arrays, perform brute force 
        for (int i = 0; i < n; i++) 
            for (int j = i + 1; j < n; j++) 
                for (int k = j + 1; k < n; k++) 
                    for (int l = k + 1; l < n; l++)
                    { 
                        if ((arr[i] ^ arr[j] ^ 
                             arr[k] ^ arr[l]) == 0) 
                        { 
                            return true; 
                        } 
                    } 
        return false; 
    } 
      
    // Driver code
    public static void Main (String[] args)
    {
        int []arr = { 1, 0, 2, 3, 7 }; 
        int n = arr.Length;
      
        if (validQuadruple(arr, n)) 
            Console.WriteLine("Yes"); 
        else
            Console.WriteLine("No"); 
    }
    }
      
    // This code is contributed by 29AjayKumar


    PHP
    = MAX) 
            return true; 
      
        // For smaller size arrays,
        // perform brute force 
        for ($i = 0; $i < $n; $i++) 
            for ($j = $i + 1; $j < $n; $j++) 
                for ($k = $j + 1; $k < $n; $k++) 
                    for ($l = $k + 1; $l < $n; $l++) 
                    { 
                        if (($arr[$i] ^ $arr[$j] ^ 
                             $arr[$k] ^ $arr[$l]) == 0) 
                        { 
                            return true; 
                        } 
                    } 
        return false; 
    } 
      
    // Driver code 
    $arr = array(1, 0, 2, 3, 7); 
    $n = count($arr); 
      
    if (validQuadruple($arr, $n)) 
        echo ("Yes"); 
    else
        echo ("No"); 
      
    // This code is contributed by Naman_Garg
    ?>


    输出:
    Yes
    

    时间复杂度:

  • 另一种有效的方法(对于N&leq; 130为O(N 2 log N ):
    计算所有对的Xor并对其进行哈希处理。即,将索引i和j存储在列表中,并以的形式将其散列。如果对于不同的i和j再次找到相同的xor,则我们有一个Quadruplet对。

    下面是上述方法的实现:

    // Java implementation of the approach
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
      
    public class QuadrapuleXor {
      
        static boolean check(int arr[])
        {
            int n = arr.length;
              
            if(n < 4)
                return false;
            if(n >=130)
                return true;
                  
            Map> map = new HashMap<>();
              
            for(int i=0;i());
                      
                    List data = map.get(k);
                    if(!data.contains(i) && !data.contains(j))
                    {
                        data.add(i);
                        data.add(j);
                        if(data.size()>=4)
                            return true;
                        map.put(k, data);
                    }
                }    
            }
            return false;
        }
          
        // Driver code
        public static void main (String[] args) 
                      throws java.lang.Exception
        {
            int arr[] = { 1, 0, 2, 3, 7 }; 
           
            if (check(arr)) 
                System.out.println("Yes"); 
            else
                System.out.println("No"); 
        }
    }
      
    //This code contributed by Pramod Hosahalli
    
    输出:
    Yes
    

    时间复杂度: