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.
Read full article from A Boolean Array Puzzle | GeeksforGeeks
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.
Method 1
void changeToZero( int a[2]) { a[ a[1] ] = a[ !a[1] ]; }
Method 2
|
void changeToZero( int a[2]) { a[ !a[0] ] = a[ !a[1] ] } |
Method 3
This method doesn’t even need complement.
This method doesn’t even need complement.
void changeToZero( int a[2]) { a[ a[1] ] = a[ a[0] ] } |
Method 4
void changeToZero( int a[2]) { a[0] = a[a[0]]; a[1] = a[0]; } |