*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 <bits/stdc++.h> ` `using` `namespace` `std; ` `void` `changeToZero(` `int` `a[2]) ` `{ ` ` ` `a[ a[1] ] = a[ !a[1] ]; ` `} ` ` ` `// Driver code ` `int` `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 ` |

*chevron_right*

*filter_none*

## C

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

*chevron_right*

*filter_none*

## Python3

`# Python3 program for a ` `# boolean array puzzle ` `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[0] = "` `+` `str` `(a[` `0` `])) ` ` ` `print` `(` `" arr[1] = "` `+` `str` `(a[` `1` `])) ` ` ` `# This code is contributed by Yash_R ` |

*chevron_right*

*filter_none*

**Method 2**

`void` `changeToZero(` `int` `a[2]) ` `{ ` ` ` `a[ !a[0] ] = a[ !a[1] ] ` `} ` |

*chevron_right*

*filter_none*

**Method 3**

This method doesn’t even need complement.

`void` `changeToZero(` `int` `a[2]) ` `{ ` ` ` `a[ a[1] ] = a[ a[0] ] ` `} ` |

*chevron_right*

*filter_none*

**Method 4**

Thanks to **purvi** for suggesting this method.

`void` `changeToZero(` `int` `a[2]) ` `{ ` ` ` `a[0] = a[a[0]]; ` ` ` `a[1] = a[0]; ` `} ` |

*chevron_right*

*filter_none*

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Lucky alive person in a circle | Code Solution to sword puzzle
- Construct an array from XOR of all elements of array except element at same index
- Find Duplicates of array using bit array
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Number of ways to change the Array such that largest element is LCM of array
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element
- Minimize K whose XOR with given array elements leaves array unchanged
- Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2
- Program to count number of set bits in an (big) array
- Count inversions in an array | Set 3 (Using BIT)
- Find the maximum subarray XOR in a given array
- Sum of Bitwise And of all pairs in a given array
- Find even occurring elements in an array of limited range
- Count all pairs of an array which differ in K bits
- Find missing number in another array which is shuffled copy
- Reverse an array without using subtract sign ‘-‘ anywhere in the code
- Check in binary array the number represented by a subarray is odd or even
- Replace every element of the array with BitWise XOR of all other
- Add minimum number to an array so that the sum becomes even