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).
Auxillary Space: O(1).
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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 number of binary strings such that there is no substring of length greater than or equal to 3 with all 1's
- 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 steps to remove substring 010 from a binary string
- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- 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 of total bits toggled/flipped in binary representation of 0 to N
- Count binary strings with k times appearing adjacent two set bits
- 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
- Number of binary strings such that there is no substring of length ≥ 3
- Prime Number of Set Bits in Binary Representation | Set 1
- Maximum sum by adding numbers 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 email@example.com. 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.