Maximum number of set bits count in a K-size substring of a Binary String
Last Updated :
18 Oct, 2023
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 have any set bits in it, so ans is 0.
Naive Approach:
- Generate all substring of size K.
- Find maximum of count of set bits in all substrings.
Time Complexity: O( N2).
Auxiliary Space: O(1).
Efficient Approach: The problem can be solved using Sliding window technique.
- Take maxcount variable to store maximum count of set bit and Count variable to store count set bit of current window.
- Traverse string from 1 to K and calculate the count of set bits and store as maxcount.
- Traverse string from K + 1 to length of the string.
- At every iteration, decrease count if (K – i)th bit is set. Increase count if ith bit is set. Compare and update maxcount.
- After complete array traversal, finally return maxcount.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int maxSetBitCount(string s, int k)
{
int maxCount = 0, n = s.length();
int count = 0;
for ( int i = 0; i < k; i++)
{
if (s[i] == '1' )
count++;
}
maxCount = count;
for ( int i = k; i < n; i++)
{
if (s[i - k] == '1' )
count--;
if (s[i] == '1' )
count++;
maxCount = max(maxCount, count);
}
return maxCount;
}
int main()
{
string s = "100111010" ;
int k = 3;
cout << (maxSetBitCount(s, k));
return 0;
}
|
Java
import java.util.*;
class GFG {
static int maxSetBitCount(String s, int k)
{
int maxCount = 0 , n = s.length();
int count = 0 ;
for ( int i = 0 ; i < k; i++) {
if (s.charAt(i) == '1' )
count++;
}
maxCount = count;
for ( int i = k; i < n; i++) {
if (s.charAt(i - k) == '1' )
count--;
if (s.charAt(i) == '1' )
count++;
maxCount = Math.max(maxCount, count);
}
return maxCount;
}
public static void main(String[] args)
{
String s = "100111010" ;
int k = 3 ;
System.out.println(maxSetBitCount(s, k));
}
}
|
Python3
def maxSetBitCount(s, k):
maxCount = 0
n = len (s)
count = 0
for i in range (k):
if (s[i] = = '1' ):
count + = 1
maxCount = count
for i in range (k, n):
if (s[i - k] = = '1' ):
count - = 1
if (s[i] = = '1' ):
count + = 1
maxCount = max (maxCount, count)
return maxCount
if __name__ = = '__main__' :
s = "100111010"
k = 3
print (maxSetBitCount(s, k))
|
C#
using System;
class GFG {
static int maxSetBitCount( string s, int k)
{
int maxCount = 0, n = s.Length;
int count = 0;
for ( int i = 0; i < k; i++)
{
if (s[i] == '1' )
count++;
}
maxCount = count;
for ( int i = k; i < n; i++)
{
if (s[i - k] == '1' )
count--;
if (s[i] == '1' )
count++;
maxCount = Math.Max(maxCount, count);
}
return maxCount;
}
public static void Main()
{
string s = "100111010" ;
int k = 3;
Console.Write(maxSetBitCount(s, k));
}
}
|
Javascript
<script>
function maxSetBitCount(s, k)
{
var maxCount = 0, n = s.length;
var count = 0;
for ( var i = 0; i < k; i++)
{
if (s[i] == '1' )
count++;
}
maxCount = count;
for ( var i = k; i < n; i++)
{
if (s[i - k] == '1' )
count--;
if (s[i] == '1' )
count++;
maxCount = Math.max(maxCount, count);
}
return maxCount;
}
var s = "100111010" ;
var k = 3;
document.write(maxSetBitCount(s, k));
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(1).
Brute Force in python:
Approach:
One simple approach to solve the problem is to generate all possible substrings of length K from the binary string S and count the number of set bits in each substring. Finally, return the maximum count of set bits among all the substrings.
- Define a function count_set_bits(s) that takes a binary string s and returns the count of set bits (i.e., number of ‘1’s) in it.
- Define a function max_set_bits(s, k) that takes a binary string s and an integer k and returns the maximum count of set bits in a substring of length k of s.
- Get the length n of the binary string s.
- Initialize a variable ans to 0, which will hold the maximum count of set bits among all the substrings.
- Iterate over all possible substrings of length k in the binary string s. We do this by iterating over all starting positions i from 0 to n-k and taking the substring s[i:i+k].
- For each substring, compute the count of set bits by calling the function count_set_bits(s[i:i+k]).
- Update the ans variable to hold the maximum count of set bits among all the substrings seen so far.
- Return the ans variable as the output.
Example usage: Call the max_set_bits() function with the binary string “100111010” and k=3 as inputs and print the output, which should be 3. Similarly, call the function with the binary string “0000000” and k=4 as inputs and print the output, which should be 0.
C++
#include <iostream>
using namespace std;
int countSetBits(string s) {
int count = 0;
for ( char c : s) {
if (c == '1' ) {
count++;
}
}
return count;
}
int maxSetBits(string s, int k) {
int n = s.length();
int ans = 0;
for ( int i = 0; i <= n - k; i++) {
string subStr = s.substr(i, k);
int setBitsCount = countSetBits(subStr);
ans = max(ans, setBitsCount);
}
return ans;
}
int main() {
cout << maxSetBits( "100111010" , 3) << endl;
cout << maxSetBits( "0000000" , 4) << endl;
return 0;
}
|
Java
public class Main {
public static int countSetBits(String s)
{
int count = 0 ;
for ( char c : s.toCharArray()) {
if (c == '1' ) {
count++;
}
}
return count;
}
public static int maxSetBits(String s, int k)
{
int n = s.length();
int ans = 0 ;
for ( int i = 0 ; i <= n - k; i++) {
String subStr = s.substring(i, i + k);
int setBitsCount = countSetBits(subStr);
ans = Math.max(ans, setBitsCount);
}
return ans;
}
public static void main(String[] args)
{
System.out.println(
"Maximum set bits in '100111010' with k=3: "
+ maxSetBits( "100111010" , 3 ));
System.out.println(
"Maximum set bits in '0000000' with k=4: "
+ maxSetBits( "0000000" , 4 ));
}
}
|
Python3
def count_set_bits(s):
return s.count( '1' )
def max_set_bits(s, k):
n = len (s)
ans = 0
for i in range (n - k + 1 ):
ans = max (ans, count_set_bits(s[i:i + k]))
return ans
print (max_set_bits( "100111010" , 3 ))
print (max_set_bits( "0000000" , 4 ))
|
C#
using System;
class Program {
static int CountSetBits( string s)
{
int count = 0;
foreach ( char c in s)
{
if (c == '1' ) {
count++;
}
}
return count;
}
static int MaxSetBits( string s, int k)
{
int n = s.Length;
int ans = 0;
for ( int i = 0; i <= n - k; i++) {
string subStr = s.Substring(i, k);
int setBitsCount = CountSetBits(subStr);
ans = Math.Max(ans, setBitsCount);
}
return ans;
}
static void Main()
{
Console.WriteLine(MaxSetBits( "100111010" , 3));
Console.WriteLine(MaxSetBits( "0000000" , 4));
}
}
|
Javascript
function countSetBits(s) {
let count = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] === '1' ) {
count++;
}
}
return count;
}
function maxSetBits(s, k) {
const n = s.length;
let ans = 0;
for (let i = 0; i <= n - k; i++) {
const subStr = s.substring(i, i + k);
const setBitsCount = countSetBits(subStr);
ans = Math.max(ans, setBitsCount);
}
return ans;
}
console.log(maxSetBits( "100111010" , 3));
console.log(maxSetBits( "0000000" , 4));
|
Time Complexity: O(N * K^2), where N is the length of the binary string S.
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...