# A Boolean Array Puzzle

A Boolean Array Puzzle - Bit Algorithm - Following are the specifications to follow. It is guaranteed that one element is 0 but we do not know its position

Input: A array arr[] of two elements having value 0 and 1

Output: Make both elements 0.

Specifications: Following are the specifications to follow.
1) It is guaranteed that one element is 0 but we do not know its position.
2) We can’t say about another element it can be 0 or 1.
3) We can only complement array elements, no other operation like and, or, multi, division, …. etc.
4) We can’t use if, else and loop constructs.
5) Obviously, we can’t directly assign 0 to array elements.

There are several ways we can do it as we are sure that always one Zero is there. Thanks to devendraiiit for suggesting following 3 methods.

Method 1

c
``````void changeToZero(int a)
{
a[ a ] = a[ !a ];
}

int main()
{
int a[] = {1, 0};
changeToZero(a);

printf(" arr = %d \n", a);
printf(" arr = %d ", a);
getchar();
return 0;
}``````

Method 2

c
``````void changeToZero(int a)
{
a[ !a ] = a[ !a ]
}``````

Method 3

This method doesn’t even need complement.

c
``````void changeToZero(int a)
{
a[ a ] = a[ a ]
}``````

Method 4

c
``````void changeToZero(int a)
{
a = a[a];
a = a;
}``````

READ  Compute the minimum or maximum of two integers without branching 