Make given Binary array of size two to all 0s in a single line
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:
- 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:
- Using only assignment operator.
- Using assignment operator two times.
- 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>
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>
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
Python3
# Python program to set both elements to 0 in binary array[2].
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[2].
using System;
public 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);
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 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>
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.
Similar Reads
Count the number of 1's and 0's in a binary array using STL in C++ ?
Given a binary array, the task is to count the number of 1's and 0's in this array using STL in C++. Examples: Input: arr[] = {1, 0, 0, 1, 0, 0, 1} Output: 1's = 3, 0's = 4 Input: arr[] = {1, 1, 1, 1, 0, 0, 1} Output: 1's = 5, 0's = 2 Approach: We can count the same using count_if() function present
1 min read
Count total set bits in all numbers from 1 to n | Set 2
Given a positive integer N, the task is to count the sum of the number of set bits in the binary representation of all the numbers from 1 to N.Examples: Input: N = 3 Output: 4 DecimalBinarySet Bit Count101121013112 1 + 1 + 2 = 4Input: N = 16 Output: 33 Recommended PracticeCount total set bitsTry It!
11 min read
Maximize the decimal equivalent by flipping only a contiguous set of 0s
Given a binary number in the form of a string, the task is to print a binary equivalent obtained by flipping only one contiguous set of 0s such that the decimal equivalent of this binary number is maximum.Note: Do not assume any trailing zeroes in the start of the binary number i.e. "0101" is given
4 min read
C++ Program to Move all zeroes to end of array | Set-2 (Using single traversal)
Given an array of n numbers. The problem is to move all the 0's to the end of the array while maintaining the order of the other elements. Only single traversal of the array is required.Examples:Â Â Input : arr[] = {1, 2, 0, 0, 0, 3, 6} Output : 1 2 3 6 0 0 0 Input: arr[] = {0, 1, 9, 8, 4, 0, 0, 2, 7
2 min read
Check if a Binary String contains A pairs of 0s and B independent 0s or not
Given a binary string S and two positive integers A and B, the task is to check if the string consists of A independent pair of adjacent 0s and B independent number of 0s in the binary string or not. If found to be true, then print "Yes". Otherwise, print "No". Examples: Input: S = "10100", A = 1, B
9 min read
Longest subsequence possible that starts and ends with 1 and filled with 0 in the middle
Given a binary string s, the task is to find the length of the longest subsequence that can be divided into three substrings such that the first and third substrings are either empty or filled with 1 and the substring at the middle is either empty or filled with 0. Examples: Input: s = "1001" Output
7 min read
C++ Program to Find all rectangles filled with 0
We have one 2D array, filled with zeros and ones. We have to find the starting point and ending point of all rectangles filled with 0. It is given that rectangles are separated and do not touch each other however they can touch the boundary of the array.A rectangle might contain only one element. Ex
5 min read
C++ Program For Sorting An Array Of 0s, 1s and 2s
Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples: Input: {0, 1, 2, 0, 1, 2} Output: {0, 0, 1, 1, 2, 2} Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1} Output: {0, 0, 0,
6 min read
C++ Program For Decimal To Binary Conversion
Binary Numbers uses only 0 and 1 (base-2), while Decimal Number uses 0 to 9 (base-10). In this article, we will learn to implement a C++ program to convert Decimal numbers to Binary Numbers. The below diagram shows an example of converting the decimal number 17 to an equivalent binary number. Recomm
3 min read
C++ Program For Binary To Decimal Conversion
The binary number system uses only two digits 0 and 1 to represent an integer and the Decimal number system uses ten digits 0 to 9 to represent a number. In this article, we will discuss the program for Binary to Decimal conversion in C++. Algorithm to Convert Binary Numbers to DecimalInitialize a v
4 min read