Imagine an imaginary array of length N containing balls. Given 2 arrays color and radius of length N each, where color[i] represents the color of the ith ball while radius[i] represents the radius of ith ball. If two consecutive balls have the same color and size, both are removed from the array. Geek wants to know the length of the final imaginary array.
Example 1:
Input:
N = 3
color[] = {2, 2, 5}
radius[] = {3, 3, 4}
Output: 
1
Explanation: 
First ball and second ball have same color 2 and 
same radius 3.So,after removing only one ball 
is left.It could not be removed from the array. 
Hence, the final array has length 1.
Example 2:
Input:
N = 4
color[] = {1, 3, 3, 1}
radius[] = {2, 5, 5, 2}
Output:
0
Explanation:
Ball 2 and ball 3 have same color 3 and same radius
5.So,they are removed. Now, we have got our
color[]={1, 1} and radius[]={2, 2}.Both the left
balls are consecutive now and they are having
same color and radius. So,these two balls are
removed as well. Now,we are left with empty
final array. Hence, the length of final array is 0.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function finLength(), which takes integer N and array color[ ] and array radius[ ] as input parameters and returns the size of the final imaginary array.
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ color[i] ≤ 10^{9}
1 ≤ radius[i] ≤ 10^{9}^{                                                                                                                                           }

Difficulty: MEDIUM
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(n)
Expected Auxiliary Space: O(n)

Starter code:
from typing import List





class Solution:

    def finLength(self, N : int, color : List[int], radius : List[int]) -> int:

        # code here

        



Solutions:
from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		l = []
		for i in range(N):
			if len(l) > 0 and l[-1][0] == color[i] and (l[-1][1] == radius[i]):
				l.pop()
			else:
				l.append([color[i], radius[i]])
		return len(l)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		s = []
		for i in zip(color, radius):
			if len(s) == 0:
				s.append(i)
			elif s[-1] == i:
				s.pop()
			else:
				s.append(i)
		return len(s)


from typing import List

class Solution:

	def finLength(self, n: int, color: List[int], radius: List[int]) -> int:
		s = []
		for i in range(n):
			if len(s) > 0 and s[-1][0] == color[i] and (s[-1][1] == radius[i]):
				while len(s) > 0 and s[-1][0] == color[i] and (s[-1][1] == radius[i]):
					s.pop()
			else:
				s.append((color[i], radius[i]))
		return len(s)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		st = []
		for (x, y) in zip(color, radius):
			if st and st[-1] == [x, y]:
				st.pop()
			else:
				st.append([x, y])
		return len(st)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		S = []
		for (c, r) in zip(color, radius):
			if S and S[-1] == (c, r):
				S.pop()
			else:
				S.append((c, r))
		return len(S)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		st = []
		for i in range(N):
			x = color[i]
			y = radius[i]
			f = 0
			while st and (x, y) == st[-1]:
				st.pop()
				f = 1
			if f == 0:
				st.append((x, y))
		return len(st)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		(i, count) = (0, N)
		s1 = []
		s2 = []
		if N < 2:
			return N
		for i in range(0, N):
			if len(s1) == 0:
				s1.append(color[i])
				s2.append(radius[i])
			elif s1[-1] == color[i] and s2[-1] == radius[i]:
				s1.pop(-1)
				s2.pop(-1)
			else:
				s1.append(color[i])
				s2.append(radius[i])
		return len(s1)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		st1 = []
		st2 = []
		st1.append(color[0])
		st2.append(radius[0])
		count = 0
		for x in range(1, N):
			if len(st1) == 0:
				st1.append(color[x])
				st2.append(radius[x])
				continue
			if color[x] == st1[-1] and radius[x] == st2[-1]:
				st1.pop()
				st2.pop()
			else:
				st1.append(color[x])
				st2.append(radius[x])
		return len(st1)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		remaining_balls = []
		for i in range(len(color)):
			if not remaining_balls or (color[i] != remaining_balls[-1][0] or radius[i] != remaining_balls[-1][1]):
				remaining_balls.append((color[i], radius[i]))
			else:
				remaining_balls.pop()
		return len(remaining_balls)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		i = 0
		j = -1
		stack = []
		while i < N:
			if len(stack) == 0:
				stack.append(i)
				i += 1
				j += 1
			elif color[i] == color[stack[j]] and radius[i] == radius[stack[j]]:
				stack.pop(j)
				i += 1
				j -= 1
			else:
				stack.append(i)
				i += 1
				j += 1
		return len(stack)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		t = 1
		while t == 1:
			t = 0
			c = []
			r = []
			for i in range(len(color)):
				if color[i] != 0:
					c.append(color[i])
					r.append(radius[i])
			color = c
			radius = r
			l = len(color)
			for i in range(len(color) - 1):
				if color[i] == color[i + 1]:
					if radius[i] == radius[i + 1]:
						color[i] = color[i + 1] = 0
						radius[i] = radius[i + 1] = 0
						t = 1
		return len(color)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		s1 = []
		s2 = []
		for i in range(N):
			if s1 and s2 and (color[i] == s1[-1]) and (radius[i] == s2[-1]):
				s1.pop()
				s2.pop()
				continue
			s1.append(color[i])
			s2.append(radius[i])
		return len(s1)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		stack = []
		for i in range(N):
			stack.append((color[i], radius[i]))
		ans = []
		while stack:
			if len(ans) == 0:
				ans.append(stack[-1])
				stack.pop()
			if stack[-1] != ans[-1]:
				ans.append(stack[-1])
				stack.pop()
			else:
				stack.pop()
				ans.pop()
		return len(ans)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		stack = []
		for i in range(N):
			k = color[i]
			l = radius[i]
			if len(stack) == 0:
				stack.append((k, l))
			elif (k, l) != stack[-1]:
				stack.append((k, l))
			elif (k, l) == stack[-1]:
				stack.pop()
		res = len(stack)
		return res


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		stack = []
		stack_1 = []
		ind = 0
		for (i, j) in zip(color, radius):
			if stack == []:
				stack.append(i)
				stack_1.append(j)
				continue
			if i == stack[-1] and j == stack_1[-1]:
				stack.pop()
				stack_1.pop()
			else:
				stack.append(i)
				stack_1.append(j)
		return len(stack)


