qyliang/CodeNanoFix huggingface dataset:
Buggy code:
K = int(input())

ans = 1
X = 7
cnt = K
while ans <= K:
    x = X % K
    if rem == 0:
        print(ans)
        break
    else:
        ans += 1
        X = x*10 + 7 

if rem != 0:
    print(-1)

Fixed code:
K = int(input())

ans = 1
X = 7
cnt = K
while ans <= K:
    x = X % K
    if x == 0:
        print(ans)
        break
    else:
        ans += 1
        X = x*10 + 7 

if x != 0:
    print(-1)
Buggy code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    K = int(input())
    if K % 2 == 0 or K % 5 == 0:
        print(-1)
        return True
    ans = 1
    K1 = K % 10
    t = K
    while t:
        # print(ans)
        for i in range(10):
            if (t + K1 * i) % 10 == 7:
                break
        else:
            ans = -1
            break
        t = t + K * i
        while t % 10 == 7:
            t //= 10
            ans += 1
            
    print(ans)

if __name__ == '__main__':
    main()

Fixed code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**6)
    K = int(input())
    if K % 2 == 0 or K % 5 == 0:
        print(-1)
        return True
    ans = 0
    K1 = K % 10
    t = K
    while t:
        # print(ans)
        for i in range(10):
            if (t + K1 * i) % 10 == 7:
                break
        else:
            ans = -1
            break
        t = t + K * i
        while t % 10 == 7:
            t //= 10
            ans += 1
            
    print(ans)

if __name__ == '__main__':
    main()
Buggy code:
k=int(input())
n='7'
cnt=1
if k%2==0 or k%5==0:
  print(-1)
  exit()
while True:
  if int(n)%k!=0:
    n+='7'
    cnt+=1
  else:
    print(cnt)
    exit()

Fixed code:
k=int(input())
n='7'
cnt=1
if k%2==0 or k%5==0:
  print(-1)
  exit()
while True:
  if int(n)%k!=0:
    n=int(n)%k
    n=str(n)+'7'
    cnt+=1
  else:
    print(cnt)
    exit()
Buggy code:
k=int(input())
mod=0
ans=0
for i in range(1000000):
    ans+=1
    mod=(mod*10+7)%k

    if mod==0:
        break
print(ans if ans!=1000001 else -1)

Fixed code:
k=int(input())
mod=0
ans=0
for i in range(1000000):
    ans+=1
    mod=(mod*10+7)%k

    if mod==0:
        break
print(ans if ans!=1000000 else -1)
Buggy code:
k = int(input())
n = 0

for i in range(1, 10**6+100):
    n = n * 10 + 7
    if n % k == 0:
        print(i)
        exit()

print(-1)


Fixed code:
k = int(input())
n = 0

for i in range(1, 10**6+100):
    n = n * 10 + 7
    n %= k
    if n == 0:
        print(i)
        exit()

print(-1)
Buggy code:
k = int(input())


i = 0;
mod = 0;
while True:
    mod = (mod*10 + 7) % k;
    if mod == 0:
        break;
    i += 1;
    if i == k:
        j = -2
        break;

print(i+1)

Fixed code:
k = int(input())


i = 0;
mod = 0;
while True:
    mod = (mod*10 + 7) % k;
    if mod == 0:
        break;
    i += 1;
    if i == k:
        i = -2
        break;

print(i+1)
Buggy code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().rstrip().split()
def S(): return sys.stdin.readline().rstrip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353


k = I()
now = 0
for i in range(k):
    now = (now * 10 + 7) % k
    if now == 0:
        print(i + 1)
        exit()

Fixed code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().rstrip().split()
def S(): return sys.stdin.readline().rstrip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353


k = I()
now = 0
for i in range(k):
    now = (now * 10 + 7) % k
    if now == 0:
        print(i + 1)
        exit()
print(-1)
Buggy code:
k=int(input())
k *= 9
amari = 63
for i in range(1, 10 ** 6 + 1):
  amari %= k
  if amari == 0:
    print(i)
    break
  else:
    amari = amari * 70 + 63
else:
  print(-1)

Fixed code:
k=int(input())
k *= 9
amari = 63
for i in range(1, 10 ** 6 + 1):
  amari %= k
  if amari == 0:
    print(i)
    break
  else:
    amari = amari * 10 + 63
else:
  print(-1)
Buggy code:
k=int(input())
a=7
if k%2==0 or k%5==0:
	print(-1)
