 Open in App
Not now

# Make given Binary array of size two to all 0s in a single line

• Difficulty Level : Medium
• Last Updated : 14 Jul, 2022

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:

1. arr = 1 and arr = 0
2. arr = 0 and arr = 1
3. arr = 0 and arr = 0

1. Using only assignment operator.
2. Using assignment operator two times.
3. negation (!) operator (logical NOT).

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] = arr[arr]
If arr={0, 1}, then arr will be assigned to arr.
If arr={1, 0}, then arr will be assigned to arr.

2. arr[arr] = 0
If arr=0, then arr will be 1, so arr[arr] will make arr=0.
If arr=1, then arr will be 1, so arr[arr] will make arr=0.

3. arr[1 – arr] = arr[1 – arr]
If arr=0 and arr=1, then 1-arr will be 1, so arr will be assigned to arr.
If arr=1 and arr=0, then 1-arr will be 0, so arr will be assigned to arr.

Below is the C++ code to implement the approach:

## C++

 `// C++ program to set both elements` `// to 0 in binary array.` `#include ` `using` `namespace` `std;`   `void` `MakeBothZeros(``int` `arr[])` `{` `    ``arr[arr] = arr[arr];`   `    ``// Two other approaches to solve ` `    ``// the problem` `    ``// arr[arr] = 0;` `    ``// arr[1 - arr] = arr[1 - arr];` `}`   `// Driver code` `int` `main()` `{` `    ``int` `First_Arr[] = {0, 1};` `    ``MakeBothZeros(First_Arr);` `    ``cout << First_Arr << ``" "` `<< ` `            ``First_Arr << endl;`   `    ``int` `Second_Arr[] = {1, 0};` `    ``MakeBothZeros(Second_Arr);` `    ``cout << Second_Arr << ``" "` `<< ` `            ``Second_Arr << endl;`   `    ``int` `Thrd_Arr[] = {0, 0};` `    ``MakeBothZeros(Thrd_Arr);` `    ``cout << Thrd_Arr << ``" "` `<< ` `            ``Thrd_Arr << endl;`   `    ``return` `0;` `}`

## Java

 `// Java program to set both elements` `// to 0 in binary array` `import` `java.util.*;` `class` `GFG{`   `static` `void` `MakeBothZeros(``int` `arr[])` `{` `    ``arr[arr[``1``]] = arr[arr[``0``]];`   `    ``// Two other approaches to solve ` `    ``// the problem` `    ``// arr[arr] = 0;` `    ``// arr[1 - arr] = arr[1 - arr];` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `First_Arr[] = {``0``, ``1``};` `    ``MakeBothZeros(First_Arr);` `    ``System.out.print(First_Arr[``0``]+ ``" "` `+  ` `            ``First_Arr[``1``] +``"\n"``);`   `    ``int` `Second_Arr[] = {``1``, ``0``};` `    ``MakeBothZeros(Second_Arr);` `    ``System.out.print(Second_Arr[``0``]+ ``" "` `+  ` `            ``Second_Arr[``1``] +``"\n"``);`   `    ``int` `Thrd_Arr[] = {``0``, ``0``};` `    ``MakeBothZeros(Thrd_Arr);` `    ``System.out.print(Thrd_Arr[``0``]+ ``" "` `+  ` `            ``Thrd_Arr[``1``] +``"\n"``);`   `}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# Python code for the above approach` `def` `MakeBothZeros(arr):` `    ``arr[arr[``1``]] ``=` `arr[arr[``0``]]`   `    ``# Two other approaches to solve` `    ``# the problem` `    ``# arr[arr] = 0;` `    ``# arr[1 - arr] = arr[1 - arr];`   `# Driver code` `First_Arr ``=` `[``0``, ``1``]` `MakeBothZeros(First_Arr)` `print``(f``"{First_Arr} {First_Arr}  "``)`   `Second_Arr ``=` `[``1``, ``0``]` `MakeBothZeros(Second_Arr)` `print``(f``"{Second_Arr} {Second_Arr} "``)`   `Thrd_Arr ``=` `[``0``, ``0``]` `MakeBothZeros(Thrd_Arr)` `print``(f``"{Thrd_Arr} {Thrd_Arr} "``)`   `# This code is contributed by GFGKING`

