Given a stack of integers of size N, your task is to complete the function pairWiseConsecutive(), that checks whether numbers in the stack are pairwise consecutive or not. The pairs can be increasing or decreasing, and if the stack has an odd number of elements, the element at the top is left out of a pair. The function should retain the original stack content.
Only following standard operations are allowed on stack.
	push(X): Enter a element X on top of stack.
	pop(): Removes top element of the stack.
	empty(): To check if stack is empty.
Input Format:
The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains two lines of input. The first line contains n denoting the number of elements to be inserted into the stack. The second line contains the elements to be inserted into the stack.
Output Format:
For each testcase, in a new line, print "Yes"(without quote) if the elements of the stack is pairwise consecutive, else print "No".
Your Task:
This is a function problem. You only need to complete the function pairWiseConsecutive that takes a stack as an argument and returns true if the stack is found to be pairwise consecutive, else it returns false. The printing is done by the driver code.
Constraints:
1 < =T <= 100
1 < =N <= 10^{3}
Example:
Input:
2
6
1 2 3 4 5 6
5
1 5 3 9 7
Output:
Yes
No
Explanation:
Testcase1: The number of elements are even and they are pairwise consecutive so we print Yes.
Testcase2: The number of elements are odd so we remove the top element and check for pairwise consecutive. It is not so we print No.

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3



# Function to check if elements are 

# pairwise consecutive in stack 

def pairWiseConsecutive(l):

    

    #add code here

Solutions:
def pairWiseConsecutive(l):
	n = len(l)
	if n % 2 != 0:
		l.pop()
		while l:
			a = l.pop()
			b = l.pop()
			if abs(a - b) == 1:
				continue
			else:
				return False
		return True
	else:
		while l:
			a = l.pop()
			b = l.pop()
			if abs(a - b) == 1:
				continue
			else:
				return False
		return True


def getFlag(l):
	flag = True
	while len(l) != 0:
		x = l.pop()
		y = l.pop()
		if abs(x - y) > 1:
			flag = False
	return flag

def pairWiseConsecutive(l):
	if len(l) % 2 == 0:
		return getFlag(l)
	else:
		l.pop()
		return getFlag(l)


def pairWiseConsecutive(st):
	if len(st) % 2 != 0:
		st.pop(0)
	n = len(st)
	stack = []
	for i in range(n):
		if not stack:
			stack.append(st[i])
		elif abs(stack[-1] - st[i]) != 1:
			return False
		else:
			stack.append(st[i])
	return True


def pairWiseConsecutive(l):
	ans = []
	while len(l) != 0:
		ans.append(l[-1])
		l.pop()
	result = True
	while len(ans) > 1:
		x = ans[-1]
		ans.pop()
		y = ans[-1]
		ans.pop()
		if abs(x - y) != 1:
			result = False
		l.append(x)
		l.append(y)
	if len(ans) == 1:
		l.append(ans[-1])
	return result


def pairWiseConsecutive(l):
	if len(l) % 2 != 0:
		l.pop(-1)
	for i in range(0, len(l), 2):
		if abs(l[i] - l[i + 1]) != 1:
			return False
	return True


def pairWiseConsecutive(l):
	x = len(l)
	for i in range(x - 1):
		if abs(l[i] - l[i + 1]) != 1:
			return False
	return True


def check(l):
	if len(l) == 2:
		return l[1] - l[0] == 1
	if len(l) < 2:
		return 1
	d = l[1] - l[0]
	for i in range(1, len(l)):
		if d != l[i] - l[i - 1]:
			return -1
	return 1

def pairWiseConsecutive(l):
	if len(l) % 2 == 0:
		return check(l)
	else:
		l.pop()
		return check(l)


def pairWiseConsecutive(l):
	n = len(l)
	if n == 1:
		return True
	if n % 2 == 1:
		l.pop()
		n -= 1
	a = l[-1]
	l.pop()
	ans = True
	while l:
		if a == l[-1] - 1 or a == l[-1] + 1:
			a = l[-1]
			l.pop()
		else:
			l.pop()
			ans = False
	return ans


def pairWiseConsecutive(l):
	for i in range(1, len(l), 2):
		if l[i] != l[i - 1] + 1:
			return False
	return True


def pairWiseConsecutive(l):
	if len(l) % 2 == 1:
		l.pop()
	if not l:
		return True
	s = l.pop()
	while l:
		if l[-1] == s - 1 or l[-1] == s + 1:
			s = l.pop()
		else:
			return False
	return True


