Open In App

Bitwise OR of N binary strings

Last Updated : 14 Jun, 2021
Comments
Improve
Suggest changes
1 Like
Like
Report

Given an array arr[] of binary strings, the task is to calculate the bitwise OR of all of these strings and print the resultant string.
Examples: 
 

Input: arr[] = {"100", "1001", "0011"} 
Output 1111 
0100 OR 1001 OR 0011 = 1111
Input: arr[] = {"10", "11", "1000001"} 
Output: 1000011 
 


 


Approach: We can do this by first finding the maximum sized string. We need this since we have to add 0s at the front of the strings whose lengths are less than max size. Then apply OR operation on each bit. 
For example, if strings are "100", "001" and "1111". Here max size is 4, so we have to add 1 zero on first and second string to make their length 4 and then the OR operation can be performed on each of the bits of the numbers resulting in "0100" OR "0001" OR "1111" = "1111".
Below is the implementation of the above approach:
 

C++
Java Python3 C# JavaScript

Output: 
1000011

 

Article Tags :
Practice Tags :

Similar Reads