0% found this document useful (0 votes)
110 views3 pages

Boolean Array Puzzle Solutions

The document describes a Boolean array puzzle and provides 4 methods to solve it. The puzzle involves an array with two elements that are either 0 or 1, with the goal of making both elements 0 using only bitwise complement operations and without using loops or conditional statements. The 4 methods provided use bitwise operations on the array indices and elements to solve the puzzle in a single operation.

Uploaded by

chinni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views3 pages

Boolean Array Puzzle Solutions

The document describes a Boolean array puzzle and provides 4 methods to solve it. The puzzle involves an array with two elements that are either 0 or 1, with the goal of making both elements 0 using only bitwise complement operations and without using loops or conditional statements. The 4 methods provided use bitwise operations on the array indices and elements to solve the puzzle in a single operation.

Uploaded by

chinni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

9/2/2016 A Boolean Array Puzzle ­ GeeksforGeeks

GeeksforGeeks
A computer science portal for geeks
 

Placements   Practice   GATE CS   IDE   Q&A   GeeksQuiz


Login/Register

A Boolean Array Puzzle
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

void changeToZero(int a[2])
{
   a[ a[1] ] = a[ !a[1] ];
}
 
int main()
{
   int a[] = {1, 0};
   changeToZero(a);
 
   printf(" arr[0] = %d \n", a[0]);
   printf(" arr[1] = %d ", a[1]);
   getchar();
   return 0;
}
Run on IDE

Method 2

void changeToZero(int a[2])
{
    a[ !a[0] ] = a[ !a[1] ]
}
Run on IDE

Method 3
This method doesn’t even need complement.

void changeToZero(int a[2])
{
    a[ a[1] ] = a[ a[0] ]
[Link] 1/3
9/2/2016 A Boolean Array Puzzle ­ GeeksforGeeks
    a[ a[1] ] = a[ a[0] ]
}
Run on IDE

Method 4
Thanks to purvi for suggesting this method.

void changeToZero(int a[2])
{
  a[0] = a[a[0]];
  a[1] = a[0];
}
Run on IDE

There may be many more methods.

Source: [Link]

Please write comments if you find the above codes incorrect, or find other ways to solve the same problem.

Company Wise Coding Practice    Topic Wise Coding Practice

35 Comments  Category:  Bit Magic

Related Posts:
Find profession in a special family
Print first n numbers with exactly two set bits
Generate 0 and 1 with 25% and 75% probability
Find even occurring elements in an array of limited range
Cyclic Redundancy Check and Modulo­2 Division
Copy set bits in a range
Check if a number is Bleak
Count strings with consecutive 1’s

[Link] 2/3
9/2/2016 A Boolean Array Puzzle ­ GeeksforGeeks

(Login to Rate and Mark)  

Average Difficulty : 3.4/5.0  Add to TODO List
3.4  Based on 13 vote(s)
 

 Mark as DONE

Writing code in comment? Please use [Link], generate link and share the link here.

@geeksforgeeks, Some rights reserved        Contact Us!        About Us!               Advertise with us!       

[Link] 3/3

You might also like