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.
This method doesn’t even need complement.
Thanks to purvi for suggesting this method.
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.
- Swap all odd and even bits
- Count total set bits in all numbers from 1 to n
- Swap bits in a given number
- Add two numbers without using arithmetic operators
- Smallest of three integers without comparison operators
- Program to count number of set bits in an (big) array
- Next higher number with same number of set bits
- Optimization Techniques | Set 1 (Modulus)
- Add 1 to a given number
- Find whether a given number is a power of 4 or not
- Compute the integer absolute value (abs) without branching
- Compute the minimum or maximum of two integers without branching
- Find the two non-repeating elements in an array of repeating elements
- Write an Efficient C Program to Reverse Bits of a Number
- Write an Efficient Method to Check if a Number is Multiple of 3