else:
	for i in range(k):
		if a%k==0:
			print(i+1)
			break
		a=a*10+7
	else:
		print(-1)

Fixed code:
k=int(input())
a=7%k
if k%2==0 or k%5==0:
	print(-1)
elif k==1:
	print(1)
else:
	for i in range(k):
		if a==0:
			print(i+1)
			break
		a=(a*10+7)%k
	else:
		print(-1)
Buggy code:
K = int(input())

t=7
for i in range(K):
    if t % K == 0 :
        print(i+1)
        break
    t = 10 * t + 7

else :
    print(-1)
        

Fixed code:
K = int(input())

t=7
for i in range(K):
    if t % K == 0 :
        print(i+1)
        break
    t = (10 * t + 7) % K

else :
    print(-1)
Buggy code:
K = int(input())
a = 7
cnt = 1
for k in range(K):
    if a % K == 0:
        print(cnt)
        exit()
    else:
        cnt += 1
        a = a * 10 + 7
else:
    print("-1")


Fixed code:
K = int(input())
a = 7 % K
cnt = 1
for k in range(K):
    if a % K == 0:
        print(cnt)
        exit()
    else:
        cnt += 1
        a = (a * 10 + 7) % K
else:
    print("-1")
Buggy code:
k = int(input())
x = 7%k
i = 1
for i in range(0,k+1):
    if x==0:
        break    
    x = (x*10+7)%k
    i += 1
    
if x == 0:
    print(i)
else:
    print('-1')

Fixed code:
k = int(input())
x = 7%k
i = 1
for i in range(1,k+1):
    if x==0:
        break    
    x = (x*10+7)%k
    i += 1
    
if x == 0:
    print(i)
else:
    print('-1')
Buggy code:
K = int(input())
if K//2==0:
    print("-1")
else:
    ans = 0
    temp = 0
    check = 0
    for i in range(K):
        ans += 1
        temp *= 10
        temp += 7
        temp %= K
        
        if temp==0:
            check+=1
        
        if check==1:
            break
    print(ans)

Fixed code:
K = int(input())
if K%2==0:
    print("-1")
elif K%5==0:
    print("-1")
else:
    ans = 0
    temp = 0
    check = 0
    for i in range(K):
        ans += 1
        temp *= 10
        temp += 7
        temp %= K
        
        if temp==0:
            check+=1
        
        if check==1:
            break
    print(ans)
Buggy code:
k = int(input())
if k % 2 == 0 or k % 5 == 0:
  print(-1)
else:
  myconst = 7
  cnt = 1
  while True:
    if myconst % k == 0:
      print(cnt)
      break
    else:
      myconst = (10 * myconst) + 7
    cnt += 1
 
      


Fixed code:
k = int(input())
if k % 2 == 0 or k % 5 == 0:
  print(-1)
else:
  myconst = 7 % k
  cnt = 1
  while True:
    if not myconst:
      print(cnt)
      break
    else:
      myconst = ((10 * myconst) + 7) % k
    cnt += 1
Buggy code:
k = int(input())
x = 7
ans = 1
if k%2 == 0 or k%5 ==0:
  ans = -1
elif 7%k == 0:
  ans = 1
else:
  for i in range(k)
    ans +=1
    x = x*10+7
    if x%k == 0:
      break


print(ans)



Fixed code:
k = int(input())
x = 7
ans = 1
if k%2 == 0 or k%5 ==0:
  print(-1)
  exit()
elif 7%k == 0:
  ans = 1

else:
  for i in range (1,k):
    x = (x*10+7)%k

    if x%k == 0:
      ans = i+1
      break

print(ans)
Buggy code:
k = int(input())
i = 0
s = 0
ans = -1

for z in range(k):
    i += 1
    #s = (7 + 10*s) % k
    s = 7 + 10*s
    if s % k != 0:
        s = s % k
    else:
        ans = i

print(i)


Fixed code:
k = int(input())
i = 0
s = 0
ans = -1

for z in range(k):
    i += 1
    s = 7 + 10*s
    if s % k != 0:
        s = s % k
    else:
        ans = i
        break

print(ans)
Buggy code:
k = int(input())
if(k%2==0 or k%5==0):
    print(-1)
    exit()
else:
    i=1
    seven=7%k
    while seven!=0:
        seven = (seven*10+7)%k
        print(seven)
        i+=1
    print(i)

Fixed code:
k = int(input())
if(k%2==0 or k%5==0):
    print(-1)
    exit()
