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:
- LCS formed by consecutive segments of at least length K
- Maximum number of segments of lengths a, b and c
- Maximum Sum Subsequence of length k
- Maximum sum subarray of even length
- Maximum length substring having all same characters after k changes
- Maximum Length Chain of Pairs | DP-20
- Maximum length subsequence possible of the form R^N K^N
- Find maximum sum array of length less than or equal to m
- Count maximum-length palindromes in a String
- Find maximum length Snake sequence
- Find the first maximum length even word from a string
- Print Maximum Length Chain of Pairs
- Maximum sum of non-overlapping subarrays of length atmost K
- Maximum even length sub-string that is permutation of a palindrome
- Maximum Sequence Length | Collatz Conjecture
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.