Given an unsorted array and two elements num1 and num2. The task is to count the number of elements occurs between the given elements (excluding num1 and num2). If there are multiple occurrences of num1 and num2, we need to consider leftmost occurrence of num1 and rightmost occurrence of num2.
 
Example 1:
Input : Arr[] = {4, 2, 1, 10, 6}
num1 = 4 and num2 = 6
Output : 3
Explanation:
We have an array [4, 2, 1, 10, 6] and 
num1 = 4 and num2 = 6. 
So, the left most index of num1 is 0 and 
rightmost index of num2 is 4. 
So, the total number of element between 
them is [2, 1, 10] So, function will return 
3 as an answer.
Example 2:
Input : Arr[] = {3, 2, 1, 4}
num1 = 2 and num2 = 4
Output : 1
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function getCount() that takes an array (arr), sizeOfArray (n), integer num1, integer num2, and return the number of elements between these numbers. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
2 ≤ N ≤ 10^{5}
1 ≤ A[i], num1, num2 ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Data structures

Starter code: #User function Template for python3

class Solution:
    def getCount(self,arr, n, num1, num2): 
        #Comlete the function

Solutions:
class Solution:

	def getCount(self, arr, n, num1, num2):
		l = arr.index(num1)
		r = len(arr) - 1 - arr[::-1].index(num2)
		count = 0
		for i in range(l + 1, r):
			count += 1
		return count


class Solution:

	def getCount(self, arr, n, num1, num2):
		index1 = 0
		index2 = 0
		for i in range(n):
			if arr[i] == num1:
				index1 = i
				break
		for i in range(n):
			if arr[i] == num2:
				index2 = i
		if index2 == index1:
			return 0
		return index2 - index1 - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		i = arr.index(num1)
		j = arr[::-1].index(num2)
		result = n - 2 - i - j
		return max(0, result)


class Solution:

	def getCount(self, arr, n, num1, num2):
		return len(arr[arr.index(num1) + 1:len(arr) - arr[::-1].index(num2) - 1])


class Solution:

	def getCount(self, arr, n, num1, num2):
		i = 0
		first = 0
		last = 0
		while i < n:
			if arr[i] == num1:
				first = i
				break
			else:
				i += 1
		j = n - 1
		while j >= 0:
			if arr[j] == num2:
				last = j
				break
			else:
				j -= 1
		if first == last:
			return 0
		if last != 0 and first != last:
			return last - first - 1
		else:
			return 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		a = arr.index(num1)
		b = arr[::-1].index(num2)
		res = n - 2 - a - b
		return max(0, res)


class Solution:

	def getCount(self, arr, n, num1, num2):
		left = arr.index(num1)
		right = n - 1
		i = n - 1
		while i >= 0:
			if arr[i] == num2:
				right = i
				break
			i -= 1
		return len(arr[left + 1:right])


class Solution:

	def getCount(self, arr, n, num1, num2):
		ind = dict.fromkeys(arr, 0)
		for i in range(n):
			ind[arr[i]] = i
		count = 0
		for i in range(arr.index(num1) + 1, ind[num2]):
			count += 1
		return count


class Solution:

	def getCount(self, arr, n, num1, num2):
		count = 0
		i = 0
		for i in range(n):
			if arr[i] == num1:
				m = i
				break
		for i in range(n):
			if arr[i] == num2:
				n = i
		if m > n:
			return m - n - 1
		elif m == n:
			return 0
		else:
			return n - m - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		f = 0
		l = 0
		for i in range(n):
			if arr[i] == num1:
				f = i
				break
		for i in range(n - 1, -1, -1):
			if arr[i] == num2:
				l = i
				break
		if l == f:
			return 0
		else:
			return l - f - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		if num1 == num2 and arr.count(num1) == 1:
			return 0
		echiga = arr.index(num1) + 1
		niiga = n - arr[::-1].index(num2) - 1
		return niiga - echiga


