C++ Program to find minimum possible unlancedness of generated string T

C++Server Side ProgrammingProgramming

<p>Suppose we have a string S with possible characters &#39;0&#39;, &#39;1&#39; or &#39;?&#39;. We want to make string T by replacing each occurrence of &#39;?&#39; 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 &lt;= l &lt;= r &lt; Size of S. We have to find the minimum possible unbalancedness of T.</p><p>So, if the input is like S = &quot;0??0&quot;, then the output will be 2</p><p>To solve this, we will follow these steps &minus;</p><pre class="prettyprint notranslate" data-lang="cpp">Define a function check(), this will take S, x, L := 0, R = x B := true for initialize i := 0, when i &lt; size of S, update (increase i by 1), do: &nbsp; &nbsp;if S[i] is same as &#39;0&#39;, then: &nbsp; &nbsp; &nbsp; decrease L and R by 1, each &nbsp; &nbsp;if S[i] is same as &#39;1&#39;, then: &nbsp; &nbsp; &nbsp; increase L and R by 1, each &nbsp; &nbsp;if S[i] is same as &#39;?&#39;, then: &nbsp; &nbsp; &nbsp; if L is same as R, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;B := false &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(decrease L by 1) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(increase R by 1) &nbsp; &nbsp;if R is same as x + 1, then: &nbsp; &nbsp; &nbsp; if B is non-zero, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(decrease R by 1) &nbsp; &nbsp; &nbsp; Otherwise &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;R := R - 2 &nbsp; &nbsp;if L &lt; 0, then: &nbsp; &nbsp; &nbsp; if B is non-zero, then: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(increase L by 1) &nbsp; &nbsp; &nbsp; Otherwise &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L := L + 2 &nbsp; &nbsp;if L &gt; R, then: &nbsp; &nbsp; &nbsp; return false return true From the main method, do the following L := 1, R := 1000000 while L &lt;= R, do: Mid := floor of (L + R)/2 if check(S, Mid), then: R := Mid - 1 Otherwise L := Mid + 1 return R + 1</pre><h2>Example</h2><p>Let us see the following implementation to get better understanding &minus;</p><pre class="demo-code notranslate language-cpp" data-lang="cpp">#include &lt;bits/stdc++.h&gt; using namespace std; bool check(string S, int x) { &nbsp; &nbsp;int L = 0, R = x; &nbsp; &nbsp;bool B = true; &nbsp; &nbsp;for (int i = 0; i &lt; S.size(); i++) { &nbsp; &nbsp; &nbsp; if (S[i] == &#39;0&#39;) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L--, R--; &nbsp; &nbsp; &nbsp; if (S[i] == &#39;1&#39;) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L++, R++; &nbsp; &nbsp; &nbsp; if (S[i] == &#39;?&#39;) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (L == R) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B = false; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;L--; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;R++; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;if (R == x + 1) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (B) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;R--; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;R -= 2; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; if (L &lt; 0) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (B) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L += 2; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; if (L &gt; R) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return false; &nbsp; &nbsp;} &nbsp; &nbsp;return true; } int solve(string S) { &nbsp; &nbsp;int L = 1, R = 1000000; &nbsp; &nbsp;while (L &lt;= R) { int Mid = L + R &gt;&gt; 1; if (check(S, Mid)) R = Mid - 1; else L = Mid + 1; } return R + 1; } int main() { string S = &quot;0??0&quot;; cout &lt;&lt; solve(S) &lt;&lt; endl; }</pre><h2>Input</h2><pre class="result notranslate">0??0</pre><h2>Output</h2><pre class="result notranslate">2</pre>
raja
Updated on 25-Feb-2022 12:36:39

Advertisements