def pairWiseConsecutive(l):
	a = []
	if len(l) % 2 != 0:
		l.pop()
	for i in l:
		if len(a) == 0:
			a.append(i)
		elif a[-1] == 1 + i or a[-1] + 1 == i:
			a.pop()
		else:
			a.append(i)
	if len(a) == 0:
		return True
	else:
		return False


def pairWiseConsecutive(l):
	if n % 2 == 0:
		for i in range(0, n, 2):
			if abs(l[i] - l[i + 1]) != 1:
				return False
		return True
	else:
		for j in range(1, n, 2):
			if abs(l[j] - l[j + 1]) != 1:
				return False
		return True


def pairWiseConsecutive(l):
	if len(l) & 1 == 1:
		l.pop()
	while len(l) > 0:
		(a, b) = (l.pop(), l.pop())
		if abs(a - b) != 1:
			return False
	return True


def pairWiseConsecutive(l):
	if len(l) % 2 == 0:
		if all((l[i] - l[i + 1] == 1 for i in range(len(l) - 1))):
			return True
		if all((l[i] - l[i + 1] == -1 for i in range(len(l) - 1))):
			return True
		else:
			return False
	else:
		if all((l[i] - l[i + 1] == 1 for i in range(1, len(l) - 1))):
			return True
		if all((l[i] - l[i + 1] == -1 for i in range(1, len(l) - 1))):
			return True
		else:
			return False


def empty(l):
	return len(l) == 0

def pairWiseConsecutive(l):
	if len(l) % 2 != 0:
		l.pop()
	for i in range(0, n - 1):
		if l[i + 1] != l[i] + 1:
			return False
	return True


def pairWiseConsecutive(l):
	stack = l
	n = len(l)
	(prev, current) = (0, 1)
	if n % 2 != 0:
		stack.pop()
	while current < n:
		if abs(stack[prev] - stack[current]) != 1:
			return False
		prev += 1
		current += 1
	return True


def pairWiseConsecutive(l):
	st = l[:]
	n = len(l)
	prev = 0
	cur = 1
	if n % 2 != 0:
		l.pop()
	while cur < n:
		if abs(l[prev] - l[cur]) != 1:
			return False
		prev += 1
		cur += 1
	return True


def pairWiseConsecutive(l):
	stack = l[:]
	n = len(stack)
	prev = 0
	curr = 1
	if n % 2 != 0:
		stack.pop()
	while curr < n:
		p = stack[prev]
		c = stack[curr]
		if abs(p - c) != 1:
			return False
		prev += 1
		curr += 1
	return True


def pairWiseConsecutive(l):
	stack2 = []
	while l:
		stack2.append(l.pop())
	while stack2 and len(stack2) > 1:
		val1 = stack2.pop()
		val2 = stack2.pop()
		if abs(val1 - val2) != 1:
			return False
	return True


def pairWiseConsecutive(l):
	n = len(l)
	if n % 2 == 1:
		l.pop()
	while l:
		a = l.pop()
		b = l.pop()
		if abs(a - b) != 1:
			return False
	return True


def pairWiseConsecutive(l):
	for i in range(len(l) - 1):
		if l[i + 1] == l[i] + 1 or l[i + 1] == l[i] - 1:
			continue
		else:
			return False
	return True


def pairWiseConsecutive(l):
	if len(l) % 2 != 0:
		l.pop()
	while len(l) > 0:
		st = l.pop()
		st1 = l.pop()
		if st1 == st + 1 or st1 == st - 1:
			continue
		else:
			return False
	return True


def pairWiseConsecutive(l):
	a = l
	while a:
		A = a.pop()
		if a:
			B = a[-1]
			if abs(A - B) == 1:
				pass
			else:
				return False
	return True


def pairWiseConsecutive(l):
	if len(l) % 2 == 0:
		while len(l) != 0:
			temp1 = l.pop()
			temp2 = l.pop()
			if abs(temp1 - temp2) != 1:
				return False
		return True
	if len(l) % 2 == 1:
		l.pop()
		while len(l) != 0:
			temp1 = l.pop()
			temp2 = l.pop()
			if abs(temp1 - temp2) != 1:
				return False
		return True


def pairWiseConsecutive(l):
	if len(l) % 2:
		l.pop()
	while l:
		if abs(l.pop() - l.pop()) != 1:
			return False
	return True


def pairWiseConsecutive(l):
	while True:
		if not l:
			return True
		item1 = l.pop()
		if not l:
			return True
		item2 = l.pop()
		if abs(item1 - item2) != 1:
			return False


