Problem:


Score : 700 points

Problem Statement

Given is a sequence of N digits a_1a_2\ldots a_N, where each element is 1, 2, or 3.
Let x_{i,j} defined as follows:

x_{1,j} := a_j \quad (1 \leq j \leq N)

x_{i,j} := | x_{i-1,j} - x_{i-1,j+1} | \quad (2 \leq i \leq N and 1 \leq j \leq N+1-i)

Find x_{N,1}.

Constraints

2 \leq N \leq 10^6

a_i = 1,2,3 (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
a_1a_2\ldotsa_N

Output

Print x_{N,1}.

Sample Input 1

4
1231

Sample Output 1

1

x_{1,1},x_{1,2},x_{1,3},x_{1,4} are respectively 1,2,3,1.

x_{2,1},x_{2,2},x_{2,3} are respectively |1-2| = 1,|2-3| = 1,|3-1| = 2.

x_{3,1},x_{3,2} are respectively |1-1| = 0,|1-2| = 1.

Finally, x_{4,1} = |0-1| = 1, so the answer is 1.

Sample Input 2

10
2311312312

Sample Output 2

0



Sample input:
4
1231


Sample output:
1

Submission status: Runtime Error

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n = int(input())
  a = list(map(int,input()))
  import copy
  for _ in range(n-1):
    b=[0]*(len(a)-1)
    for i in range(len(a)-1):
      b[i]=abs(a[i]-a[i+1])
      #print(b)
    a = copy.deepcopy(b)
    #print(a)

  print(b[0])
if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
moji = input()
a = []
for i in range(N):
    a.append([0]*(N-i))

for j in range(N):
    a[0][j] = int(moji[j])

for i in range(1,N):
    for j in range(N-i):
        a[i][j] = abs(a[i-1][j]-a[i-1][j+1])

print(a[N-1][0])

Submission status: Runtime Error

Solution code:
n = input()
res = list(map(int,input()))
d = len(res)
res2 = []
while d > 1:
    for i in range(len(res) - 1):
        res2.append(abs(res[i] - res[i+1]))
    d -= 1
    res = res2.copy()
    res2.clear()
print(res[0])

Submission status: Runtime Error

Solution code:
def calc(i, j):
    if i == 1:
        return array[j]
    return abs(calc(i-1,j) - calc(i-1,j+1))
def main():
    N = int(input())
    global array
    array = [0 for i in range(N+1)]
    s = input()
    for i in range(N):
        array[i+1] = int(s[i])
    print(calc(N, 1))
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import java.util.Scanner;
import java.lang.Math;

class Scratch {
    public static void main(String[] args) {

        Scanner obj = new Scanner (System.in);
        int n = obj.nextInt();
        int[] a = new int[n];
        String b = obj.next();
//        System.out.println(b.charAt(0));

        for (int i = 0;i<n;i++)
        {
            char p = b.charAt(i);
            int c = Character.getNumericValue(p);
            a[i] = c;
        }

//        for (int i = 0 ;i<n;i++)
//            System.out.println(a[i]);

        int[][] x = new int[n][n];

        for(int i = 0;i<n;i++)
        {
            int j = 0;
            while(i+j <= n-1)
            {
                if(i == 0)
                {
                  x[i][j] = a[j];
                }
                else
                {
                    x[i][j] = Math.abs(x[i-1][j] - x[i-1][j+1]);
                }
                j++;
            }
        }
        System.out.println(x[n-1][0]);

    }
}

Submission status: Runtime Error

Solution code:
import sys
import copy
import math
import fractions

import bisect
import pprint
import bisect
from functools import reduce
from copy import deepcopy
from collections import deque
import numpy as np
from decimal import *
sys.setrecursionlimit(10 ** 6)

def CalcList(list):
    bfnum = -1
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
            afterList.append(now)
        else:
            now = abs(i -bfnum)
            if now >0:
                afterList.append(now)
            bfnum = i
    return afterList

def CheckList(list):
    bfnum = -1
    count = 0
    afterList =[]
    for i in list:
        if bfnum == -1:
            bfnum = i
            afterList.append(i)
        else:
            if i == bfnum:
                count = count +1
                if count < 3:
                    afterList.append(i)
            else:
                afterList.append(i)
    return afterList


if __name__ == '__main__':
    n = int(input())
    s = input()
    list =list(map(lambda x: int(x), s))

    while(len(list) >1):
        list =  CheckList(list)
        list = CalcList(list)

    if len(list) ==0:
        print(0)
    else:
        print(list[0])

Submission status: Runtime Error

Solution code:
N = int(input())
a = input()
if len(N) > 4:
  print(0)
elif len(N) == 2:
  if a == '13' or a == '31':
    print(2)
  elif abs(int(a[0]) - int(a[1])) == 1:
    print(1)
  else:
    print(0)
elif len(N) == 3:
  if abs(abs(int(a[0]) - int(a[1])) - abs(int(a[1]) - int(a[2]))) == 1:
    print(1)
  else:
    print(0)
else:
  t1 = abs(int(a[0]) -int(a[1]))
  t2 = abs(int(a[1]) - int(a[2]))
  t3 = abs(int(a[2]) - int(a[3]))
  s1 = abs(t1 - t2)
  s2 = abs(t2 - t3)
  t = abs(s1-s2)
  if t == 1:
    print(1)
  else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10**9)