## C#

 `// C# program to set both elements` `// to 0 in binary array` `using` `System;` `class` `GFG {`   `  ``static` `void` `MakeBothZeros(``int``[] arr)` `  ``{` `    ``arr[arr] = arr[arr];`   `    ``// Two other approaches to solve` `    ``// the problem` `    ``// arr[arr] = 0;` `    ``// arr[1 - arr] = arr[1 - arr];` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main()` `  ``{` `    ``int``[] First_Arr = { 0, 1 };` `    ``MakeBothZeros(First_Arr);` `    ``Console.WriteLine(First_Arr + ``" "` `                      ``+ First_Arr);`   `    ``int``[] Second_Arr = { 1, 0 };` `    ``MakeBothZeros(Second_Arr);` `    ``Console.WriteLine(Second_Arr + ``" "` `                      ``+ Second_Arr);`   `    ``int``[] Thrd_Arr = { 0, 0 };` `    ``MakeBothZeros(Thrd_Arr);` `    ``Console.WriteLine(Thrd_Arr + ``" "` `+ Thrd_Arr);` `  ``}` `}`   `// This code is contributed by ukasp.`

## Javascript

 ``

Output

```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 and arr=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 = arr = arr & arr
if any one of the elements is 1.
AND of 1 and 0 is always 0. So, both gets value 0.

2. arr = arr -= arr
If arr=1 then arr gets 1-1=0 so, both becomes 0.

3. arr = arr -= arr
If arr=1, then arr gets 1-1=0.
else arr= 0-0 = 0. So, both becomes 0.

Below is the C++ program to implement the approach:

## C++

 `// C++ program to set both elements` `// to 0 in binary array.` `#include ` `using` `namespace` `std;`   `void` `MakeBothZeros(``int` `arr[])` `{` `    ``arr = arr = arr & arr;`   `    ``// Two other approaches to solve ` `    ``// the problem` `    ``// arr = arr -= arr;` `    ``// arr = arr -= arr;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `First_Arr[] = {0, 1};` `    ``MakeBothZeros(First_Arr);` `    ``cout << First_Arr << ``" "` `<< ` `            ``First_Arr << endl;`   `    ``int` `Second_Arr[] = {1, 0};` `    ``MakeBothZeros(Second_Arr);` `    ``cout << Second_Arr << ``" "` `<< ` `            ``Second_Arr << endl;`   `    ``int` `Thrd_Arr[] = {0, 0};` `    ``MakeBothZeros(Thrd_Arr);` `    ``cout << Thrd_Arr << ``" "` `<< ` `            ``Thrd_Arr << endl;`   `    ``return` `0;` `}`

## Java

 `// Java program to set both elements` `// to 0 in binary array.`   `import` `java.util.*;`   `class` `GFG{`   `static` `void` `MakeBothZeros(``int` `arr[])` `{` `    ``arr[``0``] = arr[``1``] = arr[``0``] & arr[``1``];`   `    ``// Two other approaches to solve ` `    ``// the problem` `    ``// arr = arr -= arr;` `    ``// arr = arr -= arr;` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `First_Arr[] = {``0``, ``1``};` `    ``MakeBothZeros(First_Arr);` `    ``System.out.print(First_Arr[``0``]+ ``" "` `+  ` `            ``First_Arr[``1``] +``"\n"``);`   `    ``int` `Second_Arr[] = {``1``, ``0``};` `    ``MakeBothZeros(Second_Arr);` `    ``System.out.print(Second_Arr[``0``]+ ``" "` `+  ` `            ``Second_Arr[``1``] +``"\n"``);`   `    ``int` `Thrd_Arr[] = {``0``, ``0``};` `    ``MakeBothZeros(Thrd_Arr);` `    ``System.out.print(Thrd_Arr[``0``]+ ``" "` `+  ` `            ``Thrd_Arr[``1``] +``"\n"``);`   `}` `}`   `// This code is contributed by 29AjayKumar`

