Count of K length subarrays containing only 1s in given Binary String | Set 2 Last Updated : 29 Dec, 2021 Comments Improve Suggest changes Like Article Like Report Given binary string str, the task is to find the count of K length subarrays containing only 1s. Examples Input: str = "0101000", K=1Output: 2Explanation: 0101000 -> There are 2 subarrays of length 1 containing only 1s. Input: str = "11111001", K=3Output: 3 Approach: The given problem can also be solved by using the Sliding Window technique. Create a window of size K initially with the count of 1s from range 0 to K-1. Then traverse the string from index 1 to N-1 and subtract the value of i-1 and add the value of i+K to the current count. Here if the current count is equal to K, increment the possible count of subarrays. Below is the implementation of the above approach. C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the count of all possible // k length subarrays int get(string s, int k) { int n = s.length(); int cntOf1s = 0; for (int i = 0; i < k; i++) if (s[i] == '1') cntOf1s++; int ans = cntOf1s == k ? 1 : 0; for (int i = 1; i < n; i++) { cntOf1s = cntOf1s - (s[i - 1] - '0') + (s[i + k - 1] - '0'); if (cntOf1s == k) ans++; } return ans; } // Driver code int main() { string str = "0110101110"; int K = 2; cout << get(str, K) << endl; return 0; } Java // Java code to implement above approach import java.util.*; public class GFG { // Function to find the count of all possible // k length subarrays static int get(String s, int k) { int n = s.length(); int cntOf1s = 0; for (int i = 0; i < k; i++) { if (s.charAt(i) == '1') { cntOf1s++; } } int ans = cntOf1s == k ? 1 : 0; for (int i = 1; i < n; i++) { if(i + k - 1 < n) { cntOf1s = cntOf1s - (s.charAt(i - 1) - '0') + (s.charAt(i + k - 1) - '0'); } if (cntOf1s == k) ans++; } return ans; } // Driver code public static void main(String args[]) { String str = "0110101110"; int K = 2; System.out.println(get(str, K)); } } // This code is contributed by Samim Hossain Mondal. Python3 # Python code to implement above approach # Function to find the count of all possible # k length subarrays def get(s, k): n = len(s); cntOf1s = 0; for i in range(0,k): if (s[i] == '1'): cntOf1s += 1; ans = i if (cntOf1s == k) else 0; for i in range(1, n): if (i + k - 1 < n): cntOf1s = cntOf1s - (ord(s[i - 1]) - ord('0')) + (ord(s[i + k - 1]) - ord('0')); if (cntOf1s == k): ans += 1; return ans; # Driver code if __name__ == '__main__': str = "0110101110"; K = 2; print(get(str, K)); # This code is contributed by 29AjayKumar C# // C# code to implement above approach using System; public class GFG { // Function to find the count of all possible // k length subarrays static int get(string s, int k) { int n = s.Length; int cntOf1s = 0; for (int i = 0; i < k; i++) { if (s[i] == '1') { cntOf1s++; } } int ans = cntOf1s == k ? 1 : 0; for (int i = 1; i < n; i++) { if (i + k - 1 < n) { cntOf1s = cntOf1s - (s[i - 1] - '0') + (s[i + k - 1] - '0'); } if (cntOf1s == k) ans++; } return ans; } // Driver code public static void Main() { string str = "0110101110"; int K = 2; Console.WriteLine(get(str, K)); } } // This code is contributed by ukasp. JavaScript <script> // JavaScript code for the above approach // Function to find the count of all possible // k length subarrays function get(s, k) { let n = s.length; let cntOf1s = 0; for (let i = 0; i < k; i++) if (s[i] == '1') cntOf1s++; let ans = cntOf1s == k ? 1 : 0; for (let i = 1; i < n; i++) { cntOf1s = cntOf1s - (s[i - 1] - '0') + (s[i + k - 1] - '0'); if (cntOf1s == k) ans++; } return ans; } // Driver code let str = "0110101110"; let K = 2; document.write(get(str, K) + '<br>') // This code is contributed by Potta Lokesh </script> Output3 Time Complexity: O(N), Where N is the length of the string. Auxiliary Space: O(1). Comment More infoAdvertise with us Next Article Count of K length subarrays containing only 1s in given Binary String | Set 2 C code_r Follow Improve Article Tags : Strings Algo Geek DSA Algo-Geek 2021 binary-string subarray sliding-window +3 More Practice Tags : sliding-windowStrings Similar Reads Count of K length subarrays containing only 1s in given Binary String Given a binary string str, the task is to find the count of K length subarrays containing only 1s. Examples: Input: str = "0101000", K=1Output: 2Explanation: 0101000 -> There are 2 subarrays with 1 ones Input: str = "11111001", K=3Output: 3 Approach: The task can be solved by keeping track of the 4 min read Find all K length subarrays containing only 1s in given Binary String Given a binary string str[], the task is to find all possible K length subarrays containing only 1s and print their starting and ending index. Examples: Input: str = "0101000", K=1Output: 1 13 3Explanation: Substrings at positions 1 and 3 are the substrings with value 1. Input: str = "11111001", K=3 6 min read Count of binary strings of given length consisting of at least one 1 Given an integer N, the task is to print the number of binary strings of length N which at least one '1'. Examples: Input: 2 Output: 3 Explanation: "01", "10" and "11" are the possible strings Input: 3 Output: 7 Explanation: "001", "011", "010", "100", "101", "110" and "111" are the possible strings 3 min read Count of substrings of a binary string containing K ones Given a binary string of length N and an integer K, we need to find out how many substrings of this string are exist which contains exactly K ones. Examples: Input : s = â10010â K = 1 Output : 9 The 9 substrings containing one 1 are, â1â, â10â, â100â, â001â, â01â, â1â, â10â, â0010â and â010âRecommen 7 min read Count of setbits in bitwise OR of all K length substrings of given Binary String Given a binary string str of length N, the task is to find the number of setbits in the bitwise OR of all the K length substrings of string str. Examples: Input: N = 4, K = 3, str = "1111"Output: 3Explanation: All 3-sized substrings of S are:"111" and "111". The OR of these strings is "111". Therefo 8 min read Like