GeeksforGeeks App
Open App
Browser
Continue

# 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.

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 ``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`

Output

`1111110`

Time Complexity: O(N), where N is the length of the string.
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up