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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- 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
- XOR of Sum of every possible pair of an array
- Sum of XOR of sum of all pairs in an array
- Sum of XOR of all pairs in an array
- Count inversions in an array | Set 3 (Using BIT)
- Sum of Bitwise And of all pairs in a given array
- Quadruplet pair with XOR zero in the given Array
- Bitwise AND of all the elements of array
- Maximum value of XOR among all triplets of an array
- Maximize the bitwise OR of an array
- Maximum XOR of Two Numbers in an Array
- Sum of Bitwise-OR of all subarrays of a given Array | Set 2
- Sum of Bitwise OR of all pairs in a given array