Bitwise OR Triplets in a Binary String
Last Updated :
13 Dec, 2023
Given a binary string S of length N, where S[i] represents an integer value. the task is to count the number of ways to choose three different indices ( 0 <= i < j < k < N) such that the bitwise OR of the adjacent chosen element should be one (i.e, S[i] OR S[j] == 1 AND S[j] OR S[k] == 1).
Examples:
Input: S = "00110"
Output: 4
Explanation: Below are the different possible three indices:
- indices {0, 2, 4} forms "010" such that S[0] | S[2] == 1 && S[2] | S[4] == 1
- {0, 3, 4} forms "010"such that S[0] | S[3] == 1 && S[3] | S[4] == 1
- {1, 2, 4} forms "010" such that S[1] | S[2] == 1 && S[2] | S[4] == 1
- {1, 3, 4} forms "010" such that S[1] | S[3] == 1 && S[3] | S[4] == 1
Thus, there are 4 total ways.
Input: S = "11100"
Output: 0
Bitwise OR Triplets in a Binary String using PrefixSum Technique:
The idea is of Keep track of the number of zeros and ones on the left and right at any indices. At any index i, if the ith digit is zero then for this index we can select (number of ones on the left * number of ones on the right). Similarly, if the digit is 1, check for the 0s to the left and right. Keep adding the number of ways for every index in the result and finally return it.
Step-by-step approach:
- Calculate the totalZero and totalOne in the binary string.
- Initialize currZero and currOne to 0 to keep track of the number of zeros and ones until the current index i.
- Initialize a variable result to keep track of the answer.
- Iterate through the string:
- If the current digit is '0':
- Add the value of (number of ones to the left * number of ones to the right) to result.
- Increment currZero by 1.
- If the current digit is '1':
- Add the value of (number of zeros to the left * number of zeros to the right) to result.
- Increment currOne by 1.
- Finally, return result as the 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 number of ways
long long countWays(string s)
{
int n = s.size();
// Initialize a varaible totalZero and
// totalOne to 0, this will keep track
// of total number of zeros and ones
// in the binary staring respectively.
int totalZero = 0, totalOne = 0;
// Initilise varaible currZero and
// currOne to 0, this will keep track
// of number of zeros and ones till
// ith index.
int currZero = 0, currOne = 0;
// Iterate over the string
for (int i = 0; i < n; i++) {
// If the curr digit is zero
// increment the totalZero by 1
if (s[i] == '0')
totalZero++;
// Otherwise, increment the
// totalOne by 1
else
totalOne++;
}
// Initilise a varible result, to
// keep track of the answer
long long result = 0;
// Iterate over the string
for (int i = 0; i < n; i++) {
// If the current digit is 0
if (s[i] == '0') {
// Add the value of (number of
// ones to the left * number
// of ones to the right)
result += (currOne * (totalOne - currOne));
// Increment the count of
// currZero by 1
currZero++;
}
// Otherwise, Add the value of
// (number of zeros to the left
// * number of zeros to the right)
else {
result += (currZero * (totalZero - currZero));
// Increment the count of
// currOnes by 1
currOne++;
}
}
// Finally, return result.
return result;
}
// Drivers code
int main()
{
// First test case
string s = "00110";
// Function Call
cout << countWays(s) << endl;
return 0;
}
Java
import java.util.*;
public class Main {
// Function to find the number of ways
static long countWays(String s) {
int n = s.length();
// Initialize variables totalZero and totalOne to 0
int totalZero = 0, totalOne = 0;
// Initialize variables currZero and currOne to 0
int currZero = 0, currOne = 0;
// Iterate over the string
for (int i = 0; i < n; i++) {
// If the current digit is '0', increment totalZero
if (s.charAt(i) == '0')
totalZero++;
// Otherwise, increment totalOne
else
totalOne++;
}
// Initialize the result variable to keep track of the answer
long result = 0;
// Iterate over the string again
for (int i = 0; i < n; i++) {
// If the current digit is '0'
if (s.charAt(i) == '0') {
// Add the value of (number of ones to the left * number of ones to the right)
result += (currOne * (totalOne - currOne));
// Increment the count of currZero by 1
currZero++;
} else {
// Add the value of (number of zeros to the left * number of zeros to the right)
result += (currZero * (totalZero - currZero));
// Increment the count of currOne by 1
currOne++;
}
}
// Finally, return the result
return result;
}
public static void main(String[] args) {
// Test case
String s = "00110";
// Function Call
System.out.println(countWays(s));
}
}
Python3
# Python Implementation
def countWays(s):
n = len(s)
totalZero = 0
totalOne = 0
currZero = 0
currOne = 0
for i in range(n):
if s[i] == '0':
totalZero += 1
else:
totalOne += 1
result = 0
for i in range(n):
if s[i] == '0':
result += (currOne * (totalOne - currOne))
currZero += 1
else:
result += (currZero * (totalZero - currZero))
currOne += 1
return result
# Test case
s = "00110"
print(countWays(s))
# This code is contributed by Tapesh(tapeshdua420)
C#
using System;
class Program
{
// Function to find the number of ways
static long CountWays(string s)
{
int n = s.Length;
// Initialize variables to track total number of zeros and ones
int totalZero = 0, totalOne = 0;
// Initialize variables to track number of zeros and ones till ith index
int currZero = 0, currOne = 0;
// Iterate over the string
foreach (char c in s)
{
if (c == '0')
totalZero++;
else
totalOne++;
}
// Initialize a variable 'result' to keep track of the answer
long result = 0;
// Iterate over the string
for (int i = 0; i < n; i++)
{
// If the current digit is 0
if (s[i] == '0')
{
// Add the value of (number of ones to the left * number of ones to the right)
result += (currOne * (totalOne - currOne));
currZero++;
}
else
{
// Add the value of (number of zeros to the left * number of zeros to the right)
result += (currZero * (totalZero - currZero));
currOne++;
}
}
// Finally, return result
return result;
}
// Driver code
static void Main()
{
// First test case
string s = "00110";
// Function call
Console.WriteLine(CountWays(s));
}
}
JavaScript
// JavaScript code to implement the approach
// Function to find the number of ways
function countWays(s) {
const n = s.length;
// Initialize variables totalZero and totalOne to 0
let totalZero = 0,
totalOne = 0;
// Initialize variables currZero and currOne to 0
let currZero = 0,
currOne = 0;
// Iterate over the string
for (let i = 0; i < n; i++) {
// Increment totalZero or totalOne based on the digit
if (s[i] === '0')
totalZero++;
else
totalOne++;
}
// Initialize a variable result to keep track of the answer
let result = 0;
// Iterate over the string again
for (let i = 0; i < n; i++) {
if (s[i] === '0') {
// Add the value of (number of ones to the
// left * number of ones to the right)
result += (currOne * (totalOne - currOne));
currZero++;
} else {
// Add the value of (number of zeros to the
// left * number of zeros to the right)
result += (currZero * (totalZero - currZero));
currOne++;
}
}
// Finally, return result
return result;
}
// First test case
const s = "00110";
// Function Call
console.log(countWays(s));
Time Complexity: O(N)
Auxiliary Space: O(1)
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
Bitwise OR of N binary strings 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 = 1111Input: arr[] = {"10", "11", "1000001"} Output: 1000011 Approach: We can do th
7 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
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
What is Binary String? A binary string is a string that only has two characters, usually the numbers 0 and 1, and it represents a series of binary digits. Binary String Variables:In computer programming, binary string variables are used to store binary data, which is data that is represented in a binary (base-2) format, r
9 min read