else:
    i=1
    seven=7%k
    while seven!=0:
        seven = (seven*10+7)%k
        i+=1
    print(i)
Buggy code:
K=int(input())
base=7%K
for i in range(K+1):
	if base%K==0:
		print(i)
		exit()
	else:
		base=(base*10+7)%K
print(-1)


Fixed code:
K=int(input())
base=7%K
for i in range(1,K+1):
	if base%K==0:
		print(i)
		exit()
	else:
		base=(base*10+7)%K
print(-1)
Buggy code:
def main(h):
  count = 1
  n = 0
  if i in range (k):
    n = (n * 10 + 7) % k  
    if n == 0:
      break 
      print(count)
      return 0
    count += 1
  print(-1)
  
if __name__ == '__main__':
	k = int(input())
    main(k)

Fixed code:
def main(h):
  count = 1
  n = 0
  for i in range (k):
    n = (n * 10 + 7) % k  
    if n == 0: 
      print(count)
      return 0
    count += 1
  print(-1)
  
if __name__ == '__main__':
  k = int(input())
  main(k)
Buggy code:
k = int(input())
count = 1
n = 0
if k % 2 == 0 or k % 5 == 0:
    print(-1)
while 1:
    n = (n * 10 + 7) % k  
    if n == 0:
      break 
    count += 1
print(count)

Fixed code:
k = int(input())
count = 1
n = 0
if k % 2 == 0 or k % 5 == 0:
    print(-1)
else:
  while 1:
      n = (n * 10 + 7) % k  
      if n == 0:
        break 
      count += 1
  print(count)
Buggy code:
k=int(input())
sevs=7
for x in range(1,k+1):
  if sevs%k==0:
    print(x)
    exit()
  else:
    sev=(10*sevs+7)%k
print('-1')
    

Fixed code:
k=int(input())
sevs=7
for x in range(1,k+1):
  if sevs%k==0:
    print(x)
    exit()
  else:
    sevs=(10*sevs+7)%k
print('-1')
Buggy code:
k = int(input())

ans = -1
memo = 7 % k

for i in range(k):
    if a == 0:
        ans = a+1
        break
    a = (10*k+7) % k
print(ans)


Fixed code:
k = int(input())

ans = -1
memo = 7 % k
a = 7 % k
for i in range(k):
    if a == 0:
        ans = i+1
        break
    a = (10*a+7) % k
print(ans)
Buggy code:
n=int(input())
if n%2==0 or n%5==0:
    print(-1)
else:
    count=1
    flag=0
    num=7
    while flag==0:
        if num%n==0:
            flag=1
        else:
            num=num*10+7
            count=count+1
print(count)



Fixed code:
n=int(input())
if n%2==0 or n%5==0:
    print(-1)
else:
    count=1
    flag=0
    num=7
    mod=7%n
    while flag==0:
        if mod==0:
            flag=1
        else:
            mod=(mod*10+7)%n
            count=count+1
    print(count)
Buggy code:
N=int(input())
c=list(input())
c1=c[::-1]
ans=0

w_index=[i for i, x in enumerate(c) if x=="W"]
r_index=[i for i, x in enumerate(c1) if x=="R"]

for i in range(len(r_index)):
    r_index[i]=N-1-r_index[i]


if "R" in c and "W" in c:
    for i in range(min(len(r_index),len(w_index))):
        w=w_index[ans]
        r=r_index[ans]
        if w-r==1:
             ans=ans-1
             break
        ans+=1

print(ans)

Fixed code:
N=int(input())
c=list(input())
c1=c[::-1]
ans=0

w_index=[i for i, x in enumerate(c) if x=="W"]
r_index=[i for i, x in enumerate(c1) if x=="R"]

for i in range(len(r_index)):
    r_index[i]=N-1-r_index[i]


if "R" in c and "W" in c:
    for i in range(min(len(r_index),len(w_index))):
        w=w_index[ans]
        r=r_index[ans]
        if w-r>0:
             break
        ans+=1

print(ans)
Buggy code:
def main():
  n = int(input())
  li = list(input())
  i = 0
  j = n-1
  ans = 0
  while(i < j):
    while(li[i] == "R" and i < j):
      i += 1
    while(li[j] == "W" and i < j):
      j -= 1
    li[i] = "R"
    li[j] = "W"
    ans += 1
  print(ans)


if __name__ == '__main__':
  main()