class Solution:

	def getCount(self, arr, n, num1, num2):
		l = -1
		r = -1
		for i in range(n):
			if arr[i] == num1 and l == -1:
				l = i
			elif arr[i] == num2:
				r = i
		if l == -1 or r == -1:
			return 0
		return r - l - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		start = 0
		end = n - 1
		while start < n:
			if arr[start] == num1:
				num1_idx = start
				break
			start += 1
		while end > -1:
			if arr[end] == num2:
				num2_idx = end
				break
			end -= 1
		if num2_idx > num1_idx:
			return num2_idx - num1_idx - 1
		else:
			return 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		i1 = 0
		i2 = 0
		for (i, v) in enumerate(arr):
			if v == num1:
				i1 = i
				break
		for i in range(-1, -n, -1):
			if arr[i] == num2:
				i2 = n + i
				break
		return len(arr[i1 + 1:i2])


class Solution:

	def getCount(self, arr, n, num1, num2):
		num1_index = -1
		num2_index = -1
		for index in range(n):
			elem = arr[index]
			if elem == num1 and num1_index == -1:
				num1_index = index
			if elem == num2:
				num2_index = index
		return len(arr[num1_index + 1:num2_index])


class Solution:

	def getCount(self, arr, n, num1, num2):
		num_index1 = arr.index(num1)
		num = arr.count(num2)
		if num == 1:
			num_index2 = arr.index(num2)
		else:
			arr.reverse()
			num_index2 = arr.index(num2)
			num_index2 = n - 1 - num_index2
			arr.reverse
		list_slice = arr[num_index1:num_index2]
		if len(list_slice) == 0:
			return 0
		return len(list_slice) - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		a = arr.index(num1)
		b = 0
		for i in range(n - 1, 0, -1):
			if arr[i] == num2:
				b = i
				break
		c = 0
		for i in range(a + 1, b):
			c += 1
		return c


class Solution:

	def getCount(self, arr, n, num1, num2):
		index_num1 = -1
		index_num2 = -1
		for i in range(n):
			if num1 == arr[i]:
				index_num1 = i
				break
		for i in range(n - 1, -1, -1):
			if num2 == arr[i]:
				index_num2 = i
				break
		if index_num1 == -1 or index_num2 == -1:
			return 0
		if index_num1 == index_num2:
			return 0
		return abs(index_num1 - index_num2) - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		c = 0
		if num1 in arr and num2 in arr:
			for i in range(n):
				if arr[i] == num1 and c < 1:
					c = i + 1
				if arr[i] == num2:
					d = i
			if d == c - 1:
				return 0
			else:
				return d - c
		else:
			return 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		if num1 == num2 and arr.count(num1) == 1:
			return 0
		return n - arr.index(num1) - arr[::-1].index(num2) - 2


class Solution:

	def getCount(self, arr, n, num1, num2):
		pos1 = 0
		pos2 = n - 1
		i = n - 1
		vis1 = 1
		vis2 = 1
		pos1 = arr.index(num1)
		while i >= 0:
			if arr[i] == num2:
				pos2 = i
				break
			i -= 1
		return len(arr[pos1 + 1:pos2])


class Solution:

	def getCount(self, arr, n, num1, num2):
		arr1 = arr[::-1]
		x1 = arr.index(num1)
		x2 = n - arr1.index(num2) - 1
		if abs(x2 - x1) - 1 == -1:
			return 0
		return abs(x2 - x1) - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		l = 0
		r = len(arr) - 1
		ip = 0
		ep = 0
		while l <= r:
			if arr[l] == num1:
				ip = l
				break
			l += 1
		while l <= r:
			if arr[r] == num2:
				ep = r
				break
			r -= 1
		if ep == ip:
			return 0
		else:
			return ep - ip - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		dic = {}
		for i in range(n):
			if arr[i] not in dic:
				dic[arr[i]] = [i]
			else:
				dic[arr[i]] += [i]
		if num1 == num2 and len(dic[num1]) == 1:
			return 0
		k = dic[num2][-1] - 1 - dic[num1][0]
		if k > 0:
			return k
		return 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		(x, y) = (None, None)
		for i in range(n):
			if arr[i] == num1:
				x = i
				break
		for i in range(n - 1, -1, -1):
			if arr[i] == num2:
				y = i
				break
		if x == y:
			return 0
		return y - x - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		count = 0
		for i in range(n):
			if arr[i] == num1:
				left = i
				break
		for i in range(n - 1, -1, -1):
			if arr[i] == num2:
				right = i
				break
		for j in range(left + 1, right):
			count += 1
		return count


