Count of triplets in Binary String such that Bitwise AND of S[i], S[j] and S[j], S[k] are same
Last Updated :
05 May, 2022
Given a binary string S of length N, consisting of 0s and 1s. The task is to count the number of triplet (i, j, k) such that S[i] & S[j] = S[j] & S[k], where 0 ≤ i < j < k < N and & denotes bitwise AND operator.
Examples:
Input: N = 4, S = “0010”
Output: 4
Explanation: Following are 4 triplets which satisfy the condition S[i] & S[j] = S[j] & S[k]
(0, 1, 2) because 0 & 0 = 0 & 1,
(0, 1, 3) because 0 & 0 = 0 & 0,
(0, 2, 3) because 0 & 1 = 1 & 0,
(1, 2, 3) because 0 & 1 = 1 & 0.
Input: N = 5, S = “00101”
Output: 8
Explanation: 8 triplets satisfying the above condition are :
(0, 1, 2) because 0 & 0 = 0 & 1.
(0, 1, 3) because 0 & 0 = 0 & 0.
(0, 1, 4) because 0 & 0 = 0 & 1.
(0, 2, 3) because 0 & 1 = 1 & 0.
(1, 2, 3) because 0 & 1 = 1 & 0.
(0, 3, 4) because 0 & 0 = 0 & 1.
(1, 3, 4) because 0 & 0 = 0 & 1.
(2, 3, 4) because 1 & 0 = 0 & 1.
Naive Approach: The easiest way to solve the problem is to:
Generate all possible triplets and check if S[i] & S[j] = S[j] & S[k].
Follow the below steps to solve this problem:
- Declare and initialize a variable ans to 0 to store the total count.
- Now, there is a need of three nested loops to iterate over all the triplets.
- The first loop iterates over the index i, the second over index j starting from i+1, and the third over index k starting from j+1.
- Now, check the condition S[i] & S[j] = S[j] & S[k] is true or not. If true, the increment the ans by 1.
- Return the total count of triplets.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int n, string& s)
{
int ans = 0;
for ( int i = 0; i < n; ++i) {
for ( int j = i + 1; j < n; ++j) {
for ( int k = j + 1; k < n; ++k) {
int x = s[i] - '0' ;
int y = s[j] - '0' ,
z = s[k] - '0' ;
if ((x & y) == (y & z)) {
ans++;
}
}
}
}
return ans;
}
int main()
{
int N = 4;
string S = "0010" ;
cout << countTriplets(N, S);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int countTriplets( int n, String s)
{
int ans = 0 ;
for ( int i = 0 ; i < n; ++i) {
for ( int j = i + 1 ; j < n; ++j) {
for ( int k = j + 1 ; k < n; ++k) {
int x = s.charAt(i);
int y = s.charAt(j), z = s.charAt(k);
if ((x & y) == (y & z)) {
ans++;
}
}
}
}
return ans;
}
public static void main(String[] args)
{
int N = 4 ;
String S = "0010" ;
System.out.print(countTriplets(N, S));
}
}
|
Python3
def countTriplets(n, s):
ans = 0
for i in range (n):
for j in range (i + 1 , n):
for k in range (j + 1 , n):
x = ord (S[i]) - ord ( "0" )
y = ord (S[j]) - ord ( "0" )
z = ord (S[k]) - ord ( "0" )
if (x & y) = = (y & z):
ans + = 1
return ans
N = 4
S = "0010"
print (countTriplets(N, S))
|
C#
using System;
public class GFG{
static int countTriplets( int n, string s)
{
int ans = 0;
for ( int i = 0; i < n; ++i) {
for ( int j = i + 1; j < n; ++j) {
for ( int k = j + 1; k < n; ++k) {
int x = s[i];
int y = s[j], z = s[k];
if ((x & y) == (y & z)) {
ans++;
}
}
}
}
return ans;
}
static public void Main ()
{
int N = 4;
string S = "0010" ;
Console.Write(countTriplets(N, S));
}
}
|
Javascript
<script>
const countTriplets = (n, s) => {
let ans = 0;
for (let i = 0; i < n; ++i) {
for (let j = i + 1; j < n; ++j) {
for (let k = j + 1; k < n; ++k) {
let x = s.charCodeAt(i) - '0' .charCodeAt(0);
let y = s.charCodeAt(j) - '0' .charCodeAt(0),
z = s.charCodeAt(k) - '0' .charCodeAt(0);
if ((x & y) == (y & z)) {
ans++;
}
}
}
}
return ans;
}
let N = 4;
let S = "0010" ;
document.write(countTriplets(N, S));
</script>
|
Time Complexity: O(N3)
Auxiliary Space: O(1)
Efficient Approach: The idea to solve the problem efficiently is based on the following observations:
Observations:
- Since S[i] ∈ {0, 1}, there can be only 8 possible distinct triplets. Let’s analyze how many of these actually satisfy the condition S[i] & S[j] == S[j] & S[k].
S[i] |
S[j] |
S[k] |
S[i]&S[j]==S[j]&S[k] |
0 |
0 |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
0 |
0 |
1 |
1 |
1 |
1 |
- After analyzing the truth table, observe that whenever S[j] == ‘0’, the condition is always satisfied. Also, when S[j] == ‘1’, then both S[i] and S[k] should be the same.
- Thus, iterate over all the S[j] values, and depending on the value of S[j], simply count the number of triplets.
- If S[j] == ‘0’, then S[i] and S[k] can be anything, So total possible ways of choosing any i and k is j * (N – j – 1).
- Otherwise, if S[j] == ‘1’, then S[i] and S[k] should be same. So only 0s can make pair with 0s and 1s with 1s. Say there was x1 0s in prefix and x2 0s in suffix and y1 and y2 1s in prefix and suffix. Then total possible pairs are x1*x2 + y1*y2
Follow the below steps to solve this problem:
- Declare a variable (say ans) to store the count of triplets and initialize it to 0.
- Create a prefix array pre and a suffix array suf. These two arrays store the number of zeros in the prefix and suffix of the array.
- For building prefix array, iterate from 0 to N – 1:
- If S[i] == ‘0’, then pre[i] = pre[i – 1] + 1.
- If S[i]== ‘1’, then pre[i] = pre[i – 1].
- For building suffix array, iterate from N – 1 to 0:
- If S[i] == ‘0’, then suf[i] = suf[i + 1] + 1.
- If S[i] == ‘1’, then suf[i] = suf[i + 1].
- Now iterate again, from 1 to N – 2 (we are iterating for S[j]):
- If S[j] == ‘0’, then add j * (N – j – 1) to ans variable as per the observation.
- If S[j] == ‘1’, then add ‘pre[j – 1]’ * ‘suf[j + 1]’ + (j – ‘pre[j – 1]’) * (N – j – 1 – ‘suf[j + 1]’) to ans as per the above observation.
- Now return the ans variable as that contains the final count of triplets.
Below is the implementation of the above approach :
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int n, string& s)
{
int ans = 0;
vector< int > pre(n), suf(n);
for ( int i = 0; i < n; ++i) {
pre[i] = (i == 0 ? 0 : pre[i - 1])
+ (s[i] == '0' );
}
for ( int i = n - 1; i >= 0; --i) {
suf[i]
= (i == n - 1 ? 0 : suf[i + 1])
+ (s[i] == '0' );
}
for ( int j = 1; j < n - 1; ++j) {
if (s[j] == '0' ) {
ans += j * (n - j - 1);
}
else {
ans += pre[j - 1] * suf[j + 1]
+ (j - pre[j - 1])
* (n - j - 1 - suf[j + 1]);
}
}
return ans;
}
int main()
{
int N = 4;
string S = "0010" ;
cout << countTriplets(N, S);
return 0;
}
|
Java
public class GFG {
static int countTriplets( int n, String s)
{
int ans = 0 ;
int [] pre = new int [n];
int [] suf = new int [n];
for ( int i = 0 ; i < n; ++i) {
pre[i] = (i == 0 ? 0 : pre[i - 1 ])
+ ( '1' - s.charAt(i));
}
for ( int i = n - 1 ; i >= 0 ; --i) {
suf[i] = (i == n - 1 ? 0 : suf[i + 1 ])
+ ( '1' - s.charAt(i));
}
for ( int j = 1 ; j < n - 1 ; ++j) {
if (s.charAt(j) == '0' ) {
ans += j * (n - j - 1 );
}
else {
ans += pre[j - 1 ] * suf[j + 1 ]
+ (j - pre[j - 1 ])
* (n - j - 1 - suf[j + 1 ]);
}
}
return ans;
}
public static void main(String[] args)
{
int N = 4 ;
String S = "0010" ;
System.out.println(countTriplets(N, S));
}
}
|
Python3
def countTriplets(n, s):
ans = 0
pre = [ 0 ] * n
suf = [ 0 ] * n
for i in range ( 0 ,n):
pre[i] = ( 0 if i = = 0 else pre[i - 1 ]) + (s[i] = = '0' )
for i in range (n - 1 , - 1 , - 1 ):
suf[i] = ( 0 if i = = n - 1 else suf[i + 1 ]) + (s[i] = = '0' )
for j in range ( 1 ,n):
if (s[j] = = '0' ):
ans + = j * (n - j - 1 )
else :
ans = ans + pre[j - 1 ] * suf[j + 1 ] + (j - pre[j - 1 ]) * (n - j - 1 - suf[j + 1 ])
return ans
N = 4
S = "0010"
print (countTriplets(N, S))
|
C#
using System;
class GFG
{
static int countTriplets( int n, string s)
{
int ans = 0;
int [] pre = new int [n];
int [] suf = new int [n];
for ( int i = 0; i < n; ++i) {
pre[i] = (i == 0 ? 0 : pre[i - 1])
+ ( '1' - s[i]);
}
for ( int i = n - 1; i >= 0; --i) {
suf[i] = (i == n - 1 ? 0 : suf[i + 1])
+ ( '1' - s[i]);
}
for ( int j = 1; j < n - 1; ++j) {
if (s[j] == '0' ) {
ans += j * (n - j - 1);
}
else {
ans += pre[j - 1] * suf[j + 1]
+ (j - pre[j - 1])
* (n - j - 1 - suf[j + 1]);
}
}
return ans;
}
public static void Main()
{
int N = 4;
string S = "0010" ;
Console.Write(countTriplets(N, S));
}
}
|
Javascript
<script>
function countTriplets(n, s)
{
let ans = 0;
let pre = new Array(n);
let suf = new Array(n);
for (let i = 0; i < n; ++i) {
pre[i] = (i == 0 ? 0 : pre[i - 1])
+ ( '1' - s[i]);
}
for (let i = n - 1; i >= 0; --i) {
suf[i] = (i == n - 1 ? 0 : suf[i + 1])
+ ( '1' - s[i]);
}
for (let j = 1; j < n - 1; ++j) {
if (s[j] == '0' ) {
ans += j * (n - j - 1);
}
else {
ans += pre[j - 1] * suf[j + 1]
+ (j - pre[j - 1])
* (n - j - 1 - suf[j + 1]);
}
}
return ans;
}
let N = 4;
let S = "0010" ;
document.write(countTriplets(N, S));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...