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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the maximum profit
string findmax_ORvalue(string str)
{
// Initialize the variable n as
// length of the string
int i, j, n = str.length();
string str2 = "", str1 = "";
// Remove the leading zeros from the
// string str traveling through the
// string until first '1' occurs
for (i = 0; i < n; i++) {
// Add all the characters
// remaining from i to n-1
if (str[i] == '1') {
for (j = i; j < n; j++) {
str1 += str[j];
}
break;
}
}
// If i == n it means all are 0's in
// the string so return 0
if (i == n) {
return "0";
}
int k, m = str1.size();
// Traverse through the string and find
// the index where the first most
// significant zero occurs
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 and k < m; i++, k++) {
if (str1[i] == '1' or str1[k] == '1')
str2 += '1';
else
str2 += '0';
}
// Return the maximum OR value
return str2;
}
// Driver function
int main()
{
string str = "1110010";
// Function call
cout << findmax_ORvalue(str);
return 0;
}
Java
// Java implementation
import java.io.*;
class GFG {
// Function to find the maximum profit
static public String findmax_ORvalue(String str)
{
// Initialize the variable n as
// length of the string
int i, j, n = str.length();
String str2 = "", str1 = "";
// Remove the leading zeros from the
// string str traveling through the
// string until first '1' occurs
for (i = 0; i < n; i++) {
// Add all the characters
// remaining from i to n-1
if (str.charAt(i) == '1') {
for (j = i; j < n; j++) {
str1 += str.charAt(j);
}
break;
}
}
// If i == n it means all are 0's in
// the string so return 0
if (i == n) {
return "0";
}
int k, m = str1.length();
// Traverse through the string and find
// the index where the first most
// significant zero occurs
for (k = 0; k < m; k++) {
if (str1.charAt(k) == '0') {
break;
}
str2 += str1.charAt(k);
}
if (k == m)
return str2;
// Loop to find the maximum OR value
for (i = 0; i < m && k < m; i++, k++) {
if (str1.charAt(i) == '1'
|| str1.charAt(k) == '1')
str2 += '1';
else
str2 += '0';
}
// Return the maximum OR value
return str2;
}
public static void main(String[] args)
{
String str = "1110010";
// Function call
System.out.println(findmax_ORvalue(str));
}
}
// This code is contributed by lokesh
Python3
# Python code to implement the approach
# Function to find the maximum profit
def findmax_ORvalue(s):
# Initialize the variable n as
# length of the string
i, j, n = 0, 0, len(s)
str2, str1 = "", ""
# Remove the leading zeros from the
# string str traveling through the
# string until first '1' occurs
for i in range(n):
# Add all the characters
# remaining from i to n-1
if s[i] == '1':
for j in range(i, n):
str1 += s[j]
break
# If i == n it means all are 0's in
# the string so return 0
if i == n:
return "0"
k, m = 0, len(str1)
# Traverse through the string and find
# the index where the first most
# significant zero occurs
for k in range(m):
if str1[k] == '0':
break
str2 += str1[k]
if k == m:
return str2
# Loop to find the maximum OR value
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 the maximum OR value
return str2
# Driver function
s = "1110010"
# Function call
print(findmax_ORvalue(s))
# This code is contributed by Tapesh(tapeshdua420)
C#
// C# implementation
using System;
public class GFG {
// Function to find the maximum profit
static public string findmax_ORvalue(string str)
{
// Initialize the variable n as
// length of the string
int i, j, n = str.Length;
string str2 = "", str1 = "";
// Remove the leading zeros from the
// string str traveling through the
// string until first '1' occurs
for (i = 0; i < n; i++) {
// Add all the characters
// remaining from i to n-1
if (str[i] == '1') {
for (j = i; j < n; j++) {
str1 += str[j];
}
break;
}
}
// If i == n it means all are 0's in
// the string so return 0
if (i == n) {
return "0";
}
int k, m = str1.Length;
// Traverse through the string and find
// the index where the first most
// significant zero occurs
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 the maximum OR value
return str2;
}
static public void Main()
{
string str = "1110010";
// Function call
Console.WriteLine(findmax_ORvalue(str));
}
}
// This code is contributed by ksam24000
JavaScript
// JavaScript code to implement the approach
// Function to find the maximum profit
const findmax_ORvalue = (str) => {
// Initialize the variable n as
// length of the string
let i, j, n = str.length;
let str2 = "", str1 = "";
// Remove the leading zeros from the
// string str traveling through the
// string until first '1' occurs
for (i = 0; i < n; i++) {
// Add all the characters
// remaining from i to n-1
if (str[i] == '1') {
for (j = i; j < n; j++) {
str1 += str[j];
}
break;
}
}
// If i == n it means all are 0's in
// the string so return 0
if (i == n) {
return "0";
}
let k, m = str1.length;
// Traverse through the string and find
// the index where the first most
// significant zero occurs
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 the maximum OR value
return str2;
}
// Driver function
let str = "1110010";
// Function call
console.log(findmax_ORvalue(str));
// This code is contributed by rakeshsahni
Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)
Similar Reads
Count of setbits in bitwise OR of all K length substrings of given Binary String Given a binary string str of length N, the task is to find the number of setbits in the bitwise OR of all the K length substrings of string str. Examples: Input: N = 4, K = 3, str = "1111"Output: 3Explanation: All 3-sized substrings of S are:"111" and "111". The OR of these strings is "111". Therefo
8 min read
Maximum number of set bits count in a K-size substring of a Binary String Given a binary string S of size N and an integer K. The task is to find the maximum number of set bit appears in a substring of size K. Examples: Input: S = "100111010", K = 3 Output: 3 Explanation: The substring "111" contains 3 set bits. Input:S = "0000000", K = 4 Output: 0 Explanation: S doesn't
10 min read
Maximum count of unique index 10 or 01 substrings in given Binary string Given a binary string str of length N, the task is to count the maximum number of adjacent pairs of form "01" or "10" that can be formed from the given binary string when one character can be considered for only one pair. Note: Adjacent pair means pair formed using adjacent characters. Examples: Inp
5 min read
Maximize value of Binary String in K steps by performing XOR of Substrings Given a binary string S of size N, the task is to find the maximum possible value in exactly K step, where in each step, perform XOR of two substrings of S (possibly intersecting, possibly the same, possibly non-intersecting). Examples: Input: string S ="1010010", K = 2Output: "1111110"Explanation:I
15 min read
Maximum difference of zeros and ones in binary string Given a binary string of 0s and 1s. The task is to find the length of the substring which is having a maximum difference between the number of 0s and the number of 1s (number of 0s - number of 1s). In case of all 1s print -1. Examples: Input : S = "11000010001"Output : 6From index 2 to index 9, ther
15 min read