Fixed code:
def main():
  n = int(input())
  li = list(input())
  i = 0
  j = n-1
  ans = 0
  while(i < j):
    while(li[i] == "R" and i < j):
      i += 1
    while(li[j] == "W" and i < j):
      j -= 1
    if li[i] == "W" and li[j] == "R":
      li[i] = "R"
      li[j] = "W"
      ans += 1
  print(ans)    

if __name__ == '__main__':
  main()
Buggy code:
n=int(input())
s=input()
l=[0]
for i in range(n):
    l.append(l[i]+(s[i]=="R"))
ans=n
print(l)
for i in range(n+1):
    ans=min(ans,abs(l[i]-i)+abs(l[-1]-l[i]))
    #print(ans,i)
    if i==l[-1]:
        ans=min(ans,abs(l[i]-i))
        #print(ans)
print(ans)

Fixed code:
n=int(input())
s=input()
l=[0]
for i in range(n):
    l.append(l[i]+(s[i]=="R"))
ans=n
#print(l)
for i in range(n+1):
    ans=min(ans,abs(l[i]-i)+abs(l[-1]-l[i]))
    #print(ans,i)
    if i==l[-1]:
        ans=min(ans,abs(l[i]-i))
        #print(ans)
print(ans)
Buggy code:
N = int(input())
S = input()
WPrefix = [0]
RSuffix = [0]
for elem in S:
    if elem == "R":
        RSuffix += 1
for elem in S:
    if elem == "R":
        RSuffix.append(RSuffix[-1] - 1)
        WPrefix.append(WPrefix[-1])
    else:
        RSuffix.append(RSuffix[-1])
        WPrefix.append(WPrefix[-1] + 1)
minSwap = len(S)
for index in range(len(RSuffix)):
    if max(RSuffix[index],WPrefix[index]) < minSwap:
        minSwap = max(RSuffix[index],WPrefix[index])
print(minSwap)


Fixed code:
N = int(input())
S = input()
WPrefix = [0]
RSuffix = [0]
for elem in S:
    if elem == "R":
        RSuffix[0] += 1
for elem in S:
    if elem == "R":
        RSuffix.append(RSuffix[-1] - 1)
        WPrefix.append(WPrefix[-1])
    else:
        RSuffix.append(RSuffix[-1])
        WPrefix.append(WPrefix[-1] + 1)
minSwap = len(S)
for index in range(len(RSuffix)):
    if max(RSuffix[index],WPrefix[index]) < minSwap:
        minSwap = max(RSuffix[index],WPrefix[index])
print(minSwap)
Buggy code:
n = int(input())
s = input()
a = [str(c) for c in s]

def sea(a, z, n):
  while z[0] < n-1:
    if a[z[0]] == 'W':
      break
    z[0] += 1
    
  while z[1] > 0:
    if a[z[1]] == 'R':
      break
    z[1] += 1
  
  return(z)

z = [0, n-1]

z = a(a, z, n)
c = 0
while z[0] < z[1]:
  a[z[0]], a[z[1]] = a[z[1]], a[z[0]]
  c += 1
  z = a(a,z,n)
  
print(c)

Fixed code:
n = int(input())
s = input()
a = [str(c) for c in s]

def sea(a, x, y, n):
  while x < n-1:
    if a[x] == 'W':
      break
    x += 1
    
  while y > 0:
    if a[y] == 'R':
      break
    y -= 1
  
  return[x, y]

z = [0, n-1]

z = sea(a, z[0] ,z[1], n)
c = 0
while z[0] < z[1]:
  a[z[0]], a[z[1]] = a[z[1]], a[z[0]]
  c += 1
  z = sea(a,z[0], z[1],n)
  
print(c)
Buggy code:
from queue import deque
def main():
    R, W = "R", "W"
    N = int(input())
    C = input()

    counter = dict()
    for c in C:
        counter[c] = counter.get(c, 0) + 1
    r = counter[R] 

    _counter = dict()
    for c in C[r:]:
        _counter[c] = _counter.get(c, 0) + 1
    _r = _counter[R]

    ans = _r
    print(ans)
if __name__ == '__main__':
    main()


Fixed code:
from queue import deque
def main():
    R, W = "R", "W"
    N = int(input())
    C = input()

    counter = dict()
    for c in C:
        counter[c] = counter.get(c, 0) + 1
    r = counter.get(R, 0)

    _counter = dict()
    for c in C[r:]:
        _counter[c] = _counter.get(c, 0) + 1
    _r = _counter.get(R, 0)

    ans = _r
    print(ans)