class Solution:

	def getCount(self, arr, n, num1, num2):
		count = 0
		for i in range(n):
			if arr[i] == num1:
				leftind = i
				break
		for i in range(n - 1, -1, -1):
			if arr[i] == num2:
				rightind = i
				break
		for j in range(leftind + 1, rightind):
			count += 1
		return count


class Solution:

	def getCount(self, arr, n, num1, num2):
		l = arr.index(num1)
		r = -1
		for j in range(n):
			if arr[j] == num2:
				r = j
		if l != r:
			return r - l - 1
		else:
			return l - r


class Solution:

	def getCount(self, arr, n, num1, num2):
		l = arr.index(num1)
		r = n - 1 - arr[::-1].index(num2)
		if l != r:
			return r - l - 1
		else:
			return l - r


class Solution:

	def getCount(self, arr, n, num1, num2):
		lnum = 0
		rnum = 0
		for i in range(n):
			if arr[i] == num1:
				lnum = i
				break
		for j in range(n - 1, -1, -1):
			if arr[j] == num2:
				rnum = j
				break
		if lnum == rnum:
			return 0
		else:
			return rnum - lnum - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		count = 0
		flag = True
		for i in range(n):
			if arr[i] == num1 and flag:
				s = i
				flag = False
			if arr[i] == num2:
				f = i
		if f > s:
			return f - s - 1
		else:
			return 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		d = {}
		pos1 = 0
		pos2 = 0
		for i in range(n):
			try:
				d[arr[i]].append(i)
			except:
				d[arr[i]] = [i]
		for key in list(d.keys()):
			if key == num1:
				n1 = len(d[num1])
				pos1 += d[num1][0]
			if key == num2:
				n2 = len(d[num2])
				pos2 += d[num2][n2 - 1]
		if pos1 == pos2:
			return 0
		else:
			return abs(pos1 - pos2) - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		n1 = arr.index(num1)
		n2 = n - arr[::-1].index(num2)
		if n2 - n1 - 2 == -1:
			return 0
		else:
			return n2 - n1 - 2


class Solution:

	def getCount(self, arr, n, num1, num2):
		a = arr.index(num1)
		list1 = []
		for (i, e) in enumerate(arr):
			if e == num2:
				list1.append(i)
		b = list1[-1]
		return len(arr[a + 1:b])


class Solution:

	def getCount(self, arr, n, num1, num2):
		i = arr.index(num1)
		arr.reverse()
		j = arr.index(num2)
		result = n - 2 - i - j
		return result if result > 0 else 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		i = 0
		j = n - 1
		while i < j:
			if arr[i] == num1:
				if arr[j] == num2:
					return j - i - 1
				else:
					j -= 1
			else:
				i += 1
		return 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		mi = -1
		ma = n
		for i in range(n):
			if arr[i] == num1 and mi == -1:
				mi = i
			if arr[n - 1 - i] == num2 and ma == n:
				ma = n - 1 - i
		return max(0, ma - mi - 1)


class Solution:

	def getCount(self, arr, n, num1, num2):
		l = 0
		r = n - 1
		while l < r:
			if arr[l] == num1 and arr[r] == num2:
				return r - l - 1
			if arr[l] != num1:
				l += 1
			if arr[r] != num2:
				r -= 1
		return 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		(lft, rht) = ([], [])
		for i in range(n):
			if arr[i] == num1:
				lft.append(i)
			if arr[i] == num2:
				rht.append(i)
		return rht[-1] - lft[0] - 1 if rht[-1] != lft[0] else 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		l = []
		for i in range(n):
			if arr[i] == num1:
				l.append(i)
			if arr[i] == num2:
				l1 = i
		s = l1 - min(l) - 1
		if s == -1:
			return 0
		return s


class Solution:

	def getCount(self, arr, n, num1, num2):
		k = arr.index(num1)
		for i in range(n - 1, -1, -1):
			if arr[i] == num2:
				h = i
				break
		l = len(arr[k + 1:h])
		return l


