Count of K length subarrays containing only 1s in given Binary String
Last Updated :
04 Jan, 2022
Given a binary string str, the task is to find the count of K length subarrays containing only 1s.
Examples:
Input: str = "0101000", K=1
Output: 2
Explanation: 0101000 -> There are 2 subarrays with 1 ones
Input: str = "11111001", K=3
Output: 3
Approach: The task can be solved by keeping track of the group sizes of consecutive ones. Once, we get the groupSize, we can deduce that number of possible subarrays of length k, and all 1s, are groupSize - k + 1.
Follow the below steps to solve the problem:
- Iterate over the binary string from the start
- Increment the count, if 1 is encountered, and at a point where 0 comes.
- Store the current count to get the groupSize of consecutive 1s, and re-initialize the count to 0.
- Add the count of possible subarrays of size k in this groupSize using relation groupSize - k + 1
- Return the final sum of count.
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)
{
// Add dummy character at last to handle
// edge cases, where string ends with '1'
s += '0';
int n = s.length();
int cnt = 0, ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
cnt++;
else {
if (cnt >= k) {
ans += (cnt - k + 1);
}
cnt = 0;
}
}
return ans;
}
// Driver code
int main()
{
string str = "0101000";
int K = 1;
cout << get(str, K) << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the count of all possible
// k length subarrays
static int get(String s, int k)
{
// Add dummy character at last to handle
// edge cases, where String ends with '1'
s += '0';
int n = s.length();
int cnt = 0, ans = 0;
for (int i = 0; i < n; i++) {
if (s.charAt(i) == '1')
cnt++;
else {
if (cnt >= k) {
ans += (cnt - k + 1);
}
cnt = 0;
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
String str = "0101000";
int K = 1;
System.out.print(get(str, K) +"\n");
}
}
// This code is contributed by Rajput-Ji
Python3
# Python code for the above approach
# Function to find the count of all possible
# k length subarrays
def get(s, k):
# Add dummy character at last to handle
# edge cases, where string ends with '1'
s += '0'
n = len(s)
cnt = 0
ans = 0
for i in range(n):
if (s[i] == '1'):
cnt += 1
else:
if (cnt >= k):
ans += (cnt - k + 1)
cnt = 0
return ans
# Driver code
str = "0101000"
K = 1
print(get(str, K))
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the count of all possible
// k length subarrays
static int get(string s, int k)
{
// Add dummy character at last to handle
// edge cases, where string ends with '1'
s += '0';
int n = s.Length;
int cnt = 0, ans = 0;
for (int i = 0; i < n; i++) {
if (s[i] == '1')
cnt++;
else {
if (cnt >= k) {
ans += (cnt - k + 1);
}
cnt = 0;
}
}
return ans;
}
// Driver code
public static void Main()
{
string str = "0101000";
int K = 1;
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)
{
// Add dummy character at last to handle
// edge cases, where string ends with '1'
s += '0';
let n = s.length;
let cnt = 0, ans = 0;
for (let i = 0; i < n; i++) {
if (s[i] == '1')
cnt++;
else {
if (cnt >= k) {
ans += (cnt - k + 1);
}
cnt = 0;
}
}
return ans;
}
// Driver code
let str = "0101000";
let K = 1;
document.write(get(str, K) + '<br>');
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count of K length subarrays containing only 1s in given Binary String | Set 2 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
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 substrings of a Binary string containing only 1s Given a binary string of length N, we need to find out how many substrings of this string contain only 1s. Examples: Input: S = "0110111"Output: 9Explanation:There are 9 substring with only 1's characters. "1" comes 5 times. "11" comes 3 times. "111" comes 1 time. Input: S = "000"Output: 0 The_Appro
6 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 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