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.

[ad type=”banner”]

Method 1

[pastacode lang=”c” manual=”void%20changeToZero(int%20a%5B2%5D)%0A%7B%0A%20%20%20a%5B%20a%5B1%5D%20%5D%20%3D%20a%5B%20!a%5B1%5D%20%5D%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20%20int%20a%5B%5D%20%3D%20%7B1%2C%200%7D%3B%0A%20%20%20changeToZero(a)%3B%0A%20%0A%20%20%20printf(%22%20arr%5B0%5D%20%3D%20%25d%20%5Cn%22%2C%20a%5B0%5D)%3B%0A%20%20%20printf(%22%20arr%5B1%5D%20%3D%20%25d%20%22%2C%20a%5B1%5D)%3B%0A%20%20%20getchar()%3B%0A%20%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/]

Method 2

[pastacode lang=”c” manual=”void%20changeToZero(int%20a%5B2%5D)%0A%7B%0A%20%20%20%20a%5B%20!a%5B0%5D%20%5D%20%3D%20a%5B%20!a%5B1%5D%20%5D%0A%7D” message=”c” highlight=”” provider=”manual”/]

Method 3

This method doesn’t even need complement.

[pastacode lang=”c” manual=”void%20changeToZero(int%20a%5B2%5D)%0A%7B%0A%20%20%20%20a%5B%20a%5B1%5D%20%5D%20%3D%20a%5B%20a%5B0%5D%20%5D%0A%7D” message=”c” highlight=”” provider=”manual”/]

Method 4

[pastacode lang=”c” manual=”void%20changeToZero(int%20a%5B2%5D)%0A%7B%0A%20%20a%5B0%5D%20%3D%20a%5Ba%5B0%5D%5D%3B%0A%20%20a%5B1%5D%20%3D%20a%5B0%5D%3B%0A%7D” message=”c” highlight=”” provider=”manual”/] [ad type=”banner”]