## Python3

 `# Python program to set both elements` `# to 0 in binary array.` `def` `MakeBothZeros(arr):`   `    ``arr[``0``] ``=` `arr[``1``] ``=` `arr[``0``] & arr[``1``]`   `    ``# Two other approaches to solve` `    ``# the problem` `    ``# arr = arr -= arr;` `    ``# arr = arr -= arr;`   `# Driver code` `First_Arr ``=` `[``0``, ``1``]` `MakeBothZeros(First_Arr)` `print``(First_Arr[``0``], end``=``" "``)` `print``(First_Arr[``1``])`   `Second_Arr ``=` `[``0``, ``1``]` `MakeBothZeros(Second_Arr)` `print``(Second_Arr[``0``], end``=``" "``)` `print``(Second_Arr[``1``])`     `Thrd_Arr ``=` `[``0``, ``0``]` `MakeBothZeros(Thrd_Arr)` `print``(Thrd_Arr[``0``], end``=``" "``)` `print``(Thrd_Arr[``1``])`   `# This code is contributed by Samim Hossain Mondal.`

## C#

 `// C# program to set both elements` `// to 0 in binary array.` `using` `System;` `public` `class` `GFG` `{`   `  ``static` `void` `MakeBothZeros(``int` `[]arr) ` `  ``{` `    ``arr = arr = arr & arr;`   `    ``// Two other approaches to solve` `    ``// the problem` `    ``// arr = arr -= arr;` `    ``// arr = arr -= arr;` `  ``}`   `  ``// Driver code` `  ``public` `static` `void` `Main(String[] args) {` `    ``int``[] First_Arr = { 0, 1 };` `    ``MakeBothZeros(First_Arr);` `    ``Console.Write(First_Arr + ``" "` `+ First_Arr + ``"\n"``);`   `    ``int` `[]Second_Arr = { 1, 0 };` `    ``MakeBothZeros(Second_Arr);` `    ``Console.Write(Second_Arr + ``" "` `+ Second_Arr + ``"\n"``);`   `    ``int` `[]Thrd_Arr = { 0, 0 };` `    ``MakeBothZeros(Thrd_Arr);` `    ``Console.Write(Thrd_Arr + ``" "` `+ Thrd_Arr + ``"\n"``);`   `  ``}` `}`   `// This code is contributed by Rajput-Ji`

## Javascript

 ``

Output

```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] = arr[arr]
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] = arr[!arr]
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] = arr[!arr]
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:

## C++

 `// C++ program to set both elements` `// to 0 in binary array.` `#include ` `using` `namespace` `std;`   `void` `MakeBothZeros(``int` `arr[])` `{` `    ``arr[!arr] = arr[arr];`   `    ``// Two other approaches to solve` `    ``// the problem` `    ``// arr[arr] = arr[!arr]` `    ``// arr[!arr] = arr[!arr]` `}`   `// Driver code` `int` `main()` `{` `    ``int` `First_Arr[] = {0, 1};` `    ``MakeBothZeros(First_Arr);` `    ``cout << First_Arr << ``" "` `<< ` `            ``First_Arr << endl;`   `    ``int` `Second_Arr[] = {1, 0};` `    ``MakeBothZeros(Second_Arr);` `    ``cout << Second_Arr << ``" "` `<< ` `            ``Second_Arr << endl;`   `    ``int` `Thrd_Arr[] = {0, 0};` `    ``MakeBothZeros(Thrd_Arr);` `    ``cout << Thrd_Arr << ``" "` `<< ` `            ``Thrd_Arr << endl;`   `    ``return` `0;` `}`

