
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum length of segments of 0’s and 1’s in C++
Problem statement
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
Example
If input string is “10111000001011” the answer will 12 as follows −
- First segment is of length 7 10111000001011
- Second segment is of length 5 10111000001011
- Total length is length of (segment 1 + segment 2) = (7 + 5) = 12
Algorithm
- If start == n then 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].
Example
#include <bits/stdc++.h> using namespace std; int getSegmentWithMaxLength(int start, string str, int n, int dp[]) { if (start == n) { return 0; } if (dp[start] != -1) { return dp[start]; } dp[start] = 0; int one = 0; int zero = 0; int k; for (k = start; k < n; ++k) { if (str[k] == '1') { ++one; } else { ++zero; } if (one > zero) { dp[start] = max(dp[start], getSegmentWithMaxLength(k + 1, str, n, dp) + k - start + 1); } else { dp[start] = max(dp[start], getSegmentWithMaxLength(k + 1, str, n, dp)); } } return dp[start]; } int main() { string str = "10111000001011"; int n = str.size(); int dp[n + 1]; memset(dp, -1, sizeof(dp)); cout << "Maximum length of segment = " << getSegmentWithMaxLength(0, str, n, dp) << endl; return 0; }
Output
When you compile and execute above program. It generates following output −
Maximum length of segment = 12
- Related Articles
- Count number of binary strings of length N having only 0’s and 1’s in C++
- Construct DFA of alternate 0’s and 1’s
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Sort an arrays of 0’s, 1’s and 2’s using C++
- Sort an arrays of 0’s, 1’s and 2’s using Java
- Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++
- Count subarrays with equal number of 1’s and 0’s in C++
- Maximum sub-matrix area having count of 1's one more than count of 0’s in C++ program
- Find the Pattern of 1’s inside 0’s using C++
- C++ Largest Subtree having Equal No of 1's and 0's
- Find the index of first 1 in a sorted array of 0's and 1's in C++
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- Segregate 0’s and 1’s in an array list using Python?
- Binary representation of next greater number with same number of 1’s and 0’s in C Program?
- Count the number of 1’s and 0’s in a binary array using STL in C++

Advertisements