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:
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 pair
boolvalidQuadruple(intarr[], intn)
{
// We can always find a valid quadruplet pair
// for array size greater than MAX
if(n >= MAX)
returntrue;
// For smaller size arrays, perform brute force
for(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 code
intmain()
{
intarr[] = { 1, 0, 2, 3, 7 };
intn = sizeof(arr) / sizeof(arr[0]);
if(validQuadruple(arr, n))
cout << "Yes";
else
cout << "No";
return0;
}
Java
// Java implementation of the approach
importjava.util.*;
importjava.lang.*;
importjava.io.*;
classGFG
{
staticintMAX = 130;
// Function that returns true if the array
// contains a valid quadruplet pair
staticbooleanvalidQuadruple(intarr[], intn)
{
// We can always find a valid quadruplet pair
// for array size greater than MAX
if(n >= MAX)
returntrue;
// For smaller size arrays, perform brute force
for(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 code
publicstaticvoidmain (String[] args)
throwsjava.lang.Exception
{
intarr[] = { 1, 0, 2, 3, 7};
intn = 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
defvalidQuadruple(arr, n):
# We can always find a valid quadruplet pair
# for array size greater than MAX
if(n >=MAX):
returnTrue
# For smaller size arrays,
# perform brute force
fori inrange(n):
forj inrange(i +1, n):
fork inrange(j +1, n):
forl inrange(k +1, n):
if((arr[i] ^ arr[j] ^
arr[k] ^ arr[l]) ==0):
returnTrue
returnFalse
# 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
usingSystem;
classGFG
{
staticintMAX = 130;
// Function that returns true if the array
// contains a valid quadruplet pair
staticBoolean validQuadruple(int[]arr, intn)
{
// We can always find a valid quadruplet pair
// for array size greater than MAX
if(n >= MAX)
returntrue;
// For smaller size arrays, perform brute force
for(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 code
publicstaticvoidMain (String[] args)
{
int[]arr = { 1, 0, 2, 3, 7 };
intn = arr.Length;
if(validQuadruple(arr, n))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by 29AjayKumar
PHP
<?php
// PHP implementation of the approach
constMAX = 130;
// Function that returns true if the array
// contains a valid quadruplet pair
functionvalidQuadruple($arr, $n)
{
// We can always find a valid quadruplet pair
// for array size greater than MAX
if($n>= MAX)
returntrue;
// 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)
{
returntrue;
}
}
returnfalse;
}
// 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
?>
Javascript
<script>
// Javascript implementation
// of the approach
const MAX = 130;
// Function that returns true if the array
// contains a valid quadruplet pair
functionvalidQuadruple(arr, n)
{
// We can always find a valid quadruplet pair
// for array size greater than MAX
if(n >= MAX)
returntrue;
// For smaller size arrays, perform brute force
for(let i = 0; i < n; i++)
for(let j = i + 1; j < n; j++)
for(let k = j + 1; k < n; k++)
for(let l = k + 1; l < n; l++) {
if((arr[i] ^ arr[j] ^ arr[k] ^
arr[l]) == 0) {
returntrue;
}
}
returnfalse;
}
// Driver code
let arr = [ 1, 0, 2, 3, 7 ];
let n = arr.length;
if(validQuadruple(arr, n))
document.write("Yes");
else
document.write("No");
</script>
Output:
Yes
Time Complexity:
Auxiliary Space: O(1)
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 :
// Function to check whether a quadruplet with XOR 0
// exists in the given array
staticboolcheck(int[] arr)
{
intn = arr.Length;
if(n < 4)
returnfalse;
if(n >= 130)
returntrue;
Dictionary<int, List<int> > map
= newDictionary<int, List<int> >();
for(inti = 0; i < n - 1; i++) {
for(intj = i + 1; j < n; j++) {
intk = arr[i] ^ arr[j];
if(!map.ContainsKey(k))
map[k] = newList<int>();
List<int> data = map[k];
if(!data.Contains(i)
&& !data.Contains(j)) {
data.Add(i);
data.Add(j);
if(data.Count >= 4)
returntrue;
map[k] = data;
}
}
}
returnfalse;
}
// Driver code
publicstaticvoidMain(string[] args)
{
int[] arr = { 1, 0, 2, 3, 7 };
// Function Call
if(check(arr))
Console.WriteLine("Yes");
else
Console.WriteLine("No");
}
}
// This code is contributed by phasing17
Output:
Yes
Time Complexity:
Auxiliary Space: O(N2)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!