class Solution:

	def getCount(self, arr, n, num1, num2):
		l = 0
		r = n - 1
		count = 0
		while l < r:
			if arr[l] != num1:
				l += 1
			if arr[r] != num2:
				r -= 1
			if arr[l] == num1 and arr[r] == num2:
				break
		for i in range(n):
			if i < r and i > l:
				count += 1
		return count


class Solution:

	def getCount(self, arr, n, num1, num2):
		flag = True
		num_ind = 0
		num_ind1 = 0
		for (i, v) in enumerate(arr):
			if v == num1 and flag:
				num_ind = i
				flag = False
			if v == num2:
				num_ind1 = i
		return num_ind1 - num_ind - 1 if num_ind < num_ind1 else 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		l = 0
		r = 0
		for i in range(0, len(arr)):
			if arr[i] == num1:
				l = i
				break
		for i in range(n - 1, -1, -1):
			if arr[i] == num2:
				r = i
				break
		if l == r:
			return 0
		return r - l - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		num1ind = arr.index(num1)
		num2index = 0
		count = 0
		for i in range(0, n, 1):
			if arr[i] == num2:
				num2ind = i
		for i in range(num1ind, num2ind - 1, 1):
			count += 1
		return count
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	(num1, num2) = map(int, input().strip().split())
	ob = Solution()
	v = ob.getCount(arr, n, num1, num2)
	print(v)


class Solution:

	def getCount(self, arr, n, num1, num2):
		(x, y, cnt) = (0, 0, 0)
		if arr.count(num1) == 1 and arr.count(num2) == 1:
			x = arr.index(num1)
			y = arr.index(num2)
			for x in range(x + 1, y):
				cnt += 1
			return cnt
		for i in range(len(arr)):
			if num1 == arr[i]:
				x = i
				break
		for j in range(len(arr) - 1, -1, -1):
			if num2 == arr[j]:
				y = j
				break
		for i in range(x + 1, y):
			cnt += 1
		return cnt


class Solution:

	def getCount(self, arr, n, num1, num2):
		m = 0
		ind1 = arr.index(num1)
		for i in range(n):
			if arr[i] == num2:
				m = i
		a = arr[ind1 + 1:m]
		return len(a)


class Solution:

	def getCount(self, arr, n, num1, num2):
		l1 = arr.copy()
		l1 = l1[::-1]
		a = arr.index(num1)
		b = l1.index(num2)
		b = len(l1) - 1 - b
		l = arr[a + 1:b]
		return len(l)


class Solution:
	from typing import List

	@staticmethod
	def getCount(arr: List[int], n: int, num1: int, num2: int) -> int:
		(index1, index2) = (None, None)
		for i in range(n):
			if arr[i] == num1:
				index1 = i
				break
		for i in range(n - 1, -1, -1):
			if arr[i] == num2:
				index2 = i
				break
		count = 0
		for _ in range(index1 + 1, index2):
			count += 1
		return count


class Solution:

	def getCount(self, arr, n, num1, num2):
		rightIdx = -1
		leftIdx = -1
		for i in range(n):
			if arr[i] == num1:
				leftIdx = i
				break
		for i in range(n):
			if arr[n - 1 - i] == num2:
				rightIdx = n - 1 - i
				break
		result = 0
		if rightIdx != leftIdx:
			result = rightIdx - leftIdx - 1
		return result


class Solution:

	def getCount(self, arr, n, num1, num2):
		n1 = arr.index(num1)
		n2 = 0
		for i in range(n - 1, 0, -1):
			if arr[i] == num2:
				n2 = i
				break
		if n1 == n2:
			return 0
		return n2 - n1 - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		flag1 = True
		for i in range(n):
			if arr[i] == num1 and flag1:
				num1_ind = i
				flag1 = False
			if arr[i] == num2:
				num2_ind = i
		return num2_ind - num1_ind - 1 if num1_ind < num2_ind else 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		counter = None
		latest_ans = 0
		for element in arr:
			if element == num1 and counter == None:
				counter = 0
				continue
			elif element == num2:
				latest_ans = counter
			if counter != None:
				counter += 1
		return latest_ans


class Solution:

	def getCount(self, arr, n, num1, num2):
		start = 0
		end = 0
		for i in range(n):
			if arr[i] == num1:
				start = i
				break
		for j in range(n):
			if arr[j] == num2:
				end = j
		count = 0
		for i in range(start + 1, end):
			count = count + 1
		return count


