You have a binary string S of length N. In one operation you can select a substring of S and reverse it. For example, on reversing the substring S[2,4] for S=11000, we change 1 \textcolor{red}{100} 0 \rightarrow 1 \textcolor{red}{001} 0.

Find the minimum number of operations required to sort this binary string.  
It can be proven that the string can always be sorted using the above operation finite number of times.

------ Input Format ------ 

- The first line of input will contain a single integer T, denoting the number of test cases.
- Each test case consists of 2 lines of input.
- The first line of each test case contains a single integer N — the length of the binary string.
- The second line of each test case contains a binary string S of length N.

------ Output Format ------ 

For each test case, output on a new line — the minimum number of operations required to sort the binary string.

------ Constraints ------ 

$1 ≤ T ≤ 2\cdot 10^{5}$
$1 ≤ N ≤ 2\cdot 10^{5}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
- String $S$ consists of only '$0$'s and '$1$'s.

----- Sample Input 1 ------ 
4
3
000
4
1001
4
1010
6
010101
----- Sample Output 1 ------ 
0
1
2
2
----- explanation 1 ------ 
Test case $1$: The string is already sorted, hence, zero operations are required to sort it. 

Test case $2$: We can sort the string in the following way:  $\textcolor{red}{100} 1$ $\rightarrow$ $0011$. 

Test case $3$: We can sort the string in the following way:  
$1 \textcolor{red}{01} 0$ $\rightarrow$ $\textcolor{red}{1100}$ $\rightarrow$ $0011$.  
It can be proven that this string cannot be sorted in less than $2$ operations.

Test case $4$: We can sort the string in the following way:  
$0 \textcolor{red}{1010}1$ $\rightarrow$ $00 \textcolor{red}{10}11$ $\rightarrow$ $000111$.  
It can be proven that this string cannot be sorted in less than $2$ operations.

Source: codechef | Difficulty: EASY
Tags: String algorithms

Data structures


Solutions:
import math
t = int(input())
for i in range(t):
	a = int(input())
	b = input()
	print(b.count('10'))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	count = 0
	for i in range(n - 1):
		if s[i] == '1' and s[i + 1] == '0':
			count += 1
	print(count)


for _ in range(int(input())):
	n = int(input())
	s = input()
	print(s.count('10'))


for i in range(int(input())):
	n = int(input())
	s = input()
	print(s.count('10'))


for i in range(int(input())):
	n = int(input())
	x = input()
	print(x.count('10'))


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	count = 0
	s1 = s.count('0')
	s2 = s.count('1')
	if s1 == n or s2 == n:
		print(0)
	else:
		for i in range(1, len(s)):
			if s[i - 1] <= s[i]:
				pass
			else:
				count = count + 1
		print(count)


for i in range(int(input())):
	n = int(input())
	a = input()
	print(a.count('10'))


for _ in range(int(input())):
	n = int(input())
	str = input()
	print(str.count('10'))


for t in range(int(input())):
	n = int(input())
	s = input()
	c = 0
	for i in range(1, len(s)):
		if s[i - 1] <= s[i]:
			pass
		else:
			c = c + 1
	print(c)


s = int(input())
for i in range(s):
	a = int(input())
	b = input()
	count = 0
	for j in range(a - 1):
		if b[j] == '1' and b[j + 1] == '0':
			count += 1
	print(count)


n = int(input())
for i in range(n):
	n1 = int(input())
	s = input()
	k = s.count('10')
	print(k)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	count = 0
	con = 0
	for j in range(n - 1, -1, -1):
		if con == 0 and s[j] == '0':
			count += 1
			con = 1
		elif con == 1 and s[j] == '1':
			con = 0
	if s[0] == '0':
		count -= 1
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	c = 0
	for j in range(0, n - 1):
		if s[j] == '1' and s[j + 1] == '0':
			c = c + 1
	print(c)


for i in range(int(input())):
	N = int(input())
	s = input()
	print(s.count('10'))


for t in range(int(input())):
	n = int(input())
	s = input()
	count = 0
	(x, y) = ('0', '1')
	i = 0
	j = len(s) - 1
	while i < j:
		while i < len(s) and s[i] == x:
			i += 1
		while j >= 0 and s[j] == y:
			j -= 1
		if j - i > 0:
			count += 1
		(x, y) = (y, x)
	print(count)


for i in range(int(input())):
	n = int(input())
	n1 = input()
	print(n1.count('10'))


for i in range(int(input())):
	N = int(input())
	BS = input()
	print(BS.count('10'))


from collections import Counter
t = int(input())
for i in range(t):
	n = int(input())
	st = input()
	print(st.count('10'))


for t in range(int(input())):
	n = int(input())
	s = input()
	print(s.count('10'))


from collections import Counter

def test():
	n = int(input())
	s = input()
	print(s.count('10'))
t = int(input())
for _ in range(t):
	test()


n = int(input())
for i in range(n):
	s = int(input())
	t = input()
	if '10' in t:
		print(t.count('10'))
	else:
		print('0')


for _ in range(int(input())):
	n = int(input())
	s = list(input())
	count = 0
	lZero = len(s) - 1
	fUnit = 0
	f = 0
	g = 1
	while lZero > fUnit and fUnit < lZero:
		while lZero > fUnit and s[lZero] == str(g):
			lZero -= 1
		while fUnit < lZero and s[fUnit] == str(f):
			fUnit += 1
		if lZero != fUnit:
			count += 1
		(f, g) = (g, f)
	print(count)


goals = int(input())
for distractions in range(goals):
	wife = int(input())
	life = input()
	print(life.count('10'))


n = int(input())
for i in range(n):
	a = int(input())
	b = input()[:a]
	c = b.count('10')
	print(c)


for _ in range(int(input())):
	n = int(input())
	l = input()
	print(l.count('10'))


T = int(input())
for i in range(T):
	n = int(input())
	S = input()
	print(S.count('10'))


for _ in range(int(input())):
	n = int(input())
	a = input()
	print(a.count('10'))


t = int(input())
for _ in range(t):
	n = int(input())
	l = list(input())
	cnt = 0
	for i in range(n - 1):
		if l[i] == '1' and l[i + 1] == '0':
			cnt += 1
	print(cnt)


cases = int(input())
for _ in range(cases):
	n = int(input())
	string = input()
	print(string.count('10'))


test = int(input())
for i in range(test):
	n = int(input())
	s = input()
	a = s.count('10')
	print(a)


for _ in range(int(input())):
	N = int(input())
	s = input()
	lis = list(s)
	count = 0
	for i in range(1, N):
		if s[i - 1] == '1' and s[i] == '0':
			count += 1
	print(count)


for i in range(int(input())):
	N = int(input())
	S = input()
	S = S + '1'
	l = []
	for j in range(N + 1):
		if S[j] == '1':
			l.append(j)
	c = 0
	for j in range(len(l) - 1):
		if l[j + 1] - l[j] > 1:
			c += 1
	print(c)


for i in range(int(input())):
	n = int(input())
	s = input()
	c = 0
	l = list(s)
	for i in range(n - 1):
		if l[i] > l[i + 1]:
			c += 1
	print(c)


for i in range(int(input())):
	ip = int(input())
	a = input()
	print(a.count('10'))


def string(x):
	c = 0
	for j in range(len(x) - 1):
		if x[j] == '1' and x[j + 1] == '0':
			c = c + 1
	return c
T = int(input())
for i in range(T):
	y = int(input())
	x = input()
	print(string(x))