N = int(input())
S = list(map(int, list(input())))
# S = list(map(int, input().split()))

@lru_cache(maxsize=None)
def func(i,j):
    if i == 1:
        return S[j]
    return abs(func(i-1,j) - func(i-1, j+1))

def main():
    # print(S)
    print(func(N, 0))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10**10)
N = int(input())
S = list(map(int, list(input())))
# S = list(map(int, input().split()))

@lru_cache(maxsize=None)
def func(i,j):
    if i == 1:
        return S[j]
    return abs(func(i-1,j) - func(i-1, j+1))

def main():
    # print(S)
    print(func(N, 0))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def calc(i, j):
    if i == 0:
        return array[j]
    return abs(calc(i-1,j) - calc(i-1,j+1))
def main():
    N = int(input())
    global array
    array = [0 for i in range(N)]
    s = input()
    for i in range(N):
        array[i] = int(s[i])
    print(calc(N-1, 0))
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
a = int(input())
b = int(input())
 
print(random.randint(0,2))

Submission status: Runtime Error

Solution code:
a = int(input())
b = int(input())

print(random.randint(0,2))

Submission status: Runtime Error

Solution code:
i=input().strip(' ')
n=int(i)
a = input().strip()
def cal(i,j):
    if i==1:
        return int(a[j-1])
    return abs(cal(i-1,j)-cal(i-1,j+1))
print(cal(n,1))

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10**7)
N = int(input())
S = list(map(int, list(input())))
# S = list(map(int, input().split()))

@lru_cache(maxsize=None)
def func(i,j):
    if i == 1:
        return S[j]
    return abs(func(i-1,j) - func(i-1, j+1))

def main():
    # print(S)
    print(func(N, 0))

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
S = map(int, input().split())

a =[]
b =[]
c =[]

for j in range(0, len(S-i-1)):
    a.append(abs(S[j]-S[j+1]))
    
for i in range(1, len(S-2)):
  b = a
  c = a
  a = []
  for j in range(0, len(a-i-1)):
    a.append(abs(b[j]-b[j+1]))
    
print(a)

Submission status: Runtime Error

Solution code:
n=int(input())
S=input()
def aa(i,j):
    if i==1:
        return int(S[j-1])
    else:
        return abs(aa(i-1,j)-aa(i-1,j+1))
print(int(aa(n,0)))

Submission status: Runtime Error

Solution code:
N=int(input())
A=input()
# N = 4
# A = "1231"

def union(x):
  new_x = [x[0]]
  for i in range(1, len(x)):
    if new_x[-1] != x[i]:
      new_x.append(x[i])
  return new_x
X = []
X.append( union([int(a) for a in list(A)]) )
# print(X)

for i in range(2,N+1):
  # print(X)
  # print("i:",i)
  x = []
  for j in range(1,len(X[-1])):
    nx = abs(X[i-2][j-1] - X[i-2][j])
    x.append( nx )
    # print("  j:", j, X[i-2][j-1], X[i-2][j])
  
  if len(x)==1:
    print(x[0])
    break

  X.append( union(x) )
  if len(X[-1])==1:
    print(0)
    break

Submission status: Runtime Error

Solution code:
n = int(input())
x = [[0] * n for i in range(10)]

inp = list(input())
for i in range(n):
    x[0][i] = int(inp[i])

