Given a string comprising of ones and zeros. The task is to find the maximum length of the segments of string such that a number of 1 in each segment is greater than 0.
Note: Each segment taken should be distinct. Index starts from 0.
Input: str = “100110001010001”
First segment from index 0 to 4 (10011), total length = 5
Second segment from index 8 to 10 (101), total length = 3
Third segment from index 14 till 14 (1), total length = 1,
Hence asnwer is 5 + 3 + 1 = 9
Input: str = “0010111101100000”
The maximum length can be formed by taking segment
from index 0 till index 12 (0010111101100),
i.e. of total length = 13
- If start == n, limiting condition arises, return 0.
- Run a loop from start till n, computing for each subarray till n.
- If character is 1 then increment the count of 1 else increment the count of 0.
- If count of 1 is greater than 0, recursively call the function for index (k+1) i.e. next index and add the remaining length i.e. k-start+1.
- Else only recursively call the function for next index k+1.
- Return dp[start].
Below is the implementation of above approach:
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.
- LCS formed by consecutive segments of at least length K
- Maximum number of segments of lengths a, b and c
- Maximum number on 7-segment display using N segments : Recursive
- Partition the array in K segments such that bitwise AND of individual segment sum is maximized
- Check if a large number can be divided into two or more segments of equal sum
- Divide the array in K segments such that the sum of minimums is maximized
- Minimum splits required to convert a number into prime segments
- Count of ways to split a given number into prime segments
- Minimum length of the sub-string whose characters can be used to form a palindrome of length K
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Construct a string of length L such that each substring of length X has exactly Y distinct letters
- Maximum length of balanced string after swapping and removal of characters
- Maximum length palindrome that can be created with characters in range L and R
- Maximum length palindromic substring such that it starts and ends with given char
- Maximum length prefix of one string that occurs as subsequence in another
- Find maximum length Snake sequence
- Print Maximum Length Chain of Pairs
- Maximum length substring having all same characters after k changes
- Maximum length subsequence with difference between adjacent elements as either 0 or 1
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.