Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
C++ Program to find minimum possible unlancedness of generated string T
Suppose we have a string S with possible characters '0', '1' or '?'. We want to make string T by replacing each occurrence of '?' with 0 or 1. The unbalancedness of T is like: maximum of the all absolute differences between number of occurrences of 0 and 1 in between lth and rth character in S where 0
So, if the input is like S = "0??0", then the output will be 2
To solve this, we will follow these steps −
Define a function check(), this will take S, x, L := 0, R = x B := true for initialize i := 0, when i R, then: return false return true From the main method, do the following L := 1, R := 1000000 while LExample
Let us see the following implementation to get better understanding −
#includeusing namespace std; bool check(string S, int x) { int L = 0, R = x; bool B = true; for (int i = 0; i R) return false; } return true; } int solve(string S) { int L = 1, R = 1000000; while (L > 1; if (check(S, Mid)) R = Mid - 1; else L = Mid + 1; } return R + 1; } int main() { string S = "0??0"; cout Input
0??0Output
2
Advertisements