for j in range(1, n):
    for k in range(0, n - j):
        x[j][k] = x[j - 1][k] - x[j - 1][k + 1]
        if x[j][k] < 0:
            x[j][k] *= -1


print(x[n-1][0])

Submission status: Runtime Error

Solution code:

n = int(input())
num_list = list(input())  # n個の数字がリストに格納される
#print(n)
#print(num_list)
#for num in range(1,n-1):
 #   print(num_list[num])
#print(num_list[0])

field = [[-1] * n for i in range(n)]
field[0] = num_list
#print(field)

k = 0
for i in range(0,n-1):
    #print("i = {}".format(i))
    for j in range(0,n-1-k):
        field[i+1][j] = abs(int(field[i][j])-int(field[i][j+1]))
    k = k + 1
    #print(field)
            
print(field[n-1][0])

Submission status: Runtime Error

Solution code:
n = int(input())
A=[]
for i in range(n):
  A.append(int(input()))
def list(L):
  if len(L)==1:
    print(L[0])
  else:
    m = len(L)
    B=[]
    for i in range(m-1):
      B.append(abs(L[i+1]-L[i]))
      list(B)
list(A)

Submission status: Runtime Error

Solution code:
n=int(input())
S=input()
def aa(i,j):
    if i==1:
        return int(S[j-1])
    else:
        return abs(aa(i-1,j)-aa(i-1,j+1))
print(int(aa(n,1)))

Submission status: Runtime Error

Solution code:
n=int(input())
p=int(input())
a=[]
for i in range(n):
    a.append(p%10)
    p=int(p/10)
for i in range(n-1):
    for j in range(n-1-i):
        a[j]=abs(a[j]-a[j+1])
print(a[0])

Submission status: Runtime Error

Solution code:
n=int(input())
p=int(input())
a=[]
for i in range(n):
    a.append(p%10)
    q=int(p/10)
for i in range(n-1):
    for j in range(n-1-i):
        a[j]=abs(a[j]-a[j+1])
print(a[0])

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(int, input())
z = []

for i in range (N-1):
    if i % 2 == 0:
        for j in range(N-i-1):
            x = a[j] - a[j+1]
            z.append(abs(x))
        clear(a)
    else:
        for j in range(N-i-1):
            x = z[j] - z[j+1]
            a.append(abs(x))
        clear(z)
ans = a + z
print(ans)

Submission status: Runtime Error

Solution code:
# Code for B - 123 Triangle
# Use input() to fetch data from STDIN
import numpy as np
N = int(input())
a = np.array(list(str(input())), dtype=np.int)

dp = np.zeros((N, N), dtype=np.int)

dp[0, :] = a

for i in range(1, N):
    # print(0, -i)
    # print(1, -i+1)
    tmp = np.abs(dp[i-1, :N-i] - dp[i-1, 1:N-i+1])
    dp[i, :N-i] = tmp
    # print(dp)

print(dp[-1, 0])

Submission status: Runtime Error

Solution code:
n=int(input())
S=input()
def aa(i,j):
    if i==1:
        return int(S[j-1])
    else:
        return abs(aa(i-1,j)-aa(i-1,j+1))
print(aa(n,1))

Submission status: Runtime Error

Solution code:
n=int(input())
p=int(input())
#print(n)
#print(p)
a=[]
q=p
for i in range(n):
    a.append(q%10)
    q=int(q/10)
b=a
#print(a)
for i in range(n-1):
    for j in range(n-1-i):
        b[j]=abs(b[j]-b[j+1])
print(b[0])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, list(input().strip())))
tri = [A]
for i in range(min(N-1, 9)):
    nx = []
    nw = tri[-1]
    for j in range(1,len(nw)):
        nx.append(abs(nw[j]-nw[j-1]))
    tri.append(nx)
if N < 10:
    print(tri[-1][0])
    exit()

if len(tri[-1]) % 2 == 0:
    tar = tri[-2]
else:
    tar = tri[-1]

if tar[0] ^ tar[1]:
    print(1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
from sys import stdin
N=int(stdin.readline().rstrip())
A = int(stdin.readline().rstrip())
An=[0]*N
for i in range(N):
  An[i]=A%10
  A=round(A/10)
for i in range(N-1):
  An1 =[0]*(N-1-i)
  for x in range(N-i-1):
    An1[x] = abs(An[x]-An[x+1])
  An = An1
print(An[0])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, list(input().strip())))
tri = [A]
for i in range(min(N-1, 10)):
    nx = []
    nw = tri[-1]
    for j in range(1,len(nw)):
        nx.append(abs(nw[j]-nw[j-1]))
    tri.append(nx)