from typing import List

class Solution:

	def compare_list(self, list1, list2):
		if list1[0] == list2[0] and list1[1] == list2[1]:
			return True
		return False

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		initial_length = len(color)
		stack = [[color[0], radius[0]]]
		stack_top = 0
		for i in range(1, initial_length):
			new_stack = [color[i], radius[i]]
			if stack_top == -1:
				stack.append(new_stack)
				stack_top += 1
				continue
			is_same = self.compare_list(new_stack, stack[stack_top])
			if is_same == True:
				stack.pop()
				stack_top -= 1
				continue
			stack.append(new_stack)
			stack_top += 1
		return stack_top + 1


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		i = 0
		s = [[0, 0, 0]]
		try:
			while i < N:
				if [color[i], radius[i]] == s[-1][:2]:
					del s[-1]
				else:
					s.append([color[i], radius[i], i])
				i += 1
		except:
			return len(s) - 1
		return len(s) - 1


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		l = []
		k = []
		i = 0
		p = 0
		while i < len(color):
			if i == 0 or len(l) == 0:
				l.append(color[i])
				k.append(radius[i])
				i = i + 1
				p = p + 1
			elif l[p - 1] == color[i] and k[p - 1] == radius[i]:
				l.pop()
				k.pop()
				i = i + 1
				p = p - 1
			else:
				l.append(color[i])
				k.append(radius[i])
				i = i + 1
				p = p + 1
		return len(l)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		cc = []
		ss = []
		cc.append(color[0])
		ss.append(radius[0])
		for i in range(1, N):
			if len(cc) == 0 and len(ss) == 0:
				cc.append(color[i])
				ss.append(radius[i])
				continue
			elif color[i] == cc[-1] and radius[i] == ss[-1]:
				cc.pop()
				ss.pop()
			else:
				cc.append(color[i])
				ss.append(radius[i])
		return len(cc)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		(temp, len1) = ([], N)
		for i in range(N):
			if temp and temp[-1] == (color[i], radius[i]):
				temp.pop()
				len1 -= 2
			else:
				temp.append((color[i], radius[i]))
		return len1


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		s = []
		cnt = 0
		for i in range(len(color)):
			t = (radius[i], color[i])
			if s == []:
				s.append(t)
			else:
				t1 = s.pop()
				if t1 == t:
					cnt = cnt + 2
				else:
					s.append(t1)
					s.append(t)
		return N - cnt


from typing import List

class Solution:

	def finLength(self, n: int, color: List[int], radius: List[int]) -> int:
		stack_c = []
		stack_r = []
		for i in range(n):
			if stack_c and color[i] == stack_c[-1] and stack_r and (radius[i] == stack_r[-1]):
				stack_c.pop()
				stack_r.pop()
			else:
				stack_c.append(color[i])
				stack_r.append(radius[i])
		return len(stack_c)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		balls = []
		for i in range(N):
			x = []
			x.append(color[i])
			x.append(radius[i])
			balls.append(x)
		gt = []
		for i in balls:
			if len(gt) == 0:
				gt.append(i)
			elif len(gt) > 0 and gt[-1][0] == i[0] and (gt[-1][1] == i[1]):
				gt.pop()
			else:
				gt.append(i)
		return len(gt)