def pairWiseConsecutive(l):
	n = len(l)
	if n % 2 == 0:
		for i in range(0, n, 2):
			if l[i] != l[i + 1] - 1 and l[i] != l[i + 1] + 1:
				return 0
				break
		return 1
	else:
		for i in range(0, n - 1, 2):
			if l[i] != l[i + 1] - 1 and l[i] != l[i + 1] + 1:
				return 0
				break
		return 1


import queue

def pairWiseConsecutive(que):
	q = queue.Queue()
	for i in que:
		q.put(i)
	temp = []
	while q.qsize() != 0:
		temp.append(q.queue[0])
		q.get()
	temp2 = []
	while len(temp) != 0:
		temp2.append(temp[len(temp) - 1])
		temp.pop()
	result = bool(True)
	while len(temp2) > 1:
		x = temp2[len(temp2) - 1]
		temp2.pop()
		y = temp2[len(temp2) - 1]
		temp2.pop()
		if abs(x - y) != 1:
			result = False
		q.put(x)
		q.put(y)
	if len(temp2) == 1:
		q.put(temp2[len(temp2) - 1])
	return result


def pairWiseConsecutive(l):
	le = len(l)
	if le % 2 != 0:
		le = le - 1
	i = 0
	while i < le:
		if l[i] != l[i + 1] - 1:
			return False
		i += 2
	return True


def pairWiseConsecutive(l):
	stack = [element for element in l]
	if len(stack) % 2 != 0:
		stack.pop()
	while stack:
		x = stack.pop()
		y = stack.pop()
		if x - y not in (1, -1):
			return False
	return True


from collections import deque

def pairWiseConsecutive(l):
	s = deque()
	for i in l:
		if len(s) == 0:
			s.append(i)
		elif i + 1 == s[-1] or i - 1 == s[-1]:
			s.append(i)
		else:
			return False
	return True


def pairWiseConsecutive(l):
	if len(l) % 2 != 0:
		l.pop()
	if len(l) < 2:
		return True
	elif len(l) == 2:
		if l[0] == l[1] + 1 or l[0] == l[1] - 1:
			return True
	else:
		flag = True
		for i in range(0, len(l), 2):
			if l[i] == l[i + 1] + 1 or l[i] == l[i + 1] - 1:
				continue
			else:
				flag = False
				break
		return flag


def pairWiseConsecutive(l):
	if len(l) % 2 == 0:
		tot = len(l)
	else:
		tot = len(l) - 1
	state = 0
	for i in range(0, tot, 2):
		a = l.pop()
		b = l.pop()
		if a - b != 1 and a - b != -1:
			state = 1
	if state == 1:
		return False
	else:
		return True


def pairWiseConsecutive(l):
	StackLength = len(l)
	cursor = 1
	TotalPairs = StackLength // 2
	ConsecutivePairs = 0
	while cursor < StackLength:
		PairDifference = l[cursor] - l[cursor - 1]
		if abs(PairDifference) == 1:
			ConsecutivePairs += 1
		cursor += 2
	return ConsecutivePairs == TotalPairs


def pairWiseConsecutive(l):
	i = 0
	while i < len(l) - 1:
		if l[i] + 1 == l[i + 1] or l[i] - 1 == l[i + 1]:
			i += 2
		else:
			return False
	return True


def pairWiseConsecutive(l):
	if len(l) % 2 != 0:
		l.pop()
	s = []
	for i in l:
		s.append(i)
	while s:
		a = s.pop()
		b = s.pop()
		if a != b + 1 and a != b - 1:
			return False
	return True


class STACKS:

	def __init__(self):
		self.data = []

	def push(self, num):
		return self.data.append(num)

	def pop(self):
		return self.data.pop()

	def empty(self):
		if len(self.data) == 0:
			return True
		else:
			return False

