![]() |
VOOZH | about |
Given a binary array arr[N], (where N = 2) of size two having at least one element as zero. The task is to write a single line function to set both elements of the array to zero. There is a constraint to writing the function. The ternary operator and direct assignment of elements cannot be used.
As per problem constraints, only three combinations of array elements are possible:
- arr[0] = 1 and arr[1] = 0
- arr[0] = 0 and arr[1] = 1
- arr[0] = 0 and arr[1] = 0
This article discusses the following methods:
Let's start discussing each of these methods in detail.
1. Using only assignment operator:
The assignment operator can be used to set both the elements of the given binary array to 0, but in this approach, the indexes are not used directly.
Approach:
There are three ways to achieve this:
1. arr[arr[1]] = arr[arr[0]]
If arr={0, 1}, then arr[0] will be assigned to arr[1].
If arr={1, 0}, then arr[1] will be assigned to arr[0].2. arr[arr[1]] = 0
If arr[1]=0, then arr[0] will be 1, so arr[arr[1]] will make arr[0]=0.
If arr[1]=1, then arr[1] will be 1, so arr[arr[1]] will make arr[1]=0.3. arr[1 – arr[0]] = arr[1 – arr[1]]
If arr[1]=0 and arr[0]=1, then 1-arr[1] will be 1, so arr[1] will be assigned to arr[0].
If arr[1]=1 and arr[0]=0, then 1-arr[1] will be 0, so arr[0] will be assigned to arr[1].
Below is the C++ code to implement the approach:
0 0 0 0 0 0
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
2. Using assignment operator two times:
As listed under the constraints, direct assignment is not allowed. Thus arr[0]=0 and arr[1]=0 are not valid statements. The assignment operator will be used twice to set both elements to zero.
Approach:
There are three ways to achieve this:
1. arr[0] = arr[1] = arr[0] & arr[1]
if any one of the elements is 1.
AND of 1 and 0 is always 0. So, both gets value 0.2. arr[0] = arr[1] -= arr[1]
If arr[1]=1 then arr[1] gets 1-1=0 so, both becomes 0.3. arr[1] = arr[0] -= arr[0]
If arr[0]=1, then arr[0] gets 1-1=0.
else arr[0]= 0-0 = 0. So, both becomes 0.
Below is the C++ program to implement the approach:
0 0 0 0 0 0
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Note:
Time complexity is O(1) since just one statement is used.
3. By using the negation (!) operator (logical NOT):
In this approach, the assignment operator is used with a negation operator to make both elements of the given array 0 in a single line of code.
Approach:
There are three ways to do this:
1. arr[!arr[0]] = arr[arr[0]]
If arr={0, 1} then index 1 is assigned the index 0 value.
If arr={1, 0} then index 0 is given the index 1 value.2. arr[arr[1]] = arr[!arr[1]]
If arr={0, 1} then index 0 value is assigned to the index 1.
If arr={1, 0} then index 1 value is assigned to the index 0.3. arr[!arr[0]] = arr[!arr[1]]
If arr={0, 1}, since 1 is the value at index 1, the index 0 value which is 0 again,
will be assigned to index 1, making array full of zeros.
If arr={1, 0} then index 1 value is assigned to the index 0.
Below is the C++ program to implement the approach:
0 0 0 0 0 0
Time Complexity: O(1), the code will run in a constant time.
Auxiliary Space: O(1), no extra space is required, so it is a constant.