from typing import List
from collections import deque

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		balls = deque(list(zip(color, radius)))
		arrayChanged = True
		while arrayChanged:
			arrayChanged = False
			newBalls = deque(list())
			while len(balls) > 1:
				firstBall = balls.popleft()
				if firstBall != balls[0]:
					newBalls.append(firstBall)
				else:
					balls.popleft()
					arrayChanged = True
			if len(balls) == 1:
				newBalls.append(balls[0])
			balls = newBalls
		return len(balls)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		i = 1
		while i < len(color):
			if color[i - 1] == color[i] and radius[i - 1] == radius[i]:
				color[i - 1:i + 1] = []
				radius[i - 1:i + 1] = []
				if i > 1:
					i -= 1
				if len(color) == 1:
					return 1
			else:
				i += 1
		return len(color)


from typing import List
from collections import deque

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		stk = deque()
		arr = [(color[i], radius[i]) for i in range(N)]
		for i in arr:
			flag = False
			while stk and stk[-1] == i:
				stk.pop()
				flag = True
			if not flag:
				stk.append(i)
		return len(stk)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		count = 0
		color_stack = []
		radius_stack = []
		top = -1
		for i in range(N):
			if top == -1:
				color_stack.append(color[i])
				radius_stack.append(radius[i])
				top += 1
			elif color_stack[top] == color[i] and radius_stack[top] == radius[i]:
				color_stack.pop()
				radius_stack.pop()
				top -= 1
			else:
				color_stack.append(color[i])
				radius_stack.append(radius[i])
				top += 1
		return len(color_stack)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:

		class Pair:

			def __init__(self, color, radius):
				self.color = color
				self.radius = radius
		stack = []
		for i in range(N):
			if stack:
				if stack[-1].color == color[i] and stack[-1].radius == radius[i]:
					stack.pop()
				else:
					stack.append(Pair(color[i], radius[i]))
			else:
				stack.append(Pair(color[i], radius[i]))
		return len(stack)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		list1 = []
		list2 = []
		top = -1
		i = 0
		while i < N:
			if top == -1:
				list1.append(color[i])
				list2.append(radius[i])
				i += 1
				top += 1
			elif color[i] != list1[top] or radius[i] != list2[top]:
				list1.append(color[i])
				list2.append(radius[i])
				i += 1
				top += 1
			else:
				list1.pop()
				list2.pop()
				top -= 1
				i += 1
		return len(list1)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		new_arr = []
		i = 0
		flag = False
		dup_flag = False
		while True:
			if i < N - 1:
				if color[i] == color[i + 1] and radius[i] == radius[i + 1]:
					dup_flag = True
					i += 2
					continue
				else:
					new_arr.append((color[i], radius[i]))
					i += 1
			elif i == N - 1:
				new_arr.append((color[i], radius[i]))
				flag = True
			else:
				if len(new_arr) == 0:
					break
				flag = True
			if flag == True:
				if dup_flag == False:
					break
				(color, radius) = zip(*new_arr)
				new_arr = []
				N = len(color)
				flag = False
				dup_flag = False
				i = 0
		return len(new_arr)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		l1 = []
		l2 = []
		for i in range(N):
			if l1 == [] or l2 == []:
				l1.append(color[i])
				l2.append(radius[i])
				continue
			if l1[-1] == color[i] and l2[-1] == radius[i]:
				l1.pop()
				l2.pop()
			else:
				l1.append(color[i])
				l2.append(radius[i])
		return len(l1)


from typing import List

class Solution:

	def finLength(self, N: int, color: List[int], radius: List[int]) -> int:
		(temp, len1) = ([], N)
		for i in range(N):
			if temp and temp[-1] == (color[i], radius[i]):
				temp.pop()
				len1 -= 2
			else:
				temp.append((color[i], radius[i]))
		return len1

class IntArray:

	def __init__(self) -> None:
		pass

	def Input(self, n):
		arr = [int(i) for i in input().strip().split()]
		return arr

	def Print(self, arr):
		for i in arr:
			print(i, end=' ')
		print()
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		color = IntArray().Input(N)
		radius = IntArray().Input(N)
		obj = Solution()
		res = obj.finLength(N, color, radius)
		print(res)