if N < 10:
    print(tri[-1][0])
    exit()

if len(tri[-1]) % 2 == 0:
    tar = tri[-2]
else:
    tar = tri[-1]

if tar[0] ^ tar[1]:
    print(1)
else:
    print(0)

Submission status: Runtime Error

Solution code:
ef func(l):
    if len(l)==1:
        return l[0]
    else:
        l1=[]
        for i in range(len(l)-1):
            l1.append(abs(l[i]-l[i+1]))
        return func(l1)
n=int(input())
n1=int(input())
l=[]
while n1>0:
    l.append(n1%10)
    n1//=10
print(func(l[::-1]))

Submission status: Runtime Error

Solution code:
n = int(input())

A = list(str(int(input())))
A = [int(s) for s in A]
def x(a,b):
  if a == 1:
    return A[b]
  else:
    return abs(x(a-1,b) - x(a-1,b+1))

print(x(n,0))

Submission status: Runtime Error

Solution code:
import sys
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import numpy as np

n = ni()
a = list(map(int, list(ns())))
b = np.array(a)

for i in range(n-1):
    b = np.abs(b[1:]-b[:-1])

print(b[0])

Submission status: Runtime Error

Solution code:
# n = input()
# s = raw_input()
import sys
sys.setrecursionlimit(10**9)
# n = 1000
# s = ''.join(["1231" for i in xrange(250)])
n = input()
s = raw_input()
def memoize(f):
    memory = {}

    def inner(i, j):
        if (i, j) not in memory:
            memory[(i, j)] = f(i, j)
        return memory[(i, j)]
    return inner


@memoize
def valueOf(i, j):
    if i == 1:
        return int(s[j - 1])
    else:
        return abs(valueOf(i - 1, j) - valueOf(i - 1, j + 1))


print valueOf(n, 1)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**18)
# n = 1000
# s = ''.join(["1231" for i in xrange(250)])
n = input()
s = raw_input()
def memoize(f):
    memory = {}

    def inner(i, j):
        if (i, j) not in memory:
            memory[(i, j)] = f(i, j)
        return memory[(i, j)]
    return inner


@memoize
def valueOf(i, j):
    if i == 1:
        return int(s[j - 1])
    else:
        return abs(valueOf(i - 1, j) - valueOf(i - 1, j + 1))


print valueOf(n, 1)

Submission status: Runtime Error

Solution code:
N = int(input())
s = input()
num = [int(c) for c in s]

temp = num

def sumlist(x1,x2):
  return abs(x1-x2)

I = N-1
for i in range(I):
  list1 = temp[1:]
  list2 = temp[:-2]
  result = map(sumlist, list1, list2)
  temp = result

print(result[0])

Submission status: Runtime Error

Solution code:
N=int(input())
list=[0]*N
for i in range(N):
    list[i]=[0]*(N-i)
number=str(input())
for i in range(N):
    list[0][i]=number[i]
if N>=2:
    for i in range(1,N):
        for j in range(N-i):
            list[i][j]=abs(int(list[i-1][j])-int(list[i-1][j+1]))
print(list[N-1][0])

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N = int(readline())
A = list(map(int, read().decode().rstrip()))

