Given an unsorted array arr[] of size N, the task is to check whether the array consists of consecutive numbers or not.
Example 1:
Input: N = 5, arr[] = {5, 4, 2, 1, 3}
Output: Yes
Explanation: All are consecutive elements,
according to this order 1,2,3,4 and 5.
Example 2:
Input: N = 6, arr[] = {2, 1, 0, -3, -1, -2}
Output: Yes
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function areConsecutives() that takes array arr[] and integer N as parameters and returns the boolean true if array elements are consecutive else boolean false.
Note: If the elements are consecutive driver code outputs Yes otherwise, it outputs No
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{7}

Difficulty: EASY
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).

Starter code:
class Solution:

    def areConsecutives(self, arr, n):

        # Your code goes here

Solutions:
class Solution:

	def areConsecutives(self, arr, n):
		min_ = min(arr)
		p = list(range(min_, min_ + n))
		dict_ = {elem: 0 for elem in arr}
		for elem in p:
			if elem not in dict_:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		summ = sum(arr)
		a = min(arr)
		ap_sum = n / 2 * (2 * a + (n - 1))
		if summ == ap_sum:
			return True
		else:
			return False


class Solution:

	def areConsecutives(self, A, n):
		n = len(A)
		mini = min(A)
		for i in range(n):
			A[i] = A[i] - mini + 1
			if A[i] > n:
				return 0
		for i in range(n):
			if A[abs(A[i]) - 1] > 0:
				A[abs(A[i]) - 1] = A[abs(A[i]) - 1] * -1
		for i in range(n):
			if A[i] > 0:
				return 0
		return 1


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		for i in range(n - 1):
			if arr[i + 1] - arr[i] != 1:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		a = sum(arr)
		b = min(arr)
		apsum = n / 2 * (2 * b + (n - 1))
		if a == apsum:
			return True
		else:
			return False


class Solution:

	def areConsecutives(self, arr, n):
		smallest = min(arr)
		largest = max(arr)
		if largest - smallest + 1 != n:
			return False
		visited = [False for i in range(n)]
		for num in arr:
			if visited[largest - num]:
				return False
			visited[largest - num] = True
		return True


from collections import defaultdict

class Solution:

	def D(self):
		return -1

	def areConsecutives(self, arr, n):
		d = defaultdict(self.D)
		for i in arr:
			d[i] = 1
		mi = min(arr)
		k = 0
		for i in range(mi, max(arr) + 1):
			if d[i] == -1:
				return False
			k += 1
		if k != n:
			return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		a = arr[0]
		b = arr[n - 1]
		l = []
		for i in range(a, b + 1):
			l.append(i)
		if l == arr:
			return True
		else:
			return False


class Solution:

	def areConsecutives(self, arr, n):
		sum_arr = sum(arr)
		min_val_arr = min(arr)
		ap_sum = n / 2 * (2 * min_val_arr + (n - 1))
		return sum_arr == ap_sum


class Solution:

	def areConsecutives(self, arr, n):
		sum_arr = sum(arr)
		min_val_arr = min(arr)
		ap_sum = n / 2 * (2 * min_val_arr + (n - 1) * 1)
		if sum_arr == ap_sum:
			return True
		else:
			return False


class Solution:

	def areConsecutives(self, arr, n):
		d = []
		e = []
		c = 0
		for i in range(0, len(arr)):
			e.append(arr[i])
		e.sort()
		for i in range(0, len(e)):
			if e[i] - e[i - 1] == 1:
				c += 1
		if c == n - 1:
			return 1
		else:
			return 0


class Solution:

	def areConsecutives(self, arr, n):
		ans = []
		arr.sort()
		for i in range(1, n):
			ans.append(arr[i] - arr[i - 1] == 1)
		if ans.__contains__(False):
			return False
		else:
			return True


class Solution:

	def areConsecutives(self, arr, n):
		a = min(arr)
		d = dict(zip(iter(arr), iter(arr)))
		for i in range(n):
			if a in d:
				a += 1
			else:
				return 0
				break
		return 1


class Solution:

	def areConsecutives(self, arr, n):
		if n == 1:
			return 'Yes'
		arr.sort()
		return sorted(arr) == list(range(min(arr), max(arr) + 1))


class Solution:

	def areConsecutives(self, arr, n):
		c = min(arr)
		d = max(arr)
		l = []
		a = d - c + 1
		if n != a:
			return False
		arr.sort()
		for i in range(n - 1):
			if arr[i + 1] - arr[i] == 1:
				l.append(1)
			else:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		arr = sorted(arr)
		for i in range(n - 1):
			if arr[i + 1] - arr[i] != 1:
				return 0
		return 1


from collections import defaultdict

class Solution:

	def areConsecutives(self, arr, n):
		minm = float('infinity')
		maxm = float('-infinity')
		hashmap = defaultdict(int)
		for i in range(len(arr)):
			if arr[i] < minm:
				minm = arr[i]
			if arr[i] > maxm:
				maxm = arr[i]
			hashmap[arr[i]] += 1
		for i in range(minm, maxm + 1):
			if i not in hashmap:
				return False
			if hashmap[i] > 1:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		sums = sum(arr)
		mini = min(arr)
		maxi = max(arr)
		nums = [i for i in range(mini, maxi + 1)]
		if sums == sum(nums):
			return 1
		return 0