class Solution:

	def FindIndex(Self, arr, n, number, Num):
		if Num == 'Num1':
			for i in range(n):
				if arr[i] == number:
					return i
		else:
			for i in range(n - 1, -1, -1):
				if arr[i] == number:
					return i

	def getCount(self, arr, n, num1, num2):
		Num1Index = Solution().FindIndex(arr, n, num1, 'Num1')
		Num2Index = Solution().FindIndex(arr, n, num2, 'Num2')
		if Num1Index == Num2Index:
			return 0
		result = Num2Index - Num1Index - 1
		return result


class Solution:

	def getCount(self, arr, n, num1, num2):
		i = 0
		j = n - 1
		num1_index = -1
		num2_index = -1
		while i < n and j > 0:
			if arr[i] == num1 and num1_index < 0:
				num1_index = i
			if arr[j] == num2 and num2_index < 0:
				num2_index = j
			if num1_index >= 0 and num2_index >= 0:
				break
			i += 1
			j -= 1
		if num1_index == num2_index:
			return 0
		return abs(num1_index - num2_index) - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		index = [-1, -1]
		c = 0
		for (i, j) in enumerate(arr):
			if j == num1 and index[0] == -1:
				index[0] = i
			if j == num2:
				index[1] = i
		k = len(index)
		c = max(index) - min(index)
		if k >= 2 and c != 0:
			return c - 1
		return 0


class Solution:

	def getCount(self, arr, n, num1, num2):
		(small, big) = (n, 0)
		c = []
		for i in range(n):
			if arr[i] == num1:
				if small > i:
					small = i
			if arr[i] == num2:
				if big < i:
					big = i
		if big == small:
			diff = 0
		else:
			diff = big - small - 1
		return diff


class Solution:

	def getCount(self, arr, n, num1, num2):
		i1 = arr.index(num1)
		arr.reverse()
		i2 = n - 1 - arr.index(num2)
		if i1 == i2:
			return 0
		return i2 - i1 - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		n2 = 0
		for (i, ele) in enumerate(arr):
			if ele == num2:
				n2 = max(n2, i)
		t = arr[arr.index(num1) + 1:n2]
		return len(t)


class Solution:

	def getCount(self, arr, n, num1, num2):
		index1 = arr.index(num1)
		arr.reverse()
		index2 = n - 1 - arr.index(num2)
		if index1 == index2:
			return 0
		return index2 - index1 - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		j = 0
		while j < n:
			if arr[j] == num1:
				break
			j += 1
		k = n - 1
		while k >= 0:
			if arr[k] == num2:
				break
			k -= 1
		if j == k:
			return 0
		return k - j - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		first_index = 0
		last_index = 0
		for i in range(n):
			if arr[i] == num1:
				first_index = i
				break
		for i in range(n - 1, first_index - 1, -1):
			if arr[i] == num2:
				last_index = i
				break
		if abs(first_index - last_index) - 1 == -1:
			return 0
		else:
			return abs(first_index - last_index) - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		i1 = arr.index(num1)
		arr2 = arr[::-1]
		i2 = arr2.index(num2)
		if n - i2 - 1 - i1 - 1 < 0:
			return 0
		else:
			return n - i2 - 1 - i1 - 1


class Solution:

	def getCount(self, arr, n, num1, num2):
		for i in range(n):
			if arr[i] == num1:
				left = i
				break
		for i in range(n - 1, -1, -1):
			if arr[i] == num2:
				right = i
				break
		if left == right:
			return 0
		return right - left - 1
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	(num1, num2) = map(int, input().strip().split())
	ob = Solution()
	v = ob.getCount(arr, n, num1, num2)
	print(v)


class Solution:

	def getCount(self, arr, n, num1, num2):
		minpos = 9999
		maxpos = -1
		for i in range(n):
			if arr[i] == num1:
				minpos = min(minpos, i)
			if arr[i] == num2:
				maxpos = max(maxpos, i)
		count = maxpos - minpos - 1
		if count < 1:
			return 0
		return count
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	(num1, num2) = map(int, input().strip().split())
	ob = Solution()
	v = ob.getCount(arr, n, num1, num2)
	print(v)