- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Algorithm to add binary arrays in JavaScript

In this problem statement, our target is to add two binary arrays with the help of Javascript. So we will break down the problem step by step to ensure understanding.

## What is the Binary array ?

A binary array is an array that represents a binary number. In a binary number system we have only two possible digits, 0 and 1. The binary array will have the elements which can only be 0s and 1s. Every item in the array will represent a bit of the binary number.

For example we have a binary array [1, 0, 1, 0]. This array shows the binary number 1010. The leftmost bit 1 is the MSB (Most Significant Bit) and the rightmost bit 0 is the LSB (Left Significant Bit). Binary arrays are mostly used to represent and perform tasks on binary numbers in programming languages.

## Understanding the problem

The problem is to add two binary arrays. A binary array is an array which consists of only 0s and 1s, binary numbers. For example [1, 0, 1] this array also represents binary number 101. So we have to create an algorithm to do the binary addition on two binary arrays.

## Logic for the given problem

As we have to construct an algorithm. So this algorithm will take two parameters as input. These inputs will be the binary arrays. To add these binary array numbers we will start from the rightmost bit and move towards the leftmost bit. Then we will add the corresponding bits from the arrays with any carry from the previous addition. The input arrays array1 and array2 will show the binary numbers respectively. The resulting sum will be the output array of the addition of two binary arrays.

## Algorithm

**Step 1**: Start an empty array for storing the result array of sum of binary arrays.

**Step 2**: Check that the two input arrays have the same length or not. If they do not have the same length then we need to create a shorter array with leading zero to make their lengths equal. This step is essential because binary addition should be performed with the same number of digits.

**Step 3**: Initialize a carry variable and give the value as zero. The carry shows the value to be carried over to the next bit when the addition of two bits is 2 or greater.

**Step 4**: Traverse through the binary array from right to left.

**Step 5**: In every iteration we will perform the sum of current bits from both the arrays. Also calculate the current bit of the result by taking the modulo 2 of the sum.

**Step 6**: Calculate the new carry by taking the floor division of the sum divided by 2. Then we will insert the calculated bit at the beginning of the result array.

**Step 7**: Check that there is remaining carry. If there is then insert it at the beginning of the result array.

**Step 8**: We will have the final result array as the sum of the binary arrays.

### Example

//Function to add binary arrays function addBinaryArrays(array1, array2) { const result = []; const length = Math.max(array1.length, array2.length); let carry = 0; while (array1.length < length) { array1.unshift(0); } while (array2.length < length) { array2.unshift(0); } // Perform binary addition for (let i = length - 1; i >= 0; i--) { const bitSum = array1[i] + array2[i] + carry; const bit = bitSum % 2; carry = Math.floor(bitSum / 2); result.unshift(bit); } // Add the remaining carry, if any if (carry > 0) { result.unshift(carry); } return result; } //Define binary arrays const array1 = [1, 0, 1, 1]; const array2 = [1, 1, 1, 0]; const sum = addBinaryArrays(array1, array2); console.log(sum);

### Output

[ 1, 1, 0, 0, 1 ]

## Complexity

The time complexity for the algorithm is O(n) because the main loop iterates over the array from right to left and performs the constant time operations. Here n is the length of the longest input array. The space complexity is also O(n) because the result array stores the sum of the arrays.

## Conclusion

The function we have created adds two binary arrays in Javascript. It also checks that the arrays should have the same length and if they are not the same length then we have added padding with the shorter array with leading zeros. It performs binary addition bit by bit. The function has a time complexity and space complexity of O(n), which is efficient for adding binary arrays of any size.