class Solution:

	def areConsecutives(self, arr, n):
		s = sum(arr)
		a = min(arr)
		h = n / 2 * (2 * a + (n - 1))
		if s == h:
			return 1
		else:
			return 0


class Solution:

	def areConsecutives(self, nums, n):
		nums_min = min(nums)
		nums_hasmap = {num: 0 for num in nums}
		count = 0
		while nums_min in nums_hasmap:
			count += 1
			nums_min += 1
		return count == n


class Solution:

	def areConsecutives(self, arr, n):
		first = min(arr)
		last = max(arr)
		if len(arr) == 1:
			return 1
		diff = 1
		curr_sum = sum(arr)
		sum_ap = 2 * first + (n - 1) * diff
		sum_ap = n * sum_ap // 2
		if curr_sum == sum_ap:
			return 1
		return 0


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		c = arr[0]
		for i in arr[1:]:
			if i != c + 1:
				return False
			else:
				c = i
		return True


import sys

class Solution:

	def areConsecutives(self, arr, n):
		map_ = {}
		min_ = sys.maxsize
		for i in arr:
			map_[i] = 1
			min_ = min(i, min_)
		for i in range(min_, min_ + len(arr)):
			if i not in map_:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		start = arr[0]
		a = start
		newarr = []
		i = 0
		while i < n:
			newarr.append(a)
			a += 1
			i += 1
		if newarr == arr:
			return True
		else:
			return False


class Solution:

	def areConsecutives(self, arr, n):
		summ = sum(arr)
		a = min(arr)
		ap_sum = n / 2 * (2 * a + (n - 1))
		if summ == ap_sum:
			return True
		else:
			return False
if __name__ == '__main__':
	t = int(input())
	for _ in range(0, t):
		n = int(input())
		a = list(map(int, input().split()))
		ob = Solution()
		ans = ob.areConsecutives(a, n)
		if ans:
			print('Yes')
		else:
			print('No')


class Solution:

	def areConsecutives(self, arr, n):
		dict = {arr[i]: i for i in range(n)}
		a = min(arr)
		for i in range(n - 1):
			if a + 1 not in dict.keys():
				return False
			else:
				a += 1
				continue
		return True
if __name__ == '__main__':
	t = int(input())
	for _ in range(0, t):
		n = int(input())
		a = list(map(int, input().split()))
		ob = Solution()
		ans = ob.areConsecutives(a, n)
		if ans:
			print('Yes')
		else:
			print('No')


class Solution:

	def areConsecutives(self, arr, n):
		max_ele = max(arr)
		min_ele = min(arr)
		s = set()
		if max_ele - min_ele != n - 1:
			return False
		s = {i for i in arr}
		if len(s) != n:
			return False
		return True
if __name__ == '__main__':
	t = int(input())
	for _ in range(0, t):
		n = int(input())
		a = list(map(int, input().split()))
		ob = Solution()
		ans = ob.areConsecutives(a, n)
		if ans:
			print('Yes')
		else:
			print('No')


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		for i in range(n - 1):
			if arr[i + 1] - arr[i] != 1:
				return False
		return True
if __name__ == '__main__':
	t = int(input())
	for _ in range(0, t):
		n = int(input())
		a = list(map(int, input().split()))
		ob = Solution()
		ans = ob.areConsecutives(a, n)
		if ans:
			print('Yes')
		else:
			print('No')


class Solution:

	def areConsecutives(self, arr, n):
		dictonary = {}
		minimum_ele = float('inf')
		for i in range(n):
			dictonary[arr[i]] = 0
			minimum_ele = min(minimum_ele, arr[i])
		for i in range(minimum_ele, minimum_ele + len(arr)):
			if i not in dictonary:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		map_ = {}
		min_ = 1000000
		max_ = -1000000
		for n in arr:
			if n not in map_:
				min_ = min(min_, n)
				max_ = max(max_, n)
				map_[n] = 1
		for i in range(min_, min_ + len(arr)):
			if i not in map_:
				return False
		return True


class Solution:

	def areConsecutives(self, a, n):
		return sum(a) == n * (2 * min(a) + n - 1) // 2


class Solution:

	def areConsecutives(self, arr, n):
		total_sum = sum(arr)
		mina = min(arr)
		AP = n / 2 * (2 * mina + (n - 1))
		if total_sum == AP:
			return True
		else:
			return False


class Solution:

	def areConsecutives(self, arr, n):
		minm = arr[0]
		for i in range(n):
			minm = min(minm, arr[i])
		for i in range(n):
			arr[i] -= minm
		return self.sorty(arr, n)

	def sorty(self, arr, n):
		i = 0
		while i < n:
			if i != arr[i]:
				corrPos = arr[i]
				if corrPos >= n:
					return False
				if arr[corrPos] == corrPos:
					return False
				(arr[i], arr[corrPos]) = (arr[corrPos], arr[i])
			else:
				i += 1
		return True


