# A Boolean Array Puzzle

• Difficulty Level : Medium
• Last Updated : 16 May, 2022

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++

 `#include ` `using` `namespace` `std; ` `void` `changeToZero(``int` `a) ` `{ ` `    ``a[ a ] = a[ !a ]; ` `} `   `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = {1, 0}; ` `    ``changeToZero(a); ` `    `  `    ``cout<<``"arr = "``<

## 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; ` `} `

## Java

 `import` `java.io.*;`   `class` `GFG{`   `public` `static` `void` `changeToZero(``int` `a[]) ` `{ ` `    ``a[a[``1``]] = a[``1` `- a[``1``]]; ` `} `   `// Driver code` `public` `static` `void` `main(String args[])` `{ ` `    ``int``[] arr;` `    ``arr = ``new` `int``[``2``];` `    ``arr[``0``] = ``1``;` `    ``arr[``1``] = ``0``;` `    ``changeToZero(arr); ` `    `  `    ``System.out.println(``"arr= "` `+ arr[``0``]); ` `    ``System.out.println(``"arr= "` `+ arr[``1``]); ` `} ` `}`   `// This code is contributed by rohitsingh07052`

## Python3

 `def` `changeToZero(a): ` `    ``a[ a[``1``] ] ``=` `a[ ``not` `a[``1``] ] ` `    ``return` `a `   `# Driver code ` `if` `__name__``=``=``'__main__'``: ` `    ``a ``=` `[``1``, ``0``] ` `    ``a ``=` `changeToZero(a); ` `    `  `    ``print``(``" arr = "` `+` `str``(a[``0``])) ` `    ``print``(``" arr = "` `+` `str``(a[``1``])) `   `# This code is contributed by Yash_R `

## C#

 `using` `System;`   `class` `GFG {`   `    ``public` `static` `void` `changeToZero(``int``[] a)` `    ``{` `        ``a[a] = a[1 - a];` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int``[] arr;` `        ``arr = ``new` `int``;` `        ``arr = 1;` `        ``arr = 0;` `        ``changeToZero(arr);`   `        ``Console.WriteLine(``"arr= "` `+ arr);` `        ``Console.WriteLine(``"arr= "` `+ arr);` `    ``}` `}`   `// This code is contributed by souravmahato348.`

## Javascript

 ``

Method 2

## C

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

## Java

 `void` `changeToZero(``int` `[``2``]a) { ` `  ``a[!a[``0``]] = a[!a[``1``]];` `}`   `// This code is contributed by souravmahato348.`

## Python3

 `def` `changeToZero(a):` `    `  `    ``a[ !a[``0``] ] ``=` `a[ !a[``1``] ]` `  `  `  ``# This code is contributed by sanjoy_62.`

## C#

 `static` `void` `changeToZero(``int` `a) { ` `  ``a[!a] = a[!a];` `}`   `// This code is contributed by souravmahato348.`

## Javascript

 ``

Method 3
This method doesn’t even need complement.

## C

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

## Java

 `static` `void` `changeToZero(``int` `a[``2``]) ` `{ ` `    ``a[ a[``1``] ] = a[ a[``0``] ] ` `} `   `// this code is contributed by shivanisinghss2110`

## Python3

 `def` `changeToZero(a) :` `    `  `    ``a[ a[``1``] ] ``=` `a[ a[``0``] ]`

## Javascript

 `function` `changeToZero(a) ` `{ ` `     ``a[ a ] = a[ a ];` `} `     `//this code is contributed by phasing17`

Method 4
Thanks to purvi for suggesting this method.

## C

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

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :