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 = 6Input: 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




