Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Medium
  • Last Updated : 01 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[0] = 1 and arr[1] = 0
  2. arr[0] = 0 and arr[1] = 1
  3. arr[0] = 0 and arr[1] = 0

This article discusses the following methods:

  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[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:

C++




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

Java




// Java program to set both elements
// to 0 in binary array[2]
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[1]] = 0;
    // arr[1 - arr[0]] = arr[1 - arr[1]];
}
 
// 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[1]] = 0;
    # arr[1 - arr[0]] = arr[1 - arr[1]];
 
# Driver code
First_Arr = [0, 1]
MakeBothZeros(First_Arr)
print(f"{First_Arr[0]} {First_Arr[1]}  ")
 
Second_Arr = [1, 0]
MakeBothZeros(Second_Arr)
print(f"{Second_Arr[0]} {Second_Arr[1]} ")
 
Thrd_Arr = [0, 0]
MakeBothZeros(Thrd_Arr)
print(f"{Thrd_Arr[0]} {Thrd_Arr[1]} ")
 
# This code is contributed by GFGKING

C#




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

Javascript




<script>
      // JavaScript code for the above approach
 
      function MakeBothZeros(arr) {
          arr[arr[1]] = arr[arr[0]];
 
          // Two other approaches to solve
          // the problem
          // arr[arr[1]] = 0;
          // arr[1 - arr[0]] = arr[1 - arr[1]];
      }
 
      // Driver code
 
      let First_Arr = [0, 1];
      MakeBothZeros(First_Arr);
      document.write(First_Arr[0] + " " +
          First_Arr[1] + "<br>");
 
      let Second_Arr = [1, 0];
      MakeBothZeros(Second_Arr);
      document.write(Second_Arr[0] + " " +
          Second_Arr[1] + '<br>');
 
      let Thrd_Arr = [0, 0];
      MakeBothZeros(Thrd_Arr);
      document.write(Thrd_Arr[0] + " " +
          Thrd_Arr[1] + '<br>');
 
// This code is contributed by Potta Lokesh
  </script>

 
 

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]=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:

 

C++




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

Java




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

Javascript




<script>
// Javascript program to set both elements
// to 0 in binary array[2].
 
function MakeBothZeros(arr) {
 
    arr[0] = arr[1] = arr[0] & arr[1]
 
    // Two other approaches to solve
    // the problem
    // arr[0] = arr[1] -= arr[1];
    // arr[1] = arr[0] -= arr[0];
}
 
// Driver code
let First_Arr = [0, 1];
MakeBothZeros(First_Arr);
document.write(First_Arr[0] + " " +
            First_Arr[1]);
 
let Second_Arr = [1, 0];
MakeBothZeros(Second_Arr);
document.write(Second_Arr[0] + " " +
            Second_Arr[1]);
 
let Thrd_Arr = [0, 0];
MakeBothZeros(Thrd_Arr);
document.write(Thrd_Arr[0] + " " +
            Thrd_Arr[1]);
             
// This code is contributed by Samim Hossain Mondal.
</script>

 
 

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[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:

 

C++




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

Java




// Java program to set both elements
// to 0 in binary array[2].
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[1]] = arr[!arr[1]]
    // arr[!arr[0]] = arr[!arr[1]]
}
 
// 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

Javascript




<script>
 
// JavaScript program to set both elements
// to 0 in binary array[2].
function MakeBothZeros(arr)
{
    arr[Number(!arr[0])] = arr[arr[0]];
 
    // Two other approaches to solve
    // the problem
    // arr[arr[1]] = arr[!arr[1]]
    // arr[!arr[0]] = arr[!arr[1]]
}
 
// Driver code
let First_Arr = [0, 1];
MakeBothZeros(First_Arr);
document.write(First_Arr[0] + " " + First_Arr[1],"</br>");
 
let Second_Arr = [1, 0];
MakeBothZeros(Second_Arr);
document.write(Second_Arr[0] + " " + Second_Arr[1],"</br>")
 
let Third_Arr = [0, 0];
MakeBothZeros(Third_Arr);
document.write(Third_Arr[0] + " " + Third_Arr[1],"</br>")
 
// This code is contributed by shinjanpatra
 
</script>

 
 

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
Recommended Articles
Page :

Start Your Coding Journey Now!