A Boolean Array Puzzle - GeeksforGeeks

18 Jan, 2022
Input: A array arr[] of two elements having value 0 and 1Output: 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++
C
Java
Python3
C#
Javascript
#include <bits/stdc++.h>using namespace std;void changeToZero(int a[2]){    a[ a[1] ] = a[ !a[1] ];} // Driver codeint main(){    int a[] = {1, 0};    changeToZero(a);         cout<<"arr[0] = "<<a[0]<<endl;    cout<<" arr[1] = "<<a[1];    return 0;} // This code is contributed by rathbhupendra
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;}
import java.io.*; class GFG{ public static void changeToZero(int a[]){    a[a[1]] = a[1 - a[1]];} // Driver codepublic static void main(String args[]){    int[] arr;    arr = new int[2];    arr[0] = 1;    arr[1] = 0;    changeToZero(arr);         System.out.println("arr[0]= " + arr[0]);    System.out.println("arr[1]= " + arr[1]);}} // This code is contributed by rohitsingh07052
def changeToZero(a):    a[ a[1] ] = a[ not a[1] ]    return a # Driver codeif __name__=='__main__':    a = [1, 0]    a = changeToZero(a);         print(" arr[0] = " + str(a[0]))    print(" arr[1] = " + str(a[1])) # This code is contributed by Yash_R
using System; class GFG {     public static void changeToZero(int[] a)    {        a[a[1]] = a[1 - a[1]];    }     // Driver code    public static void Main()    {        int[] arr;        arr = new int[2];        arr[0] = 1;        arr[1] = 0;        changeToZero(arr);         Console.WriteLine("arr[0]= " + arr[0]);        Console.WriteLine("arr[1]= " + arr[1]);    }} // This code is contributed by souravmahato348.
<script> function changeToZero(a){    a[a[1]] = a[1 - a[1]];}     // Driver codelet arr;arr = [];arr[0] = 1;arr[1] = 0;changeToZero(arr); document.write("arr[0] = " + arr[0] + "<br/>");document.write("arr[1] = " + arr[1]); // This code is contributed by avijitmondal1998 </script>
Method 2
C
Java
C#
Javascript
Python3
void changeToZero(int a[2]){    a[ !a[0] ] = a[ !a[1] ]}
void changeToZero(int [2]a) {  a[!a[0]] = a[!a[1]];} // This code is contributed by souravmahato348.
static void changeToZero(int [2]a) {  a[!a[0]] = a[!a[1]];} // This code is contributed by souravmahato348.
<script>function changeToZero(a){    a[ !a[0] ] = a[ !a[1] ];} // This code is contributed by souravmahato348.</script>
def changeToZero(a):         a[ !a[0] ] = a[ !a[1] ]
 Method 3This method doesn’t even need complement. 
C
Java
void changeToZero(int a[2]){    a[ a[1] ] = a[ a[0] ]}
static void changeToZero(int a[2]){    a[ a[1] ] = a[ a[0] ]} // this code is contributed by shivanisinghss2110
  
Method 4Thanks to purvi for suggesting this method.
C
void changeToZero(int a[2]){  a[0] = a[a[0]];  a[1] = a[0];}
YouTubeGeeksforGeeks500K subscribersA boolean array puzzle | Bit Algorithms | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:52•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=0ouXUmMThmU" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
There may be many more methods.Please write comments if you find the above codes incorrect, or find other ways to solve the same problem. 
rathbhupendra
Yash_R
rohitsingh07052
avijitmondal1998
souravmahato348
shivanisinghss2110
code_hunt
Bit Magic
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Bitwise Operators in C/C++
Left Shift and Right Shift Operators in C/C++
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Count set bits in an integer
How to swap two numbers without using a temporary variable?
Cyclic Redundancy Check and Modulo-2 Division
Little and Big Endian Mystery
Binary representation of a given number
Program to find whether a given number is power of 2
Set, Clear and Toggle a given bit of a number in C