Bitwise OR of N binary strings
Last Updated :
14 Jun, 2021
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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function to return the bitwise OR of
// all the binary strings
string strBitwiseOR(string* arr, int n)
{
string res;
int max_size = INT_MIN;
// Get max size and reverse each string
// Since we have to perform OR operation
// on bits from right to left
// Reversing the string will make it easier
// to perform operation from left to right
for (int i = 0; i < n; i++) {
max_size = max(max_size, (int)arr[i].size());
reverse(arr[i].begin(), arr[i].end());
}
for (int i = 0; i < n; i++) {
// Add 0s to the end of strings
// if needed
string s;
for (int j = 0; j < max_size - arr[i].size(); j++)
s += '0';
arr[i] = arr[i] + s;
}
// Perform OR operation on each bit
for (int i = 0; i < max_size; i++) {
int curr_bit = 0;
for (int j = 0; j < n; j++)
curr_bit = curr_bit | (arr[j][i] - '0');
res += (curr_bit + '0');
}
// Reverse the resultant string
// to get the final string
reverse(res.begin(), res.end());
// Return the final string
return res;
}
// Driver code
int main()
{
string arr[] = { "10", "11", "1000001" };
int n = sizeof(arr) / sizeof(arr[0]);
cout << strBitwiseOR(arr, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the bitwise OR of
// all the binary strings
static String strBitwiseOR(String[] arr, int n)
{
String res="";
int max_size = Integer.MIN_VALUE;
// Get max size and reverse each string
// Since we have to perform OR operation
// on bits from right to left
// Reversing the string will make it easier
// to perform operation from left to right
for (int i = 0; i < n; i++)
{
max_size = Math.max(max_size, (int)arr[i].length());
arr[i] = reverse(arr[i]);
}
for (int i = 0; i < n; i++)
{
// Add 0s to the end of strings
// if needed
String s="";
for (int j = 0; j < max_size - arr[i].length(); j++)
s += '0';
arr[i] = arr[i] + s;
}
// Perform OR operation on each bit
for (int i = 0; i < max_size; i++)
{
int curr_bit = 0;
for (int j = 0; j < n; j++)
curr_bit = curr_bit | (arr[j].charAt(i) - '0');
res += (char)(curr_bit + '0');
}
// Reverse the resultant string
// to get the final string
res = reverse(res);
// Return the final string
return res;
}
static String reverse(String input)
{
char[] temparray = input.toCharArray();
int left, right = 0;
right = temparray.length - 1;
for (left = 0; left < right; left++, right--)
{
// Swap values of left and right
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.valueOf(temparray);
}
// Driver code
public static void main(String[] args)
{
String arr[] = { "10", "11", "1000001" };
int n = arr.length;
System.out.println(strBitwiseOR(arr, n));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function to return the bitwise OR of
# all the binary strings
def strBitwiseOR(arr, n):
res=""
max_size = -(2**32)
# Get max size and reverse each string
# Since we have to perform OR operation
# on bits from right to left
# Reversing the string will make it easier
# to perform operation from left to right
for i in range(n):
max_size = max(max_size, len(arr[i]))
arr[i] = arr[i][::-1]
for i in range(n):
# Add 0s to the end of strings
# if needed
s = ""
for j in range(max_size - len(arr[i])):
s += '0'
arr[i] = arr[i] + s
# Perform OR operation on each bit
for i in range(max_size):
curr_bit = 0
for j in range(n):
curr_bit = curr_bit | ord(arr[j][i])
res += chr(curr_bit)
# Reverse the resultant string
# to get the final string
res=res[::-1]
# Return the final string
return res
# Driver code
arr = ["10", "11", "1000001"]
n = len(arr)
print(strBitwiseOR(arr, n))
# This code is contributed by shubhamsingh10
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the bitwise OR of
// all the binary strings
static String strBitwiseOR(String[] arr, int n)
{
String res="";
int max_size = int.MinValue;
// Get max size and reverse each string
// Since we have to perform OR operation
// on bits from right to left
// Reversing the string will make it easier
// to perform operation from left to right
for (int i = 0; i < n; i++)
{
max_size = Math.Max(max_size, (int)arr[i].Length);
arr[i] = reverse(arr[i]);
}
for (int i = 0; i < n; i++)
{
// Add 0s to the end of strings
// if needed
String s="";
for (int j = 0; j < max_size - arr[i].Length; j++)
s += '0';
arr[i] = arr[i] + s;
}
// Perform OR operation on each bit
for (int i = 0; i < max_size; i++)
{
int curr_bit = 0;
for (int j = 0; j < n; j++)
curr_bit = curr_bit | (arr[j][i] - '0');
res += (char)(curr_bit + '0');
}
// Reverse the resultant string
// to get the final string
res = reverse(res);
// Return the final string
return res;
}
static String reverse(String input)
{
char[] temparray = input.ToCharArray();
int left, right = 0;
right = temparray.Length - 1;
for (left = 0; left < right; left++, right--)
{
// Swap values of left and right
char temp = temparray[left];
temparray[left] = temparray[right];
temparray[right] = temp;
}
return String.Join("",temparray);
}
// Driver code
public static void Main(String[] args)
{
String []arr = { "10", "11", "1000001" };
int n = arr.Length;
Console.WriteLine(strBitwiseOR(arr, n));
}
}
/* This code contributed by PrinciRaj1992 */
JavaScript
<script>
// JavaScript implementation of the approach
// Function to return the bitwise OR of
// all the binary strings
function strBitwiseOR(arr, n)
{
var res = "";
var max_size = -1000000000;
// Get max size and reverse each string
// Since we have to perform OR operation
// on bits from right to left
// Reversing the string will make it easier
// to perform operation from left to right
for (var i = 0; i < n; i++) {
max_size = Math.max(max_size, arr[i].length);
arr[i] = arr[i].split('').reverse().join('');
}
for (var i = 0; i < n; i++) {
// Add 0s to the end of strings
// if needed
var s = "";
for (var j = 0; j < max_size - arr[i].length; j++)
s += '0';
arr[i] = arr[i] + s;
}
// Perform OR operation on each bit
for (var i = 0; i < max_size; i++) {
var curr_bit = 0;
for (var j = 0; j < n; j++)
curr_bit = curr_bit | (arr[j][i].charCodeAt(0) -
'0'.charCodeAt(0));
res += String.fromCharCode(curr_bit + '0'.charCodeAt(0));
}
// Reverse the resultant string
// to get the final string
res = res.split('').reverse().join('');
// Return the final string
return res;
}
// Driver code
var arr = ["10", "11", "1000001"];
var n = arr.length;
document.write( strBitwiseOR(arr, n));
</script>
Similar Reads
Bitwise AND of N binary strings Given an array arr[] of binary strings, the task is to calculate the bitwise AND of all of these strings and print the resultant string. Examples: Input: arr[] = {"101", "110110", "111"}Output: 000100Explanation: (000101) & (110110) & (000111) = 000100 Input: arr[] = {"110010101", "111101001
15+ min read
Check for Binary String Given a string s, the task is to check if it is a binary string or not. A binary string is a string which only contains the characters '0' and '1'.Examples:Input: s = "01010101010"Output: trueInput: s = "geeks101"Output: false Approach:The idea is to iterate over all the characters of the string and
3 min read
Generate all the binary strings of N bits Given a positive integer number N. The task is to generate all the binary strings of N bits. These binary strings should be in ascending order.Examples: Input: 2Output:0 00 11 01 1Input: 3Output:0 0 00 0 10 1 00 1 11 0 01 0 11 1 01 1 1Approach: The idea is to try every permutation. For every positio
8 min read
Count valid Bitwise operation combinations for Binary Strings Given an odd integer N (N >= 3), a binary string S of length N and another string O of length (N-1)/2. Here, each character in string O can be one of the following: '&', '|' or '^' representing Bitwise operations AND, OR, and XOR respectively, the task is to find the number of possible combin
8 min read
XOR of all substrings of a given Binary String Given a binary string str of size N, the task is to calculate the bitwise XOR of all substrings of str. Examples: Input: str = "11"Output: 11Explanation: The substrings of "11" are: 1, 1, and 11.Their XOR = 1 â 1 â 11 = 11 Input: str = "110"Output: 111Explanation: The substrings of 110 are: 1, 1, 0,
6 min read
Maximum bitwise OR one of any two Substrings of given Binary String Given a binary string str, the task is to find the maximum possible OR value of any two substrings of the binary string str. Examples: Input: str = "1110010"Output: "1111110"Explanation: On choosing the substring "1110010" and "11100" we get the OR value as "1111110" which is the maximum value. Inpu
8 min read