Set both elements of a binary array to 0 in a single line
Given a binary array of size two having at least one element as zero, write a single line function to set both its elements to zero. Use of ternary operator and direct assignment of elements are not allowed.
There are three combinations of array elements as per problem constraints:
- arr[0] = 0 and arr[1] = 1
- arr[0] = 1 and arr[1] = 0
- arr[0] = 0 and arr[1] = 0
There are many ways to solve the given problem. We will discuss a few of them:
Method 1: Using assignment operator twice
We can use any of the following single line expressions to convert both elements of the given array to 0:
- arr[0] = arr[1] = arr[!arr[1]], or
- arr[0] = arr[1] = arr[0] & arr[1], or
- arr[0] = arr[1] -= arr[1] // or arr[1] = arr[0] -= arr[0]
This is demonstrated below in C++ and Java:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> using namespace std; void convert(int arr[]) { arr[0] = arr[1] = arr[!arr[1]]; // arr[0] = arr[1] = arr[0] & arr[1]; // arr[0] = arr[1] -= arr[1]; // arr[1] = arr[0] -= arr[0]; } int main() { int first[] = { 0, 1 }; convert(first); cout << first[0] << " " << first[1] << endl; int second[] = { 1, 0 }; convert(second); cout << second[0] << " " << second[1] << endl; int third[] = { 0, 0 }; convert(third); cout << third[0] << " " << third[1] << endl; return 0; } |
Output:
0 0
0 0
0 0
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Main { public static void convert(int[] arr) { arr[0] = arr[1] = arr[0] & arr[1]; // arr[0] = arr[1] -= arr[1]; // arr[1] = arr[0] -= arr[0]; } public static void main(String[] args) { int[] first = { 0, 1 }; convert(first); System.out.println(first[0] + " " + first[1]); int[] second = { 1, 0 }; convert(second); System.out.println(second[0] + " " + second[1]); int[] third = { 0, 0 }; convert(third); System.out.println(third[0] + " " + third[1]); } } |
Output:
0 0
0 0
0 0
Method 2: Using negation (logical NOT) operator
We can use the negation operator with an assignment operator to convert both elements of the given array to 1 in a single line:
- arr[!arr[0]] = arr[arr[0]], or
- arr[arr[1]] = arr[!arr[1]], or
- arr[!arr[0]] = arr[!arr[1]]
Following is the C++ and Java program that demonstrates it:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> using namespace std; void convert(int arr[]) { arr[!arr[0]] = arr[arr[0]]; // arr[arr[1]] = arr[!arr[1]]; // arr[!arr[0]] = arr[!arr[1]]; } int main() { int first[] = { 0, 1 }; convert(first); cout << first[0] << " " << first[1] << endl; int second[] = { 1, 0 }; convert(second); cout << second[0] << " " << second[1] << endl; int third[] = { 0, 0 }; convert(third); cout << third[0] << " " << third[1] << endl; return 0; } |
Output:
0 0
0 0
0 0
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Main { public static void convert(int[] arr) { arr[arr[1]] = arr[arr[0]]; // arr[1 - arr[0]] = arr[1 - arr[1]]; // arr[arr[1]] = 0; } public static void main(String[] args) { int[] first = { 0, 1 }; convert(first); System.out.println(first[0] + " " + first[1]); int[] second = { 1, 0 }; convert(second); System.out.println(second[0] + " " + second[1]); int[] third = { 0, 0 }; convert(third); System.out.println(third[0] + " " + third[1]); } } |
Output:
0 0
0 0
0 0
Method 3: Using only assignment operator
We can directly use an assignment operator to set both elements of the given binary array to 0 in a single line:
- arr[arr[1]] = arr[arr[0]], or
- arr[1 – arr[0]] = arr[1 – arr[1]], or
- arr[arr[1]] = 0
The implementation can be seen below in C++ and Java:
C++
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <iostream> using namespace std; void convert(int arr[]) { arr[arr[1]] = arr[arr[0]]; // arr[1 - arr[0]] = arr[1 - arr[1]]; // arr[arr[1]] = 0; } int main() { int first[] = { 0, 1 }; convert(first); cout << first[0] << " " << first[1] << endl; int second[] = { 1, 0 }; convert(second); cout << second[0] << " " << second[1] << endl; int third[] = { 0, 0 }; convert(third); cout << third[0] << " " << third[1] << endl; return 0; } |
Output:
0 0
0 0
0 0
Java
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Main { public static void convert(int[] arr) { arr[arr[1]] = arr[arr[0]]; // arr[1 - arr[0]] = arr[1 - arr[1]]; // arr[arr[1]] = 0; } public static void main(String[] args) { int[] first = { 0, 1 }; convert(first); System.out.println(first[0] + " " + first[1]); int[] second = { 1, 0 }; convert(second); System.out.println(second[0] + " " + second[1]); int[] third = { 0, 0 }; convert(third); System.out.println(third[0] + " " + third[1]); } } |
Output:
0 0
0 0
0 0
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)