A Boolean Array Puzzle in C?


This an array based puzzle that need you to change all the numbers of an array the contains two elements to 0. One element of the array is 0 and other may or may not be 0.

To solve this puzzle the program needs to find the non-zero element and change in to 0.

Their are the following constraints that are needed to solve the boolean array puzzle 

  • Allowed operation is complement, other operations are not allowed.
  • Loops and conditional statements are not allowed.
  • Direct assignment is also not allowed.

PROGRAM TO SOLVE BOOLEAN ARRAY PUZZLE

#include <iostream>
using namespace std;
void makeZero(int a[2]) {
   a[ a[1] ] = a[ !a[1] ];
}
int main() {
   int a[] = {1, 0};
   makeZero(a);
   cout<<"arr[0] = "<<a[0]<<endl;
   cout<<"arr[1] = "<<a[1];
   return 0;
}

Output

arr[0] = 0
arr[1] = 0
You can use other ways too. Like this one which does not require the negation operation.
a[ a[1] ] = a[ a[0] ]
raja
Published on 08-Aug-2019 09:54:27
Advertisements