Quadruplet pair with XOR zero in the given Array
Given an array arr[] of N integers such that any two adjacent elements in the array differ at only one position in their binary representation. The task is to find whether there exists a quadruple (arr[i], arr[j], arr[k], arr[l]) such that arr[i] ^ arr[j] ^ arr[k] ^ arr[l] = 0. Here ^ denotes the bitwise xor operation and 1 ≤ i < j < k < l ≤ N.
Examples:
Input: arr[] = {1, 3, 7, 3}
Output: No
1 ^ 3 ^ 7 ^ 3 = 6
Input: arr[] = {1, 0, 2, 3, 7}
Output: Yes
1 ^ 0 ^ 2 ^ 3 = 0
- Naive approach: Check for all possible quadruples whether their xor is zero or not. But the time complexity of such a solution would be N4, for all N.
Time Complexity:
- Efficient Approach (O(N4), for N ≤ 130): We can Say that for array length more than or equal to 130 we can have at least 65 adjacent pairs each denoting xor of two elements. Here it is given that all adjacent elements differ at only one position in their binary form thus there would result in only one set bit. Since we have only 64 possible positions, we can say that at least two pairs will have the same xor. Thus xor of these 4 integers will be 0. For N < 130 we can use the naive approach.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>usingnamespacestd;constintMAX = 130;// Function that returns true if the array// contains a valid quadruplet pairboolvalidQuadruple(intarr[],intn){// We can always find a valid quadruplet pair// for array size greater than MAXif(n >= MAX)returntrue;// For smaller size arrays, perform brute forcefor(inti = 0; i < n; i++)for(intj = i + 1; j < n; j++)for(intk = j + 1; k < n; k++)for(intl = k + 1; l < n; l++) {if((arr[i] ^ arr[j] ^ arr[k] ^ arr[l]) == 0) {returntrue;}}returnfalse;}// Driver codeintmain(){intarr[] = { 1, 0, 2, 3, 7 };intn =sizeof(arr) /sizeof(arr[0]);if(validQuadruple(arr, n))cout <<"Yes";elsecout <<"No";return0;}chevron_rightfilter_noneJava
// Java implementation of the approachimportjava.util.*;importjava.lang.*;importjava.io.*;classGFG{staticintMAX =130;// Function that returns true if the array// contains a valid quadruplet pairstaticbooleanvalidQuadruple(intarr[],intn){// We can always find a valid quadruplet pair// for array size greater than MAXif(n >= MAX)returntrue;// For smaller size arrays, perform brute forcefor(inti =0; i < n; i++)for(intj = i +1; j < n; j++)for(intk = j +1; k < n; k++)for(intl = k +1; l < n; l++){if((arr[i] ^ arr[j] ^arr[k] ^ arr[l]) ==0){returntrue;}}returnfalse;}// Driver codepublicstaticvoidmain (String[] args)throwsjava.lang.Exception{intarr[] = {1,0,2,3,7};intn = arr.length;if(validQuadruple(arr, n))System.out.println("Yes");elseSystem.out.println("No");}}// This code is contributed by nidhivachevron_rightfilter_nonePython3
# Python3 implementation of the approachMAX=130# Function that returns true if the array# contains a valid quadruplet pairdefvalidQuadruple(arr, n):# We can always find a valid quadruplet pair# for array size greater than MAXif(n >=MAX):returnTrue# For smaller size arrays,# perform brute forceforiinrange(n):forjinrange(i+1, n):forkinrange(j+1, n):forlinrange(k+1, n):if((arr[i] ^ arr[j] ^arr[k] ^ arr[l])==0):returnTruereturnFalse# Driver codearr=[1,0,2,3,7]n=len(arr)if(validQuadruple(arr, n)):print("Yes")else:print("No")# This code is contributed# by Mohit Kumarchevron_rightfilter_noneC#
// C# implementation of the approachusingSystem;classGFG{staticintMAX = 130;// Function that returns true if the array// contains a valid quadruplet pairstaticBoolean validQuadruple(int[]arr,intn){// We can always find a valid quadruplet pair// for array size greater than MAXif(n >= MAX)returntrue;// For smaller size arrays, perform brute forcefor(inti = 0; i < n; i++)for(intj = i + 1; j < n; j++)for(intk = j + 1; k < n; k++)for(intl = k + 1; l < n; l++){if((arr[i] ^ arr[j] ^arr[k] ^ arr[l]) == 0){returntrue;}}returnfalse;}// Driver codepublicstaticvoidMain (String[] args){int[]arr = { 1, 0, 2, 3, 7 };intn = arr.Length;if(validQuadruple(arr, n))Console.WriteLine("Yes");elseConsole.WriteLine("No");}}// This code is contributed by 29AjayKumarchevron_rightfilter_nonePHP
<?php// PHP implementation of the approachconstMAX = 130;// Function that returns true if the array// contains a valid quadruplet pairfunctionvalidQuadruple($arr,$n){// We can always find a valid quadruplet pair// for array size greater than MAXif($n>= MAX)returntrue;// For smaller size arrays,// perform brute forcefor($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){returntrue;}}returnfalse;}// Driver code$arr=array(1, 0, 2, 3, 7);$n=count($arr);if(validQuadruple($arr,$n))echo("Yes");elseecho("No");// This code is contributed by Naman_Garg?>chevron_rightfilter_noneOutput:Yes
Time Complexity:
- Another Efficient Approach (O(N2log N), for N ≤ 130):
Compute Xor of all pairs and hash it. ie, store indexes i and j in a list and Hash it in form <xor, list>. If the same xor is found again for different i and j, then we have a Quadruplet pair.Below is the implementation of the above approach :
// Java implementation of the approachimportjava.util.HashMap;importjava.util.LinkedList;importjava.util.List;importjava.util.Map;publicclassQuadrapuleXor {staticbooleancheck(intarr[]){intn = arr.length;if(n <4)returnfalse;if(n >=130)returntrue;Map<Integer,List<Integer>> map =newHashMap<>();for(inti=0;i<n-1;i++){for(intj=i+1;j<n;j++){intk = arr[i] ^ arr[j];if(!map.containsKey(k))map.put(k,newLinkedList<>());List<Integer> data = map.get(k);if(!data.contains(i) && !data.contains(j)){data.add(i);data.add(j);if(data.size()>=4)returntrue;map.put(k, data);}}}returnfalse;}// Driver codepublicstaticvoidmain (String[] args)throwsjava.lang.Exception{intarr[] = {1,0,2,3,7};if(check(arr))System.out.println("Yes");elseSystem.out.println("No");}}//This code contributed by Pramod Hosahallichevron_rightfilter_noneOutput:Yes
Recommended Posts:
- Print all Prime Quadruplet of a number less than it
- Given a sorted array and a number x, find the pair in array whose sum is closest to x
- XOR of Sum of every possible pair of an array
- Pair with largest sum which is less than K in the array
- Maximum OR value of a pair in an array
- Maximum AND value of a pair in an array
- Smallest Pair Sum in an array
- Print pair with maximum AND value in an array
- Find a pair from the given array with maximum nCr value
- Find pair with maximum GCD in an array
- Construct an array from its pair-product
- Given an array A[] and a number x, check for pair in A[] with sum as x
- Closest product pair in an array
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Check if product of every pair exists in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.