if __name__ == '__main__':
    main()
Buggy code:
balls = list(input())

backindex = len(balls) - 1
frontindex = 0
actions = 0

while frontindex < backindex:
  if balls[frontindex] == 'W':
    while backindex > frontindex:
      if balls[backindex] == 'R':
        balls[frontindex] = 'R'
        balls[backindex] = 'W'
        actions += 1
        break
      backindex -= 1
  frontindex += 1

print(actions)

Fixed code:
n = input()
balls = list(input())

backindex = len(balls) - 1
frontindex = 0
actions = 0

while frontindex < backindex:
  if balls[frontindex] == 'W':
    while backindex > frontindex:
      if balls[backindex] == 'R':
        balls[frontindex] = 'R'
        balls[backindex] = 'W'
        actions += 1
        break
      backindex -= 1
  frontindex += 1

print(actions)
Buggy code:
def func_rfindr(str):
    return str.rfind('R')

def func_lfindw(str):
    return str.find('W')

def func_replace(target, pos, str):
    ret = ""
    if pos != 0:
        ret = target[0:pos]
    ret = ret + str
    if len(target) - 1 != pos:
        ret = ret + target[pos + 1:]
    return ret

N = int(input())
C = input()
ans = 0

posrr = func_rfindr(C)
poslw = func_lfindw(C)

while poslw != -1 and posrr > poslw:
    C = func_replace(C, posrr, "W")
    C = func_replace(C, poslw, "R")
    print(C)
    print(ans)
    ans = ans + 1
    posrr = func_rfindr(C)
    poslw = func_lfindw(C)

print(ans)

Fixed code:
def func_rfindr(str):
    return str.rfind('R')

def func_lfindw(str):
    return str.find('W')

def func_replace(target, pos, str):
    ret = ""
    if pos != 0:
        ret = target[0:pos]
    ret = ret + str
    if len(target) - 1 != pos:
        ret = ret + target[pos + 1:]
    return ret

N = int(input())
C = input()
ans = 0

posrr = func_rfindr(C)
poslw = func_lfindw(C)

while poslw != -1 and posrr != -1 and posrr > poslw:
    #C = func_replace(C, posrr, "W")
    #C = func_replace(C, poslw, "R")
    C = C[poslw + 1:posrr]
    ans = ans + 1
    posrr = func_rfindr(C)
    poslw = func_lfindw(C)

print(ans)
Buggy code:
N = int(input())
S = input()
r_c = 0
ch = 0
for i in S:
  if i == "R":
    r_c += 1
for i in range(r_c):
  if i == "W"
    ch += 1
print(ch)

Fixed code:
N = int(input())
S = input()
r_c = 0
ch = 0
for i in S:
  if i == "R":
    r_c += 1
for i in range(r_c):
  if S[i] == "W":
    ch += 1
print(ch)
Buggy code:
n = int(input())
arr = list(input())
print(arr)
head = 0
tail = n-1
count = 0
while True:
    while arr[head]=='R' and head<tail:
        head+=1
    while arr[tail]=='W' and head<tail:
        tail-=1
    if arr[head]=='W' and arr[tail]=='R':
        count+=1
    head+=1
    tail-=1
    if head>=tail:
        break
print(count)

Fixed code:
n = int(input())
arr = list(input())
head = 0
tail = n-1
count = 0
while True:
    while arr[head]=='R' and head<tail:
        head+=1
    while arr[tail]=='W' and head<tail:
        tail-=1
    if arr[head]=='W' and arr[tail]=='R':
        count+=1
    head+=1
    tail-=1
    if head>=tail:
        break
print(count)
Buggy code:
N = int(input())
c = str(input())

a = 0
b = 0

for i in range(N):
    if s[i] == 'R':
        a = a + 1
ans = max(a,b)
for i in range(N):
    if s[i] == 'R':
        a = a - 1
    else:
        b = b + 1
    now = max(a,b)
    ans = min(ans, now)
print(ans)


Fixed code:
N = int(input())
s = str(input())

a = 0
b = 0

for i in range(N):
    if s[i] == 'R':
        a = a + 1
ans = max(a,b)
for i in range(N):
    if s[i] == 'R':
        a = a - 1
    else:
        b = b + 1
    now = max(a,b)
    ans = min(ans, now)
