We need to understand the problem: given a binary string of length n up to 2e5, we can repeatedly remove any adjacent pair of characters where one is 0 and the other is 1 (i.e., they differ). The removal eliminates both characters, thus shortening the string by 2. We can choose any adjacent pair at each step. The goal is to minimize the final length.
This is reminiscent of a reduction process similar to "eliminate 01 or 10 pairs". The operation is akin to canceling opposite bits. The order can matter, but we need the minimal possible residual length.
Observation: The operation essentially allows us to cancel a 0 with a neighboring 1. This is similar to treating 0 and 1 as opposite charges that can annihilate if adjacent. The string after removals may not be contiguous originally? But removals are applied sequentially, and adjacency is considered in the current string.
The minimal length is determined by the difference between counts? Not exactly, because sometimes you might not be able to cancel all pairs if they are not brought together appropriately. Let's think.
Consider we have counts: count of zeros = z, count of ones = o. Each operation reduces both by 1. So after k operations, the remaining zeros and ones satisfy: zeros' = z - k, ones' = o - k, and the total length = n - 2k = (z+o) - 2k. The maximum number of operations is limited by min(z, o). However, can we always achieve k = min(z, o) cancellations? Not necessarily, because zeros and ones might be separated such that you cannot bring them together to cancel all possible pairs. For example, if the string is "000111", you can cancel adjacent 0 and 1 at the boundary: ... you can cancel the 0 and 1 at position 3-4? Actually "000111": indices 1-3 zeros, 4-6 ones. Adjacent pairs across boundary: position 3 and 4 are 0 and 1 adjacent; remove them, you get "0011" (since removal reduces length: remove indices 3 and 4? Let's simulate: original: 0,0,0,1,1,1. Remove the pair (3,4): the third and fourth characters (0 and 1) are removed. The new string: positions: 1:0,2:0, then the remaining: original 5:1,6:1? Actually after removal, we concatenate left and right parts: characters at indices 1,2,5,6 => "0,0,1,1". So we have "0011". Then we can again remove the middle pair? The pair (2,3) now are 0 and 1? The string "0,0,1,1": positions: 1=0,2=0,3=1,4=1. Adjacent pairs: (2,3) are 0 and 1 -> remove them, we get "01"? Actually remove positions 2 and 3: left part: index1=0, right part: index4=1 => "0,1" = "01". Then remove that pair -> empty. So we can cancel all. So balanced string can be fully canceled if they are interleaved appropriately? Actually "000111" can be fully canceled.
But consider "010101" balanced, can we cancel all? Probably yes, by repeatedly canceling adjacent differing pairs. But is it always possible to cancel all if counts are equal? Not always: Consider "1010"? Let's test: "1010": we can cancel a pair: maybe (1,2) are 1 and 0 -> remove -> string becomes "10"? Actually indices: 1=1,2=0,3=1,4=0. Remove (1,2): we get characters 3,4 => "10". Then remove -> empty. So yes. Consider "1100" from sample gave 0. So maybe if counts equal, you can always cancel to 0? Let's try a tricky case: "101100"? That's length 6, 3 zeros, 3 ones? Actually "101100": digits: 1,0,1,1,0,0 => zeros:3, ones:3. Can we cancel all? Possibly. Try to think if there's any case where equal counts cannot be fully canceled? Maybe "1100" yes. "1001"? That's 1,0,0,1: zeros:2, ones:2. Can we cancel all? Try: "1001". Options: (1,2): 1 and 0 -> remove -> we get "01"? Actually removal: indices 1 and 2 removed, leaving "0,1"? Wait original: [1,0,0,1]. Remove positions 1-2 (1 and 0) -> remaining: indices 3,4 = 0,1 => "01". Then remove -> empty. So yes.
What about "101001"? That's 1,0,1,0,0,1 => zeros:3? Actually zeros: positions 2,4,5 => 3 zeros; ones: positions1,3,6 =>3 ones. Could we cancel all? Probably.
It seems equal counts might always be reducible to empty? But consider a string with all zeros on one side and all ones on the other: "000111". We saw it can be fully canceled. So maybe any string can be reduced to the absolute difference between the counts? But sample 3: n=8, "11101111" output 6. Let's calculate: zeros count? The string: 1,1,1,0,1,1,1,1. So zeros:1, ones:7. Difference = 6. Output is 6. So that matches the difference. But sample 2: "01010": zeros count: 2? Actually "01010": positions: 0,1,0,1,0 => zeros:3? Wait check: characters: 0,1,0,1,0 => zeros:3, ones:2. Difference =1, output =1. So that matches difference too. Sample 1: "1100": zeros:2, ones:2, difference=0, output=0. So is the answer simply |#ones - #zeros|? But is it always achievable? Let's test with some examples. Suppose "0". diff=1, answer should be 1. Can we reduce it? No operation possible, so length 1. So ok. "1": diff=1. "00": zeros=2, ones=0, diff=2. Can we reduce? No adjacent differing pair because both same, so length remains 2. So answer 2. So |z-o| = 2? Wait zeros=2, ones=0 -> diff=2. That matches. "01": zeros=1, ones=1 diff=0, we can cancel both -> 0. "10": same. "0011": zeros=2, ones=2 diff=0, can we cancel fully? Probably yes: "0011" -> remove middle (positions 2 and 3: 0 and 1) -> "01" -> remove -> 0. "010": zeros=2? Actually "010": 0,1,0 => zeros=2, ones=1 diff=1, can we reduce to length 1? Let's try: "010". Options: we can remove (1,2): 0 and 1 -> left "0"? Actually after removal of positions 1 and 2, we get character 3: 0 -> string "0". So length 1. So answer 1, matches diff. "101": zeros=1, ones=2 diff=1 -> can we reduce to 1? Remove (2,3): 0 and 1? Actually positions:1=1,2=0,3=1. Remove (2,3): 0 and 1 -> left "1". So length 1. Good. "110": zeros=1, ones=2 diff=1, can we reduce to 1? Options: remove (1,2): 1 and 1 -> cannot because same; remove (2,3): 1 and 0 -> 1 and 0 differ, so remove -> left "1". So length 1. Good. "100": zeros=2, ones=1 diff=1, can we reduce to 1? remove (2,3): 0 and 0? No; remove (1,2): 1 and 0 -> remove -> left "0". So length 1. Good. "000": diff=3, cannot reduce any, length 3. Good. "111": diff=3. So far so good.
Now test a more complex string: "010110". Counts: zeros? 0,1,0,1,1,0 => zeros: positions 1,3,6 =>3 zeros; ones: positions2,4,5 =>3 ones, diff=0. Can we reduce to 0? Let's try to see if we can cancel all. String: 0 1 0 1 1 0. Attempt: Remove (2,3): positions 2 and 3 are 1 and 0 -> remove -> new string: keep 1, then positions 4,5,6? Actually after removing indices 2 and 3, we get: [pos1=0] + [pos4=1, pos5=1, pos6=0] => 0 1 1 0. Now remove (2,3): positions 2 and 3 are 1 and 1? not differ. Remove (3,4): positions 3 and 4: 1 and 0 differ -> remove -> left 0 1? Actually after removal, keep pos1=0 and pos2=1 => "01". Then remove -> empty. So yes.
Consider "00100": zeros=4, ones=1 diff=3. Can we reduce to 3? Probably yes: we can cancel the one 1 with a neighboring 0. Let's try: "00100": indices:1=0,2=0,3=1,4=0,5=0. Remove (3,4): 1 and 0 -> removed -> string becomes "000"? Actually left: indices 1,2,5 => 0,0,0 => "000". No further reduction, length 3, diff=3. So works.
Consider "010100": zeros=4? Actually: 0,1,0,1,0,0 => zeros: positions1,3,5,6 =>4 zeros; ones: positions2,4 =>2 ones diff=2. Can we reduce to 2? Try to cancel both ones: maybe remove (2,3) -> 1 and 0 -> becomes "0 1 0 0"? Let's simulate: original: indices:1=0,2=1,3=0,4=1,5=0,6=0. Remove (2,3): after removal, we have indices1=0, and then 4,5,6 => 1,0,0 => string "0 1 0 0". Now remove (1,2): 0 and 1 -> remove -> left "0 0"? Actually after removal of indices1 and2 (0 and 1) we get from remaining indices3,4: 0,0 => "00". So length 2, matches diff. Good.
So it appears answer = |#ones - #zeros|? But we must be careful: Is it always achievable? Let's try a string where zeros and ones are not contiguous in a way that might leave an odd leftover? The operation is similar to eliminating opposite bits that are adjacent. This is essentially like we can cancel any 0 and 1 if we can bring them together by prior cancellations. This process is reminiscent of reducing a string by removing "01" or "10" substrings. This is known to be similar to treating the string as a stack: scanning left to right, we can simulate cancellations: if we see a character, we compare with the top of stack; if they are different, we pop; otherwise push. This algorithm yields a final string that is irreducible (no adjacent differing pairs). And that final string will consist of only one type? Actually if we apply that stack approach: start with empty stack, iterate characters: if stack not empty and top differs from current, pop; else push current. At the end, the stack contains a string where all adjacent characters are the same (because if there were differing adjacent, they would have been canceled). However, note that after cancellations, the stack may have alternating pattern? Actually if we process sequentially, the rule "if top differs, pop" ensures that after processing, the stack never has a pair of adjacent differing characters? But consider: after popping, the new top may differ from the current? Actually the algorithm processes one char at a time. It pushes if top same or empty; if top differs, it pops (so the current char is also not pushed). So the result is a string that is "reduced" in the sense that it cannot be reduced further by removing adjacent differing pairs, but is it minimal? I think this greedy algorithm yields a final string that is independent of the order of cancellations? Possibly the length of the final string after any sequence of reductions is invariant up to the absolute difference? Let's analyze.
We can view the process as: each cancellation removes a 0 and a 1. So the number of zeros and ones remaining after any sequence is (z - k, o - k) where k ≤ min(z, o). The total length is n - 2k. To minimize length, we want to maximize k. So we want the maximum number of cancellations possible, i.e., the maximum number of 0-1 pairs we can eliminate via adjacent removals. That maximum is not necessarily min(z, o) because some zeros and ones might be "trapped" and unable to meet? But the stack algorithm suggests that after performing cancellations in any order, the final string is determined up to the order? There might be an invariant like the parity of something or the absolute difference between counts of zeros and ones? Let's think about the process.
We can consider the string as a sequence. The operation reduces the string by removing an adjacent 0 and 1. This is reminiscent of the cancellation in the group Z/2Z: treat 0 as +1, 1 as -1? Actually if we map 0 to +1 and 1 to -1, then the sum of the whole string is (#0 - #1). Removing adjacent 0 and 1 (which have opposite contributions) reduces the total sum by 0? Because +1 + (-1) = 0, so the total sum remains unchanged. So sum is invariant? Actually if we remove a 0 and a 1, the sum of removed bits is 0, so the sum of remaining bits equals the original sum. So the sum (excess of zeros over ones) is invariant under the operation. The length reduces by 2, but sum remains constant. So after cancellations, we have a string of length L with sum S (where S = zeros' - ones'). The maximum possible cancellations corresponds to minimizing L subject to the constraints: L = zeros' + ones' and zeros' - ones' = S. So zeros' = (L+S)/2, ones' = (L - S)/2, both must be nonnegative integers. Since zeros' and ones' are integers, L and S must have the same parity (L ≡ S mod 2). Also L ≥ |S| (since zeros'≥0, ones'≥0 => L ≥ |S|). Also the remaining string must be such that no adjacent differing pair exists? Actually if there were an adjacent 0 and 1, we could perform another cancellation, so a minimal length string must be such that no adjacent characters differ; i.e., it is a string of all 0s or all 1s, or possibly a string of alternating pattern? Wait if it has both 0 and 1 but no adjacent differing? That's impossible because if both 0 and 1 exist, somewhere there is a transition from 0 to 1 or 1 to 0; at that boundary, the two adjacent characters differ. So any string containing both 0 and 1 must have at least one adjacent differing pair (unless the string is not contiguous? But it is contiguous). Actually consider "0011": the boundary between the last 0 and first 1 are adjacent and differ. So yes, if both characters exist, there is at least one adjacent differing pair. Therefore, a string that cannot be reduced further (i.e., a "reduced" string) must consist entirely of one character, either all 0s or all 1s. Could it be empty? Yes, empty string has no characters.
Thus the minimal possible string after operations must be either empty or consist solely of 0s or solely of 1s. Its length L must satisfy L ≥ |S|, and L ≡ S (mod 2) (since zeros' and ones' integers). Also, the remaining string is all zeros if S > 0 (more zeros than ones) or all ones if S < 0 (more ones than zeros) or empty if S=0. Actually if S>0, zeros' > ones', and since the string is all zeros (if minimal), then zeros' = L, ones' = 0, so S = L. So L = S. If S<0, then L = -S (since ones' = L, zeros'=0, so S = -L). If S=0, L could be 0 (empty). So the minimal achievable length seems to be |S|, provided we can actually reduce to a string of all same bits. But is it always possible to achieve this? That's the key question.
We have observed examples where it works. But consider a string with S=0, can we always reduce to empty? Possibly yes. But is there any counterexample? Let's try "0101"? We did, works. "0011"? works. "0110"? That's 0,1,1,0: zeros=2, ones=2, S=0. Can we reduce to empty? Try: "0110". Options: remove (2,3): 1 and 1 -> not allowed; remove (1,2): 0 and 1 -> remove -> leaves "10"? Actually after removing positions1 and2: we get "1,0" => "10". Then remove -> empty. So yes. "0100"? That's zeros=3, ones=1, S=2. Can we reduce to "00"? Let's try: "0100". We need to eliminate the one 1 with a 0. Options: remove (1,2): 0 and 1 -> leaves "00" -> length 2. So yes. "0010"? zeros=3, ones=1, S=2. Remove (2,3): 0 and 1 -> leaves "00"? Actually original:0,0,1,0. Remove positions2 and3 (0,1) -> left: pos1=0, pos4=0 => "00". So yes.
But consider a string like "000111000111"? That's zeros=6? Actually 0,0,0,1,1,1,0,0,0,1,1,1: zeros:6, ones:6, S=0. Can we reduce to empty? Likely yes. But is there any string where the sum S is not achievable because of parity? Actually S and L parity condition: For S=0, L must be even to have integer zeros' and ones'? Actually if S=0, zeros'=ones'=L/2, so L must be even. But we want L=0, which is even, ok.
But could there be a scenario where we cannot cancel enough pairs because the zeros and ones are "separated" in a way that after some cancellations we get stuck with a pattern like "010101" that still has both bits but no adjacent differing? That cannot happen because alternating pattern always has adjacent differing. So if both bits remain, there is always some adjacent differing pair. So the process can continue until only one type remains. So the only obstacle is whether we can cancel enough pairs to achieve the theoretical minimum length |S|. That would require canceling exactly min(z,o) pairs? Because if we cancel k pairs, remaining zeros = z - k, ones = o - k. To have only zeros left, we need ones' = 0 => k = o. Similarly for only ones left, k = z. So we need to be able to cancel all of the minority bit. So the question reduces to: can we always cancel all the minority bits? In other words, given a binary string, is it always possible to eliminate all ones if there are at most as many ones as zeros? Or eliminate all zeros if zeros ≤ ones? The answer appears to be yes, but we must be careful: could there be a configuration where, no matter the order, we cannot cancel a particular minority bit because it becomes isolated between same bits? Let's attempt to construct a counterexample.
Suppose we have zeros=2, ones=1, string: "101". Actually zeros=2? Wait "101": zeros=1? Actually "101": zeros=1, ones=2. So minority is zeros. To eliminate the zero, we need to pair it with a one. But we have two ones, seems possible. Indeed we can cancel.
What about zeros=3, ones=1, string: "1101"? zeros=1? Actually "1101": 1,1,0,1 -> zeros=1, ones=3, minority zero. Can we eliminate zero? Try: remove (3,4): 0 and 1 -> leaves "11 1"? Actually after removal of positions 3 and 4: keep 1,2 -> "11". So zero gone, length 2. Works.
Maybe a more subtle case: zeros=2, ones=2, but maybe we get stuck with "01" left? That's still reducible. So maybe always possible.
But consider the stack algorithm: scanning left to right, perform cancellation when top differs. This algorithm yields a final stack that consists of some number of characters all the same? Let's check. Starting with empty, process each character. At each step, if current char differs from top, pop; else push. After full processing, what's on the stack? For "0011", let's simulate: push 0; next 0: same, push -> stack: 00; next 1: top=0 differs -> pop -> stack:0; next 1: top=0 differs -> pop -> stack empty. So empty.
For "010": push 0; next 1: top 0 differs -> pop -> stack empty; next 0: stack empty, push 0 -> stack:0. So result is a single 0. So L = |S| = 1.
For "1100": push1; next1: same -> push1 -> stack:11; next0: top1 differs -> pop -> stack:1; next0: top1 differs -> pop -> stack empty. So empty.
For "1010": push1; next0: differs -> pop -> empty; next1: empty -> push1; next0: differs -> pop -> empty. So empty.
For "0110": push0; next1: differs -> pop -> empty; next1: empty -> push1; next0: differs -> pop -> empty. So empty.
For "0101": similar.
Now consider a string where the stack algorithm yields something like "01"? That would imply top and new character are same? Actually if we end with "01", that would mean we have two characters left, and they are different. But would that be possible given the algorithm? Suppose after processing, we have stack with two characters. Since the algorithm never allows a situation where top differs from new without popping, the final stack cannot have adjacent differing characters because if at any point we had top X and we push Y differing, we would have popped instead. But after processing all characters, if the stack has two characters, the top is the last pushed. Could the two characters differ? Consider state: stack = [0] (top 0). Next char = 1: since differs, we pop, resulting empty. So you cannot have stack with [0,1] because when you try to push the 1 after a 0, you pop. However, you could have [1,0] if the order is such that you had stack [1] then 0 comes, it would pop. So you cannot have differing adjacent in final stack. So final stack must be all same character. Indeed, can we ever have stack with two different? Let's test: Suppose stack currently [1] (top 1). Next char 0: differs, so we pop, stack becomes empty. So we never push the 0. So stack can never contain two different characters at any time. So the algorithm maintains the invariant that the stack consists entirely of the same character (since we only push when stack is empty or top is same as current). Wait is that true? Let's check: if stack is [0] and we get 0, we push, so stack becomes [0,0] all same. If stack is empty and we get 1, we push 1. So indeed, at any point, the stack is a sequence of identical characters (all 0s or all 1s). Because we only push when the current char equals the top (or stack empty). And when we pop due to differ, we remove the top, but that might change the top to something else? But the stack after popping: if we had stack [0,0] and we get 1: top=0 differs, we pop, stack becomes [0]. Now we have not pushed the 1; we continue with next character. So stack still all 0s. So yes, the stack always consists of a run of identical bits. So the final stack is either all zeros or all ones. Its length is something. And the sum S = (if stack is zeros: length; if ones: -length). Indeed, the total sum of the string is zeros - ones, which is invariant under operations. Under the stack algorithm, the sum is also preserved? Let's check: When we pop, we remove the top (say 0) and discard the incoming 1; that effectively removes a 0 and a 1, which leaves sum unchanged. When we push, we add a character (0 or 1) to the stack, which changes sum accordingly. But the net sum after processing all characters should equal the sum of the original string. And at the end, the stack contains only one type, so its sum = length if zeros, or -length if ones. Therefore, the length of the stack must be |S|. Since the stack algorithm yields a result that is reachable by some sequence of operations (the algorithm corresponds to a specific order of cancellations), this shows that we can achieve a final string of length |S| consisting of all zeros or all ones. Therefore, the minimal possible length is indeed |S|.
Thus the answer is simply the absolute difference between the count of zeros and ones. That matches all samples.
But wait, is the stack algorithm always valid as a sequence of operations that respects adjacency? The algorithm's process: when we encounter a character, we compare with the top of the stack; if they differ, we pop the top. This corresponds to canceling the current character with the top of the stack. However, are the current character and the top character adjacent in the current string? The stack represents the current string after some cancellations. If we maintain the string as the stack order (the order left to right), then the top of stack corresponds to the last character of the current string. The new character we are processing is the next character in the original order. But after some cancellations, the string is not exactly the original order? Actually if we simulate the process by scanning the original string left to right and maintaining a stack, each step either pushes the current character or pops the top. This simulates a specific sequence of cancellations: whenever we see a character that is opposite to the last character of the current reduced string (the stack top), we cancel them (pop). This is a valid operation because the last character of the current string and the new character are adjacent in the current string? Let's think: The current string (the stack) represents the string after processing previous characters and performing cancellations among them. At each step, we consider the next character from the original string. In the current string, the characters are in order from left to right as they appear after previous operations. The next character is to be appended to the right end of the current string. So before we decide to cancel, we have current string S (the stack) and the new character c. They are adjacent at the boundary: the last character of S (top) and c are adjacent. So if they differ, we can cancel them by removing both. That corresponds to popping the stack and not adding c. So indeed the stack algorithm is a valid sequence of operations: each pop corresponds to performing a cancellation between the new character and the last character of the current string. So the final stack is the result after performing these operations in that specific order. Therefore, it's always possible to achieve that final string.
Thus the minimal length is |count0 - count1|. The algorithm works in O(n) time.
Given n up to 2e5, we just count zeros and ones.
Edge cases: n=0? Not allowed. n>=1.
Thus answer is abs(z-o).
But wait, what about the third sample: "11101111": zeros=1, ones=7, diff=6, output 6. So matches.
Thus solution is trivial.
But hold on: There might be a nuance: The operation removes any two adjacent positions with differing bits. That means we can choose any adjacent pair, not necessarily at the boundary. The stack algorithm only cancels at the right boundary. But that's still a valid strategy, and yields a final length equal to |S|. Could there be a better result than |S|? No, because we argued that any final string must be all same, and its length must satisfy L = |S| (since sum invariant). So that's the lower bound. And we have a construction achieving it. So yes.
Thus answer is straightforward.
However, is it possible that after operations, the string might become empty even if S != 0? That would imply sum=0 but S !=0, impossible because sum invariant. So no.
Thus we can just output abs(z-o).
Let's double-check with some weird strings: n=1, string "0": diff=1, answer 1. Good.
n=2 "00": diff=2, answer 2.
n=2 "01": diff=0, answer 0.
n=3 "000": diff=3, answer 3.
n=3 "001": diff=1, answer 1? Can we reduce "001" to length 1? Try: "001": remove (2,3): 0 and 1 -> left "0". So yes.
What about "0101"? diff=0, answer 0.
Now consider "000111000": zeros=6, ones=3, diff=3. Can we reduce to "000"? Possibly: we need to eliminate all ones. Should be doable.
The stack algorithm: process: zeros then ones then zeros. Let's simulate: push 0 three times -> stack three zeros; then encounter 1: top 0 differs -> pop -> stack two zeros; next 1: top 0 differs -> pop -> stack one zero; next 1: top 0 differs -> pop -> stack empty; then zeros: push 0 three times -> stack three zeros. So final stack length 3 zeros. So works.
Thus answer is correct.
But there is a nuance: The operation removes adjacent pairs anywhere. Could we sometimes get stuck with a string that has both bits but no adjacent differing? As argued, that's impossible because if both bits exist, there must be at least one transition point where adjacent bits differ. However, is it possible that after some cancellations, the string becomes something like "010", which has a 0 and 1 adjacent, so you can continue. So indeed, if both types remain, you can always cancel at least one more pair. So the process can continue until only one type remains.
Thus minimal length is |z-o|.
Therefore, solution: count zeros, count ones, output abs(z-o).
But wait, there is a potential pitfall: The problem statement: "we choose any two adjacent positions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits". That's exactly the operation. So it's symmetric.
Thus the answer is as described.
But let's check sample 3 again: "11101111" -> zeros=1, ones=7, diff=6. The sample output is 6. Good.
Now check if there is any case where we might be forced to have a longer string because of parity constraints? Suppose S=1, then minimal length theoretically is 1 (since L must be odd to match parity? Actually if S=1, we need zeros' - ones' =1. If we aim for all zeros, then ones'=0, zeros'=L, so L=1. That's odd, fine. If we aim for all ones, then zeros'=0, ones'=L, so S = -L => L = -1, impossible. So the only possibility is all zeros. So L must be 1. So parity is fine.
If S=2, then L could be 2 (all zeros). So L = S always works.
Thus no parity constraint issue because L = |S| yields L ≡ S (mod 2) since if S positive, L=S, so L≡S mod2 trivially. Similarly negative.
Thus solution stands.
Now we can implement.
But careful: n up to 2e5, but we only count characters. So O(n) time and O(1) memory.
Implementation steps:
- Read n (though we can ignore, just read string length).
- Read string s.
- Count zeros = s.count('0'), ones = s.count('1') or n - zeros.
- Output abs(zeros - ones).
But note: n may not equal len(s) if input has spaces? Typically it matches.
Edge cases: none.
Thus answer.
I'll produce final solution accordingly.
But I should also consider if there is any trick: Could the answer be something else for some strings? Let's try to think about the invariant more rigorously.
Define f = number of zeros - number of ones. Operation removes one zero and one one, so f unchanged. So final string length L and composition must satisfy f = zeros' - ones'. If final string is all zeros, then ones'=0, zeros'=L, so f = L => L = f (which must be nonnegative). If final string is all ones, then zeros'=0, ones'=L => f = -L => L = -f (f negative). So L = |f|. So any achievable final length must be at least |f|. And we need to show that |f| is achievable. The stack algorithm demonstrates that we can achieve a final string of length |f|, composed of the majority bit. Thus answer = |f|.
Thus done.
I'll write solution in Python.
Complexities: O(n) time, O(1) space.
Now produce final answer. ```python
import sys
def solve():
data = sys.stdin.read().strip().split()
if not data:
return
n = int(data[0])
s = data[1].strip()
zeros = s.count('0')
ones = n - zeros # since string length is n
ans = abs(zeros - ones)
print(ans)
if name == "main":
solve()