## Java

 `// Java program to set both elements` `// to 0 in binary array.` `import` `java.util.*;`   `class` `GFG{`   `static` `void` `MakeBothZeros(``int` `arr[])` `{` `    `  `    ``int` `index = arr[``0``] == ``0``?``1``:``0``;` `    ``arr[index] = arr[arr[``0``]];` `  `  `    ``// Two other approaches to solve` `    ``// the problem` `    ``// arr[arr] = arr[!arr]` `    ``// arr[!arr] = arr[!arr]` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `    ``int` `First_Arr[] = {``0``, ``1``};` `    ``MakeBothZeros(First_Arr);` `    ``System.out.print(First_Arr[``0``]+ ``" "` `+  ` `            ``First_Arr[``1``] +``"\n"``);`   `    ``int` `Second_Arr[] = {``1``, ``0``};` `    ``MakeBothZeros(Second_Arr);` `    ``System.out.print(Second_Arr[``0``]+ ``" "` `+  ` `            ``Second_Arr[``1``] +``"\n"``);`   `    ``int` `Thrd_Arr[] = {``0``, ``0``};` `    ``MakeBothZeros(Thrd_Arr);` `    ``System.out.print(Thrd_Arr[``0``]+ ``" "` `+  ` `            ``Thrd_Arr[``1``] +``"\n"``);` `}` `}`   `// This code is contributed by shikhasingrajput `

## Python3

 `# Python program to set both elements to 0 in binary array.` `def` `MakeBothZeros(arr):` `    ``if` `(arr[``0``] ``=``=` `0``):` `        ``index ``=` `1` `    ``else``:` `        ``index ``=` `0` `    ``arr[index] ``=` `arr[arr[``0``]]`   `First_Arr ``=` `[``0``, ``1``]` `MakeBothZeros(First_Arr)` `print``(First_Arr[``0``], end``=``" "``)` `print``(First_Arr[``1``])`   `Second_Arr ``=` `[``0``, ``1``]` `MakeBothZeros(Second_Arr)` `print``(Second_Arr[``0``], end``=``" "``)` `print``(Second_Arr[``1``])`   `Thrd_Arr ``=` `[``0``, ``0``]` `MakeBothZeros(Thrd_Arr)` `print``(Thrd_Arr[``0``], end``=``" "``)` `print``(Thrd_Arr[``1``])`   `# This code is contributed by lokesh (lokeshmvs21).`

## C#

 `// C# program to set both elements` `// to 0 in binary array.` `using` `System;`   `public` `class` `GFG{`   `static` `void` `MakeBothZeros(``int` `[]arr)` `{` `    `  `    ``int` `index = arr == 0?1:0;` `    ``arr[index] = arr[arr];` `  `  `    ``// Two other approaches to solve` `    ``// the problem` `    ``// arr[arr] = arr[!arr]` `    ``// arr[!arr] = arr[!arr]` `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `    ``int` `[]First_Arr = {0, 1};` `    ``MakeBothZeros(First_Arr);` `    ``Console.Write(First_Arr+ ``" "` `+  ` `            ``First_Arr +``"\n"``);`   `    ``int` `[]Second_Arr = {1, 0};` `    ``MakeBothZeros(Second_Arr);` `    ``Console.Write(Second_Arr+ ``" "` `+  ` `            ``Second_Arr +``"\n"``);`   `    ``int` `[]Thrd_Arr = {0, 0};` `    ``MakeBothZeros(Thrd_Arr);` `    ``Console.Write(Thrd_Arr+ ``" "` `+  ` `            ``Thrd_Arr +``"\n"``);` `}` `}`   `// This code is contributed by shikhasingrajput`

## Javascript

 ``

Output

```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.

My Personal Notes arrow_drop_up
Related Articles