U = N + 10
fact_ord = [0] * U
for n in range(1,U):
    fact_ord[n] = fact_ord[n//2] + (n//2)

def solve_01(A):
    ret = 0
    N = len(A) - 1
    comb_ord = [fact_ord[N] - fact_ord[i] - fact_ord[N-i] for i in range(N+1)]
    return sum(x * (y==0) for x,y in zip(A, comb_ord)) % 2

def solve(A):
    if len(A) == 1:
        return A[0]
    A = [abs(x - y) for x,y in zip(A, A[1:])]
    if all(x != 1 for x in A):
        B = [x // 2 for x in A]
        return 2 * solve_01(B)
    # 1 が存在
    A = [x % 2 for x in A]
    return solve_01(A)

print(solve(A))

Submission status: Runtime Error

Solution code:
i=int(input())
s=input()
a=""
for j in range(i-1):
  a=""
  for k in range(len(s)-1):
    a += str(abs((int(s[k])-int(s[k+1]))))
    s=a
print(s)

Submission status: Runtime Error

Solution code:
n = input()
s = raw_input()


def memoize(f):
    memory = {}

    def inner(i, j):
        if (i, j) not in memory:
            memory[(i, j)] = f(i, j)
        return memory[(i, j)]
    return inner


@memoize
def valueOf(i, j):
    if i == 1:
        return int(s[j - 1])
    else:
        return abs(valueOf(i - 1, j) - valueOf(i - 1, j + 1))


print valueOf(n, 1)

Submission status: Runtime Error

Solution code:
n = int(input())
a = input()
A=[[0]*n for _ in range(n)]

for i in range(0,n):
  A[0][i]=int(a[i])

for i in range(1,n):
  for j in range(0,n-i):
    A[i][j]=abs(A[i-1][j]-A[i-1][j+1])

print(A[n-1][0])

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
from math import *
n=int(input())
a=[int(x) for x in input().split()]
print(abs(abs(a[0]-a[1])-abs(a[n-1]-a[n-2])))

Submission status: Runtime Error

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math

import itertools

#import bisect

import copy
from functools import lru_cache

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)


def main():
    n=int(input())
    a=list(map(int,input()))
    ans=0
    if n<=5:
        ans=ret(a)
    else:
        for i in range(1,n):
            if a[i]==a[i-1]:
                a[i]=0
                a[i-1]=0
        b=[]
        temp=0
        for i in range(n):
            if a[i]!=0:
                b.append(a[i])
                temp+=1
            if temp>=5:
                break
        ans=ret(b)
    print(ans)

if __name__ == "__main__":

    main()

Submission status: Runtime Error

Solution code:
N=int(input())
list=[0]*N
for i in range(N):
    list[i]=[0]*(N-i)
number=str(input())
for i in range(N):
    list[0][i]=number[i]
# for j in range(N-1):
#     list[1][j]=int(list[1-1][j])-int(list[1-1][j+1])
for i in range(1,N):
    for j in range(N-i):
        list[i][j]=abs(int(list[i-1][j])-int(list[i-1][j+1]))
print(list[N-1][0])

Submission status: Runtime Error

Solution code:
import numpy as np


def array_subtraction(arr):
  if len(arr)==1:
    return arr
  arr = np.array(arr)
  arr = np.abs(arr)
  x = np.array(arr[1:])
  y = np.array(arr[:-1])
  return array_subtraction(x-y)
 

input()
z=[int(i) for i in input()]
print(*array_subtraction(z))

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define put(arr,v) fil(all(arr) , v)
#define precise(num)  cout<<fixed; cout << std::setprecision(num)
#define sort(v) sort(all(v));
#define fill_vec(v,val)  fill(all(v) , val)
#define fill_arr(ar,n,val) fill(ar , ar+n ,val)
#define nl cout<<"\n";
#define input_i(v) generate(all(v),nxt_i);
#define input_s(v) generate(all(v),nxt_s);
#define vst vector<string>
#define vi vector<int>
#define pi pair<int,int>

namespace ohuefmi{
    
    int nxt_i(){
        int x;
        cin>>x;
        return x;
    }
    string nxt_s(){
        string x;
        cin>>x;
        return x;
    }
    void print(vector<int>v){
        for(auto i : v){
            cout<<i<<" ";
        }nl;
    }
     void print(vector<string>v){
        for(auto i : v){
            cout<<i<<" ";
        }nl;
    }
    
    string binary(int n)
    {
        bitset<32>b(n) ;
        string p =b.to_string();
        return p;

    }
    int decimal(string s)
    {
        int res = 0;
        for(int i = 0; i < s.size() ; i++ ){
            if(s[i] == '1')
            res = res + pow(2,s.size() - i -1);
        }
        return res;
    }
    bool isPrime(int n){
        if (n <= 1) return false; if (n <= 3) return true;
    if (n % 2 == 0 || n % 3 == 0) 
        return false;
    for (int i = 5; i * i <= n; i = i + 6) 
        if (n % i == 0 || n % (i + 2) == 0) 
            return false; 
  
    return true; 
    }
    
     int pow (int x, int n,int mod) {//calculate x^n
    if (n == 0) return 1;

      int res = pow (x, n / 2,mod);

    res = (res * res) % mod;
    if (n % 2) res = (res * x ) % mod;

    return res;
    }
    
}

using namespace ohuefmi;

string find(string s)
{
    string q ="";
    for(int i = 0; i< s.size() -1; i++ ){
        
        int num      = abs((s[i+1] - '0') - (s[i] - '0'));
        if(num == 0)
            continue;
        q+=to_string(num);
    }
    
    // cout<<q<<endl;
    return q;
    
}
int32_t main(){
    
    
        ios_base::sync_with_stdio(cin.tie(NULL));
        cout.tie(NULL);
    
        // int tc = 1; 
        int n = nxt_i();

        // while(tc -- ){
        
        
        
        // }  
        
        
        string s;
        cin>>s;
        
       
        
         
                string q = find(s);
                
                    if(q.size()>1)
                      q = find(q);
                // q = find(q);
                
                if(q.size() >1 || q.size() == 0) 
                        cout<<0<<endl;
                else 
                        cout<<q[0]<<endl;
            
            
           
        
      
      
}

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input().rstrip())
aN = deque()
aN.extend([int(x) for x in list(input().rstrip())])
bN = deque()
for i in range(len(aN)-1):
    for j in range(len(aN)-1):
        bN.append(abs(aN[j] - aN[j+1]))
    aN = bN.copy()
    if len(bN) == 1:
        break
    bN.clear()