def pairWiseConsecutive(l):
	s = STACKS()
	for i in range(len(l)):
		s.push(l[i])
	if len(l) % 2 != 0:
		s.pop()
	for i in range(len(l) // 2):
		s1 = s.pop()
		s2 = s.pop()
		if s1 == s2 + 1 or s2 == s1 + 1:
			pass
		else:
			return False
			break
	return True


def pairWiseConsecutive(l):
	k = l.copy()
	m = l.copy()
	m.pop()
	if len(k) % 2 == 0:
		for i in range(0, len(k), 2):
			if abs(k[i] - k[i + 1]) != 1:
				return False
		return True
	else:
		for i in range(0, len(m), 2):
			if abs(m[i] - m[i + 1]) != 1:
				return False
		return True


def pairWiseConsecutive(l):
	stack = []
	top = -1
	for i in range(len(l)):
		if i % 2 == 0:
			stack.append(l[i])
			top = top + 1
		else:
			if stack[top] + 1 != l[i] and stack[top] - 1 != l[i]:
				return False
			stack.pop()
			top = top - 1
	return True


def pairWiseConsecutive(l):
	size = len(l)
	if size % 2 != 0:
		size -= 1
	i = size - 1
	while i > 0:
		if l[i] != l[i - 1] - 1 and l[i] - 1 != l[i - 1]:
			return False
		i -= 2
	return True


def pairWiseConsecutive(l):
	n = len(l)
	i = 0
	while i < n and i + 1 < n:
		if abs(l[i] - l[i + 1]) != 1:
			return False
		i += 2
	return True


def pairWiseConsecutive(l):
	while len(l) != 0:
		x = l.pop(-1)
		if len(l) != 0:
			if x - 1 != l[-1]:
				return False
	return True


def pairWiseConsecutive(l):
	if len(l) == 1:
		return True
	if len(l) % 2 == 0:
		n = len(l)
	else:
		l.pop()
		n = len(l)
	x1 = []
	x2 = []
	for i in range(l[0], l[-1] + 1):
		x1.append(i)
	for j in range(l[-1], l[0] - 1, -1):
		x2.append(j)
	if x1 == l or x2 == l:
		return True
	else:
		return False


def pairWiseConsecutive(l):
	n = len(l)
	if n % 2 == 1:
		l.pop()
	while l:
		k = l.pop()
		m = l.pop()
		if k == m - 1 or m == k - 1:
			continue
		else:
			return False
	return True


def pairWiseConsecutive(l):
	k = 0
	m = 0
	yes = True
	if len(l) % 2 != 0:
		l.pop()
		while len(l) != 0:
			k = l.pop()
			m = l.pop()
			if k - m == abs(1):
				yes = True
			else:
				return False
	else:
		while len(l) != 0:
			k = l.pop()
			m = l.pop()
			if k - m == abs(1):
				yes = True
			else:
				return False
	return yes


def pairWiseConsecutive(l):
	if len(l) % 2 != 0:
		l.pop()
	index = 0
	for i in range(len(l) // 2):
		if l[index] + 1 == l[index + 1] or l[index] - 1 == l[index + 1]:
			index += 2
			continue
		else:
			return False
	return True


def pairWiseConsecutive(l):
	s = l.copy()
	if not l or len(l) == 1:
		return True
	if len(s) % 2:
		s.pop()
	temp = s.pop()
	l = [temp + 1, temp - 1]
	while s:
		temp = s.pop()
		if temp in l:
			l[0] = temp + 1
			l[1] = temp - 1
		else:
			return False
	return True


def pairWiseConsecutive(l):
	x = len(l)
	if x == 1:
		return True
	if x % 2 != 0:
		return False
	else:
		count = 0
		for i in range(n - 1):
			if l[i] == l[i + 1] - 1:
				count = count + 1
		if count == x - 1:
			return True
		else:
			return False
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		l = list(map(int, input().split()))
		pairWiseConsecutive(l)
		if pairWiseConsecutive(l) == True:
			print('Yes')
		else:
			print('No')


def pairWiseConsecutive(l):
	i = 0
	if len(l) == 1:
		return True
	if len(l) % 2 == 1:
		l.pop()
	if len(l) == 0:
		return False
	while i < len(l) - 1:
		if l[i] == l[i + 1] + 1 or l[i] == l[i + 1] - 1:
			state = 1
			if i == len(l) - 2 and state == 1:
				return True
			else:
				i += 2
		else:
			return False
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		l = list(map(int, input().split()))
		pairWiseConsecutive(l)
		if pairWiseConsecutive(l) == True:
			print('Yes')
		else:
			print('No')


def pairWiseConsecutive(l):
	a = len(l)
	flag = False
	if a % 2 != 0:
		l.pop()
	for i in range(a - 1):
		if l[i + 1] != l[i] + 1:
			flag = True
			break
	if flag == True:
		return False
	else:
		return True
if __name__ == '__main__':
	t = int(input())
	for i in range(t):
		n = int(input())
		l = list(map(int, input().split()))
		pairWiseConsecutive(l)
		if pairWiseConsecutive(l) == True:
			print('Yes')
		else:
			print('No')