# Set both elements of a binary array to 0 in single line

Given an binary array of size two having alteast 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 basically three combinations of array elements as per problem constraints –

1. arr[0] = 0 and arr[1] = 1

2. arr[0] = 1 and arr[1] = 0

3. arr[0] = 0 and arr[1] = 0

There are many ways to solve given problem. We will discuss few of them –

#### Method 1: Using assignment operator twice

We can use any of below single line expressions to convert both elements of 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];

Output:

0 0
0 0
0 0

#### Method 2: Using negation (logical NOT) operator

We can make use of negation operator along with assignment operator to convert both elements of given array to 0 in single line –

• arr[!arr[0]] = arr[arr[0]]; or

• arr[arr[1]] = arr[!arr[1]]; or

• arr[!arr[0]] = arr[!arr[1]];

Output:

0 0
0 0
0 0

#### Method 3: Using only assignment operator

We can directly use assignment operator to set both elements of 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;

Output:

0 0
0 0
0 0

(2 votes, average: 5.00 out of 5)