print(ans)
Buggy code:
N=int(input())
s=input()

ans=0
i,j=0,N-2
while i<j:
    if s[i]=='R':
        i+=1
    elif s[j]=='W':
        j-=1
    else:
        k+=1
        i+=1
        j-=1

print(k)


Fixed code:
N=int(input())
s=input()

ans=0
i,j=0,N-1
while i<j:
    if s[i]=='R':
        i+=1
    elif s[j]=='W':
        j-=1
    else:
        ans+=1
        i+=1
        j-=1

print(ans)
Buggy code:
n=int(input())
s = input()
ans = 0
for i in s:
	if i=="R":
		ans+=1

for i in range(0,ans):
	if i=="R":
		ans-=1

print(ans)

Fixed code:
n=int(input())
s = input()
ans = 0
for i in s:
	if i=="R":
		ans+=1

for i in range(0,ans):
	if s[i]=="R":
		ans-=1

print(ans)
Buggy code:
import sys
input = sys.stdin.readline

N = int(input())
c = input().rstrip()
white = [0] * N
red = [0] * N
if c[0] == 'W':
    white[0] = 1
for i in range(1, N):
    white[i] = white[i-1]
    if c[i] == 'W':
        white[i] += 1
for i in range(1, N)[::-1]:
    red[i-1] = red[i]
    if c[i] == 'R':
        red[i-1] += 1
if white[-1] == 0 or red[0] == 0:
    print(0)
    exit()
for i in range(N):
    if white[i] == red[i]:
        print(i)
        exit()

Fixed code:
import sys
input = sys.stdin.readline

N = int(input())
c = input().rstrip()
white = [0] * N
red = [0] * N
if c[0] == 'W':
    white[0] = 1
for i in range(1, N):
    white[i] = white[i-1]
    if c[i] == 'W':
        white[i] += 1
for i in range(1, N)[::-1]:
    red[i-1] = red[i]
    if c[i] == 'R':
        red[i-1] += 1
if white[-1] == 0 or red[0] == 0:
    print(0)
    exit()
for i in range(N):
    if white[i] == red[i]:
        print(white[i])
        exit()
Buggy code:
n = int(input())
c = input()
lst = []
w = 0
r = 0
for i in range(n):
  if c[i] == 'W':
    w += 1
  elif c[i] == 'R':
    r += 1
ans = max(w, r)

for k in range(n):
  if c[k] == 'W':
    w += 1
  elif c[k] == 'R':
    r -= 1
  cnt = max(w, r)
  if ans > cnt:
    ans = cnt
print(ans)

Fixed code:
n = int(input())
c = input()
lst = []
w = 0
r = 0
for i in range(n):
  if c[i] == 'R':
    r += 1
ans = max(w, r)

for k in range(n):
  if c[k] == 'W':
    w += 1
  elif c[k] == 'R':
    r -= 1
  cnt = max(w, r)
  if ans > cnt:
    ans = cnt
print(ans)
Buggy code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N = int(input())
C = list(input())[:-1]
r = 0
w = 0
for s in C:
    if s == "R":
        r += 1
    elif s == "W":
        w += 1
ans = 0
if w == 0:
    print(0)
else:
    for i in range(r):
        if C[i] == "W":
            ans += 1
print(ans)


Fixed code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N = int(input())
C = list(input())[:-1]
r = 0
w = 0
for s in C:
    if s == "R":
        r += 1
    elif s == "W":
        w += 1
ans = 0
if w == 0 or r == 0:
    print(0)
else:
    for i in range(r):
        if C[i] == "W":
            ans += 1
    print(ans)
Buggy code:
# -*- coding: utf-8 -*-
 
N = int(input())
C = input()

ans = 0
i = -1
j = N
while( True ):
  
  while( i<j ):
    i += 1
    if( C[i] == 'W' ):
      break
  
  while( j>i ):
    j -= 1
    if( C[j] == 'R' ):
      break
  
  if ( i<j ):
    ans += 1
  else:
    break

print(ans)

Fixed code:
# -*- coding: utf-8 -*-
 
N = int(input())
C = input()

ans = 0
i = -1
j = N
while( True ):
  
  i += 1
  while( i<j ):
    if( C[i] == 'W' ):
      break
    else:
      i += 1
  
  j -= 1
  while( j>i ):
    if( C[j] == 'R' ):
      break
    else:
      j -= 1
  
  if ( i<j ):
    ans += 1
  else:
    break

print(ans)