Given two Binary Strings, we have to return their sum in binary form.
Approach: We will start from the last of both strings and add it according to binary addition, if we get any carry we will add it to the next digit.
Input:
11 + 11
Output:
110
OutputEnter binary number 1: 1001
Enter binary number 2: 1001
1001 + 1001 = 10010
The time complexity is O(n), where n is the length of the longer binary string.
The auxiliary space is O(n), where n is the length of the longer binary string.
Method: Using a while loop
OutputSum of two binary numbers: 100000
The time complexity is O(log n)
The auxiliary space is also O(log n)
Method 3: Bitwise Addition
This approach uses the concept of binary addition to add two binary strings a and b. The steps are as follows:
- Calculate the lengths of both strings a and b using the strlen() function.
- Initialize a variable carry to 0, and two variables i and j to the lengths of the strings minus one.
- Initialize a variable maxLen to the maximum length of the two strings, and allocate memory for a result string of size maxLen + 1 using the malloc() function. Set the last element of the result string to the null character \0.
- While either i or j is greater than or equal to zero, do the following:
- Initialize a variable sum to carry.
- If i is greater than or equal to zero, add the numeric value of the i-th character of a (converted from ASCII to integer using the - '0' expression) to sum and decrement i.
- If j is greater than or equal to zero, add the numeric value of the j-th character of b (converted from ASCII to integer using the - '0' expression) to sum and decrement j.
- Divide sum by 2 using the right shift operator >>, and store the result in carry.
- Set the maxLen-th character of the result string (converted to ASCII using the +'0' expression) to the least significant bit of sum (obtained using the bitwise AND operator & with 1), and decrement maxLen.
- If there is a final carry after the previous loop, do the following:
- Reallocate memory for the result string to accommodate one extra character using the realloc() function.
- Shift the result string to the right by one using the memmove() function.
- Set the leftmost character of the result string to '1'.
- Return the result string.
- Free the memory allocated for the result string using the free() function.
Below is the implementation of the above approach:
OutputSum of 101 and 1101 is 10010
The time complexity of the addBinary function is O(n), where n is the length of the longer input string.
The auxiliary space complexity of the function is also O(n), where n is the length of the longer input string.
Approach: Binary Addition using Bitwise Operations
This approach uses bitwise operators to perform binary addition. It is a more efficient approach compared to the traditional method of converting the binary strings to decimal and then adding them.
Steps:
- Define a constant MAX_LEN to store the maximum length of the binary strings.
- Declare a function addBinary that takes two binary strings as input and returns the resultant binary string.
- Initialize a static character array result of size MAX_LEN to store the resultant binary string.
- Determine the lengths of the binary strings a and b.
- Initialize the carry to 0 and index variables i, j, and k to the lengths of the binary strings a, b, and result minus 1, respectively.
- Loop through the binary strings from right to left until all digits have been added and the carry is 0.
- Compute the sum of the digits and the carry, and add the sum modulo 2 to the result array at index k.
- Update the carry to the sum divided by 2.
- Reverse the result array to get the correct order of digits.
- In the main function, prompt the user to input the binary strings a and b.
- Call the addBinary function to add the binary strings a and b.
- Display the sum of the binary strings to the user.
OutputThe sum of 11 and 11 is 110
Time Complexity: O(n), where n is the length of the binary strings.
Auxiliary Space: O(n), where n is the length of the binary strings.