logo CodeStepByStep logo

bitArrayPuzzle

Language/Type: C++ bit algorithms
Related Links:

Write a function named bitArrayPuzzle that accepts as a parameter a reference to a vector of integers. The vector is guaranteed to be of length 2. One of its elements is guaranteed to store the value 0. The other element could be 0 or 1. Your task is to modify the array so that both indexes store 0. For example, if the vector v contains {0, 1}, the call of bitArrayPuzzle(v) should modify it to store {0, 0}.

The challenge in this problem lies in its constraints. You must not directly assign the value 0 to any index of the vector. You are not allowed to use if or if/else statements, nor loops. Do not create any auxiliary collections such as vectors, arrays, lists, sets, etc. Also do not convert any integer into a string.

A correct solution can be done in O(1) time and space.

Function: Write a C++ function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.