print(bN[0])

Submission status: Runtime Error

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

N=int(input())
A=list(map(int,list(input().strip())))

def next(A):
    B=[]
    for i in range(1,len(A)):
        B.append(abs(A[i]-A[i-1]))
    return B

A=next(A)
A=next(A)

if len(A)<=5:
    while len(A)>1:
        A=next(A)
    print(A[0])

else:
    if len(A)%2==1:
        A=next(A)

    if sum(A)%2==1:
        print(1)
    else:
        LEN=len(A)

        from collections import Counter
        C=Counter(A)

        if C[1]>0:
            print(0)
        elif C[0]==1 or C[2]==1:
            print(2)
        else:
            print(0)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
numbers = input()
input_arr = np.array([int(i) for i in numbers])

def myfunc(arr):
    if(len(arr)) == 1:
        return arr[0]
    return myfunc(np.abs(arr[1:] - arr[:-1]))
  
  print(myfunc(input_arr))

Submission status: Runtime Error

Solution code:
n = int(input())
an = str(input())

def x(i,j):
    global an;
    if i == 1:
        return int(an[j-1:j])
    else:
        return abs(x(i-1,j) - x(i-1,j+1))

print(x(n,1))

Submission status: Runtime Error

Solution code:

class Solution:
    def solve(self, N, number):
        dp = [[0] * N for _ in range(N)]

        for i, num in enumerate(number):
            dp[0][i] = num

        for i in range(1, N):
            for j in range(N-i):
                dp[i][j] = abs(dp[i-1][j] - dp[i-1][j+1])

        return dp[N-1][0]


sol = Solution()

N = int(input().strip())
number = [int(ch) for ch in input().strip()]

print(sol.solve(N, number))

Submission status: Runtime Error

Solution code:
# # a, b, c= map(int, input().split())
n = int(input())
an = str(input())

# n=4
# an="1231"
# n=10
# an="2311312312"

# print(n)
# print(an[1:2])



def x(i,j):
    global an;
    if i == 1:
        return int(an[j-1:j])
    else:
        return abs(x(i-1,j) - x(i-1,j+1))





# print(x(1,4))
print(x(n,1))

Submission status: Runtime Error

Solution code:
N = int(input())

x = [[0] * N for i in range(N)]

a = [int(i) for i in list(input())]

for j in range(N):
  x[0][j] = a[j]

for i in range(1, N):
  for j in range(N - i):
    x[i][j] = abs(x[i - 1][j] - x[i - 1][j + 1])

print(x[N - 1][0])

Submission status: Runtime Error

Solution code:

n = int(input())
digits = input()
dp = []
for i in range(n):
    dp.append([-1] * n)
for i in range(n):
    dp[0][i] = int(digits[i])
for i in range(1, n):
    for j in range(n - i):
        dp[i][j] = abs(dp[i - 1][j] - dp[i - 1][j + 1])
print(dp[n - 1][0])