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.
Input: S = “100111010”, K = 3
The substring “111” contains 3 set bits.
Input:S = “0000000”, K = 4
Explanation: S doesn’t have any set bits in it, so ans is 0.
- Generate all substring of size K.
- Find maximum of count of set bits in all substrings.
Time Complexity: O( N2).
Auxillary 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:
Time Complexity: O(N).
Auxiliary Space: O(1).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum splits in binary string such that each substring is divisible by given odd number
- Maximize count of 0s in left and 1s in right substring by splitting given Binary string
- Count substring of Binary string such that each character belongs to a palindrome of size greater than 1
- Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Check if bits of a number has count of consecutive set bits in increasing order
- Find the maximum possible Binary Number from given string
- Given a binary string, count number of substrings that start and end with 1.
- Lexicographical Maximum substring of string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Minimum steps to remove substring 010 from a binary string
- Maximum length substring with highest frequency in a string
- Minimum flips required in a binary string such that all K-size substring contains 1
- Count binary strings with k times appearing adjacent two set bits
- Count of total bits toggled/flipped in binary representation of 0 to N
- Maximize count of set bits in a root to leaf path in a binary tree
- Decode a string recursively encoded as count followed by substring
- Minimum flips required to form given binary string where every flip changes all bits to its right as well
- Find the largest number smaller than integer N with maximum number of set bits
- Maximum number of contiguous array elements with same number of set bits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.