class Solution:

	def areConsecutives(self, arr, n):
		c = 0
		arr = sorted(arr)
		for i in range(1, len(arr)):
			if arr[i] - arr[i - 1] == 1:
				c += 1
		if c == n - 1:
			return 1
		else:
			return 0


class Solution:

	def areConsecutives(self, arr, n):
		if n < 1:
			return False
		Min = min(arr)
		Max = max(arr)
		if Max - Min + 1 == n:
			visited = [False for i in range(n)]
			for i in range(n):
				if visited[arr[i] - Min] != False:
					return False
				visited[arr[i] - Min] = True
			return True
		return False


from collections import Counter

class Solution:

	def areConsecutives(self, arr, n):
		a = sum(arr)
		k = min(arr)
		l = max(arr)
		b = (2 * k + (n - 1)) * n // 2
		return a == b


from collections import Counter

class Solution:

	def areConsecutives(self, arr, n):
		gg = Counter(arr)
		mini = min(arr)
		for i in range(mini, mini + n):
			if i not in gg:
				return False
		return True


import collections

class Solution:

	def areConsecutives(self, arr, n):
		x = collections.OrderedDict()
		for i in arr:
			if i in x:
				return False
			x.update({i: 0})
		y = min(arr)
		for i in arr:
			if i != y:
				if i - 1 not in x:
					return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		cmin = min(arr)
		cmax = max(arr)
		if cmax - cmin != n - 1:
			return False
		arr_set = set(arr)
		if len(arr_set) != n:
			return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		li = [i for i in range(min(arr), max(arr) + 1)]
		arr.sort()
		return li == arr


class Solution:

	def areConsecutives(self, arr, n):
		minm = float('inf')
		umap = {}
		for i in range(n):
			umap[arr[i]] = 1
			minm = min(minm, arr[i])
		for i in range(minm, minm + n):
			if umap.get(i) is None:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		prev = None
		retval = True
		for i in arr:
			if prev == None:
				prev = i
			elif prev + 1 != i:
				return False
			else:
				prev = i
		return retval


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		f = arr[0]
		for i in range(0, n):
			if i + f != arr[i]:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		count = 0
		for i in range(len(arr) - 1):
			if arr[i] + 1 == arr[i + 1]:
				count += 1
		if count == len(arr) - 1:
			return True


class Solution:

	def areConsecutives(self, arr, n):
		mini = min(arr)
		maxi = max(arr)
		if maxi - mini + 1 != n:
			return False
		if len(list(set(arr))) != n:
			return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		hashmap = {}
		min_elem = float('inf')
		for i in range(0, n):
			hashmap[arr[i]] = 1
			min_elem = min(min_elem, arr[i])
		for i in range(min_elem, min_elem + n):
			if i not in hashmap:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		if n == 0 or n == 1:
			return 'Yes'
		else:
			arr.sort()
			ans = 0
			for i in range(n - 1):
				if arr[i] - arr[i + 1] == -1:
					ans += 1
			if ans == n - 1:
				return True
			else:
				return False


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		s = arr
		if len(s) != len(arr):
			return False
		min = arr[0]
		max = arr[n - 1]
		if max - min + 1 != n:
			return False
		for i in range(n - 1):
			if arr[i + 1] - arr[i] != 1:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		sm = 0
		sum_ = sum(arr)
		Min_ = min(arr)
		for i in range(Min_, Min_ + n):
			sm = sm + i
		if sum_ != sm:
			return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		a = arr[0]
		x = len(arr)
		sum = x / 2 * (2 * a + (x - 1))
		sum_arr = 0
		for item in arr:
			sum_arr += item
		if sum_arr == sum:
			return True


from collections import defaultdict

class Solution:

	def areConsecutives(self, arr, n):
		dict = defaultdict(int)
		minm = min(arr)
		for i in range(n):
			dict[arr[i]] = 0
		for i in range(minm, minm + n):
			if i not in dict:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		my_range = list(range(min(arr), max(arr) + 1))
		if sorted(list(arr)) == my_range:
			return True
		else:
			return False


class Solution:

	def areConsecutives(self, arr, n):
		arr.sort()
		e = arr[0]
		r = True
		for i in range(1, n):
			if not arr[i] == e + 1:
				r = False
				break
			else:
				e = arr[i]
		return r


from collections import deque

class Solution:

	def areConsecutives(self, arr, n):
		arr = sorted(arr)
		i = 0
		while i < n - 1:
			if arr[i + 1] - arr[i] == 1:
				i += 1
			else:
				return False
		return True


class Solution:

	def areConsecutives(self, arr, n):
		return len(set(arr)) == len(arr) and max(arr) - min(arr) == n - 1


class Solution:

	def areConsecutives(self, arr, n):
		a = set(arr)
		if len(a) == len(arr) and max(arr) - min(arr) == n - 1:
			return True
		return False