Maximum bitwise OR one of any two Substrings of given Binary String
Last Updated :
28 Mar, 2023
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.
Input: str = “11010”
Output: “11111”
Explanation: On choosing the substring “11010” and “101” we get the OR value as “11111” which is the maximum value.
Approach: This can be solved using the following idea.
We can consider the whole string as one of the substring and the other substring should be chosen in such a way that the most significant 0th bit should be set as 1 so that the value get maximised.
Follow the steps mentioned below to solve the problem:
- Remove the leading 0s from the string str.
- Now traverse through the rest of the array and store that in another string (say str1).
- Initialize another string (say str2) to store the maximum possible bitwise OR.
- Find the most significant bit with value ‘0’ (say k) in str1.
- Also, keep on adding the ‘1’s in str2 till k is reached.
- If k is the end of the string, return str1 as the answer [because it has all the bits set].
- Otherwise, str1 will be right shifted that many times such that the most significant setbit is at the same position as k.
- Now find the OR of these two strings in the following way:
- Iterate from i = 0 to size of str1:
- If any bit between str1[i] and str1[k] is set, append ‘1’ to str2.
- Otherwise, append ‘0’.
- Increment k. If k reaches m, stop the iteration.
- Return str2 as the required answer,
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string findmax_ORvalue(string str)
{
int i, j, n = str.length();
string str2 = "" , str1 = "" ;
for (i = 0; i < n; i++) {
if (str[i] == '1' ) {
for (j = i; j < n; j++) {
str1 += str[j];
}
break ;
}
}
if (i == n) {
return "0" ;
}
int k, m = str1.size();
for (k = 0; k < m; k++) {
if (str1[k] == '0' ) {
break ;
}
str2 += str1[k];
}
if (k == m)
return str2;
for (i = 0; i < m and k < m; i++, k++) {
if (str1[i] == '1' or str1[k] == '1' )
str2 += '1' ;
else
str2 += '0' ;
}
return str2;
}
int main()
{
string str = "1110010" ;
cout << findmax_ORvalue(str);
return 0;
}
|
Java
import java.io.*;
class GFG {
static public String findmax_ORvalue(String str)
{
int i, j, n = str.length();
String str2 = "" , str1 = "" ;
for (i = 0 ; i < n; i++) {
if (str.charAt(i) == '1' ) {
for (j = i; j < n; j++) {
str1 += str.charAt(j);
}
break ;
}
}
if (i == n) {
return "0" ;
}
int k, m = str1.length();
for (k = 0 ; k < m; k++) {
if (str1.charAt(k) == '0' ) {
break ;
}
str2 += str1.charAt(k);
}
if (k == m)
return str2;
for (i = 0 ; i < m && k < m; i++, k++) {
if (str1.charAt(i) == '1'
|| str1.charAt(k) == '1' )
str2 += '1' ;
else
str2 += '0' ;
}
return str2;
}
public static void main(String[] args)
{
String str = "1110010" ;
System.out.println(findmax_ORvalue(str));
}
}
|
Python3
def findmax_ORvalue(s):
i, j, n = 0 , 0 , len (s)
str2, str1 = " ", " "
for i in range (n):
if s[i] = = '1' :
for j in range (i, n):
str1 + = s[j]
break
if i = = n:
return "0"
k, m = 0 , len (str1)
for k in range (m):
if str1[k] = = '0' :
break
str2 + = str1[k]
if k = = m:
return str2
for i in range ( 0 , m):
if (k > = m):
break
if str1[i] = = '1' or str1[k] = = '1' :
str2 + = '1'
else :
str2 + = '0'
k + = 1
return str2
s = "1110010"
print (findmax_ORvalue(s))
|
C#
using System;
public class GFG {
static public string findmax_ORvalue( string str)
{
int i, j, n = str.Length;
string str2 = "" , str1 = "" ;
for (i = 0; i < n; i++) {
if (str[i] == '1' ) {
for (j = i; j < n; j++) {
str1 += str[j];
}
break ;
}
}
if (i == n) {
return "0" ;
}
int k, m = str1.Length;
for (k = 0; k < m; k++) {
if (str1[k] == '0' ) {
break ;
}
str2 += str1[k];
}
if (k == m)
return str2;
for (i = 0; i < m && k < m; i++, k++) {
if (str1[i] == '1' || str1[k] == '1' )
str2 += '1' ;
else
str2 += '0' ;
}
return str2;
}
static public void Main()
{
string str = "1110010" ;
Console.WriteLine(findmax_ORvalue(str));
}
}
|
Javascript
const findmax_ORvalue = (str) => {
let i, j, n = str.length;
let str2 = "" , str1 = "" ;
for (i = 0; i < n; i++) {
if (str[i] == '1' ) {
for (j = i; j < n; j++) {
str1 += str[j];
}
break ;
}
}
if (i == n) {
return "0" ;
}
let k, m = str1.length;
for (k = 0; k < m; k++) {
if (str1[k] == '0 ') {
break;
}
str2 += str1[k];
}
if (k == m)
return str2;
// Loop to find the maximum OR value
for (i = 0; i < m && k < m; i++, k++) {
if (str1[i] == ' 1 ' || str1[k] == ' 1 ')
str2 += ' 1 ';
else
str2 += ' 0';
}
return str2;
}
let str = "1110010" ;
console.log(findmax_ORvalue(str));
|
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...