Given an array of n integers(duplicates allowed). Print “Yes” if it is a set of contiguous integers else print “No”.
Example 1:
Ã¢â¬â¹Input : arr[ ] = {5, 2, 3, 6, 4, 4, 6, 6}
Output : Yes
Explanation:
The elements  of array form a contiguous 
set of integers which is {2, 3, 4, 5, 6} 
so the output is "Yes".
Example 2:
Input : arr[ ] = {10, 14, 10, 12, 12, 
                              13, 15} 
Output : No
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 areElementsContiguous() that takes an array (arr), sizeOfArray (n), and return the true if it is a set of contiguous integers else print false. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
 
CONSTRAINTS:
1 ≤ N ≤10^{5}
1 ≤ a[i] ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: String algorithms

Sorting

Data structures

Starter code: #User function Template for python3

class Solution:
    def areElementsContiguous (self, arr, n): 
    #Complete the function



Solutions:
class Solution:

	def areElementsContiguous(self, arr, n):
		num_set = set(arr)
		minimum = min(num_set)
		maximum = max(num_set)
		expected_count = maximum - minimum + 1
		if len(num_set) == expected_count:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		sorted(arr)
		x = set(arr)
		v = list(x)
		cnt = 0
		for i in range(len(v) - 1):
			j = i + 1
			if v[i] != v[j] - 1:
				return 0
		return 1


class Solution:

	def areElementsContiguous(self, arr, n):
		arr.sort()
		store = list(set(arr))
		lst = []
		for i in range(min(arr), max(arr) + 1):
			lst.append(i)
		if lst == store:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		k = list(set(arr))
		k.sort()
		a = k[0] + 1
		for i in range(1, len(k)):
			if k[i] != a:
				return 0
				break
			a = a + 1
		return 'Yes'


class Solution:

	def areElementsContiguous(self, arr, n):
		arr.sort()
		for i in range(0, n):
			if arr[i] - arr[i - 1] > 1:
				return 0
		return 1


class Solution:

	def areElementsContiguous(self, arr, n):
		m1 = min(arr)
		m6 = max(arr)
		for i in range(m1, m6 + 1):
			if i not in arr:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		arr.sort()
		min_elem = arr[0]
		for index in range(n):
			if arr[index] == min_elem:
				continue
			elif arr[index] == min_elem + 1:
				min_elem = arr[index]
			else:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		b = sorted(list(set(arr)))
		c = 0
		for i in range(len(b) - 1):
			if b[i] + 1 != b[i + 1]:
				c += 1
		if c == 0:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(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 areElementsContiguous(self, arr, n):
		s = set(arr)
		l = list(s)
		l.sort()
		m = min(l)
		t = max(l)
		r = [i for i in range(m, t + 1)]
		if r == l:
			return True
		else:
			return False


import collections

class Solution:

	def areElementsContiguous(self, arr, n):
		dict1 = collections.Counter(arr)
		min = 10 ** 9
		count = 0
		summ = 0
		for j in dict1:
			count += 1
			summ += j
			if j < min:
				min = j
		if int(count / 2 * (2 * min + (count - 1))) == summ:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		arr = list(set(arr))
		n = len(arr)
		res = []
		for i in range(n - 1):
			if arr[i] + 1 in arr:
				res.append(1)
			else:
				res.append(0)
		for i in range(len(res)):
			if res[i] == 0:
				return 0
		return 1


class Solution:

	def areElementsContiguous(self, arr, n):
		d = set(arr)
		q = [arr[-1]]
		d.remove(arr[-1])
		while q != []:
			w = q.pop()
			if w + 1 in d:
				d.remove(w + 1)
				q.append(w + 1)
			if w - 1 in d:
				d.remove(w - 1)
				q.append(w - 1)
		if len(d) == 0:
			return True
		return False


class Solution:

	def areElementsContiguous(self, arr, n):
		a = max(arr)
		b = [0] * a
		c = arr[0]
		for i in arr:
			if b[i - 1] == 0:
				b[i - 1] = 1
				if i < c:
					c = i
		for i in range(c, a):
			if b[i] == 0:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		m = list(set(arr))
		a = min(m)
		b = max(m)
		c = b - a + 1
		if c != len(m):
			return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		mx = max(arr)
		x = [0] * mx
		mn = arr[0]
		for i in arr:
			if x[i - 1] == 0:
				x[i - 1] = 1
				if i < mn:
					mn = i
		for i in range(mn, mx):
			if x[i] == 0:
				return 0
		return 1


class Solution:

	def areElementsContiguous(self, arr, n):
		d = set(arr)
		b = list(d)
		b.sort()
		con = 0
		for i in range(len(b) - 1):
			if b[i] + 1 == b[i + 1]:
				con += 1
		if con == len(b) - 1:
			return 1
		else:
			return 0


class Solution:

	def areElementsContiguous(self, arr, n):
		mn = min(arr)
		mx = max(arr)
		mp = {}
		for i in range(n):
			mp[arr[i]] = 1
		for j in range(mn, mx + 1):
			if j not in mp:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		a = set(arr)
		a = sorted(a)
		j = 1
		while j < len(a):
			if a[j] - a[j - 1] > 1:
				return False
			j += 1
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		s = set(arr)
		if len(s) == 1:
			return True
		s = sorted(s)
		for i in range(len(s) - 1):
			if s[i + 1] - s[i] != 1:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		l = []
		for i in arr:
			if i not in l:
				l.append(i)
		(max1, min1) = (max(l), min(l))
		for i in range(min(l), max(l) + 1):
			if i not in l:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		m = min(arr)
		m1 = max(arr)
		if len(set(arr)) == m1 - m + 1:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		my = set()
		maxi = 0
		mini = 10000000
		for i in arr:
			my.add(i)
		for i in arr:
			maxi = max(i, maxi)
			mini = min(i, mini)
		for i in range(mini, maxi + 1):
			if i not in my:
				return 0
		return 1


class Solution:

	def areElementsContiguous(self, arr, n):
		d = {}
		for i in arr:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
		maxi = 0
		mini = 10000000
		for i in arr:
			maxi = max(i, maxi)
			mini = min(i, mini)
		for i in range(mini, maxi + 1):
			try:
				if d[i] != 0:
					continue
			except:
				return 0
		return 1


from collections import Counter

class Solution:

	def areElementsContiguous(self, arr, n):
		arr = sorted(arr)
		d = list(set(arr))
		d = sorted(d)
		c = 0
		for j in range(1, len(d)):
			if d[j] - d[j - 1] == 1:
				c += 1
		if c + 1 == len(d):
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		l = []
		for i in arr:
			if i not in l:
				l.append(i)
		l.sort()
		a = l[0]
		for j in range(1, len(l)):
			a += 1
			if a != l[j]:
				return False
		return True


class Solution:

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


class Solution:

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


import sys

class Solution:

	def areElementsContiguous(self, arr, n):
		s = set()
		min_elem = sys.maxsize
		max_elem = -sys.maxsize
		for i in range(n):
			if arr[i] not in s:
				s.add(arr[i])
				min_elem = min(min_elem, arr[i])
				max_elem = max(max_elem, arr[i])
		if max_elem - min_elem > len(s) - 1:
			return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		li1 = list(set(arr))
		li1.sort()
		li2 = [0] * (li1[-1] + 1)
		for i in range(len(li1)):
			li2[li1[i]] = li1[i]
		flag = 0
		for i in range(li1[0], li1[-1] + 1):
			if li2[i] == 0:
				flag = 1
				break
		if flag == 0:
			return 1
		else:
			return 0


class Solution:

	def areElementsContiguous(self, arr, n):
		Min = min(arr)
		Max = max(arr)
		d = dict()
		for i in range(Min, Max + 1):
			d[i] = 0
		for j in arr:
			d[j] = 1
		for i in d:
			if d[i] == 0:
				return False
		return True


class Solution:

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


class Solution:

	def areElementsContiguous(self, arr, n):
		s = set(arr)
		l = list(s)
		l.sort()
		a = l[0]
		b = l[-1]
		c = -1
		try:
			for i in range(a, b + 1):
				c += 1
				if l[c] != i:
					return False
		except:
			return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		us = set()
		for i in range(n):
			us.add(arr[i])
		l = list(us)
		for i in range(len(l) - 1):
			p = l[i]
			if p + 1 not in us:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		s = set(arr)
		mi = min(s)
		ma = max(s)
		if len(s) == ma - mi + 1:
			return True
		return False


class Solution:

	def areElementsContiguous(self, arr, n):
		arr = list(set(sorted(arr)))
		last = arr[0]
		for i in range(1, len(arr)):
			if arr[i] != last + 1:
				return False
			last = arr[i]
		return True


class Solution:

	def areElementsContiguous(self, a, n):
		s = set(arr)
		x = min(a)
		y = max(a)
		for i in range(x, y + 1):
			if i not in s:
				return False
		return True


class Solution:

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


class Solution:

	def areElementsContiguous(self, arr, n):
		a = sorted(set(arr))
		b = a[0]
		flag = True
		for i in a:
			if i == a[-1] and i == b:
				break
			if b in a:
				b += 1
				flag = True
			else:
				flag = False
				break
		if flag:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		nums = set()
		for i in arr:
			nums.add(i)
		MIN = 1000000.0
		SUM = 0
		for i in nums:
			MIN = min(MIN, i)
			SUM += i
		l = len(nums)
		ideal_sum = l * MIN + (l - 1) * l // 2
		if SUM == ideal_sum:
			return True
		return False


class Solution:

	def areElementsContiguous(self, arr, n):
		p = list(set(arr))
		p.sort()
		for i in range(len(p) - 1):
			if p[i + 1] != p[i] + 1:
				return 0
		return 1


class Solution:

	def areElementsContiguous(self, arr, n):
		Max = max(arr)
		Min = min(arr)
		Set2 = set(arr)
		Set = set(range(Min, Max + 1))
		for number in Set:
			if number not in Set2:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		hash = dict()
		for i in range(0, n):
			if arr[i] not in hash:
				hash[arr[i]] = 1
		first = list(hash.keys())[0]
		for (key, val) in hash.items():
			if key != first:
				return False
			first += 1
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		d = sorted(set(arr), key=arr.index)
		f = sorted(d)
		c = 1
		for i in range(0, len(f) - 1):
			if f[i + 1] - f[i] == 1:
				c += 1
		if c == len(f):
			return True
		return False


class Solution:

	def areElementsContiguous(self, arr, n):
		d = set(arr)
		count = 0
		for i in arr:
			if i - 1 not in d:
				k = 0
				while i + k in d:
					k += 1
				count = max(count, k)
		return count == len(d)


class Solution:

	def areElementsContiguous(self, arr, n):
		res = list(set(arr))
		res.sort()
		x = min(res)
		y = max(res)
		a = []
		for i in range(x, y + 1):
			a.append(i)
		if res == a:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		mi = min(arr)
		ma = max(arr)
		s = set(arr)
		i = mi
		while i <= ma:
			if i not in s:
				return False
			i += 1
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		m = min(arr)
		m1 = max(arr)
		l1 = []
		s = set(arr)
		for i in range(m, m1 + 1):
			if i in s:
				l1.append('1')
			else:
				l1.append('0')
		if '0' in l1:
			return False
		else:
			return True


class Solution:

	def areElementsContiguous(self, arr, n):
		arr = sorted(list(set(arr)))
		for i in range(len(arr) - 1):
			if abs(arr[i] - arr[i + 1]) != 1:
				return 0
		return 1


class Solution:

	def areElementsContiguous(self, arr, n):
		d = []
		for i in arr:
			if i not in d:
				d.append(i)
		d.sort()
		for i in range(len(d) - 1):
			if d[i + 1] - d[i] != 1:
				return 0
		return 1


class Solution:

	def areElementsContiguous(self, arr, n):
		lo = min(arr)
		hi = max(arr)
		if len(set(arr)) == hi - lo + 1:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		x = set(arr)
		p = min(x) - 1
		q = max(x)
		t = p * (p + 1) // 2
		s = q * (q + 1) // 2
		if sum(x) == s - t:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		lst = list(set(arr))
		t = len(lst)
		for i in range(t - 1):
			if lst[i] + 1 != lst[i + 1]:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		s = {}
		for i in arr:
			s[i] = 1
		b = []
		for i in s:
			b.append(i)
		b.sort()
		n = len(b)
		for i in range(n - 1):
			if b[i + 1] - b[i] != 1:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		flag = False
		min_num = min(arr)
		max_num = max(arr)
		A = [0] * (max_num + 1)
		for i in range(n):
			if A[arr[i]] == 0:
				A[arr[i]] = 1
		j = min_num
		while j <= max_num:
			if A[j] == 0:
				flag = True
			j += 1
		if flag:
			return False
		else:
			return True


class Solution:

	def areElementsContiguous(self, arr, n):
		arr = list(set(arr))
		arr.sort()
		if arr[-1] == arr[0] + len(arr) - 1:
			return True
		else:
			return False


class Solution:

	def areElementsContiguous(self, arr, n):
		min_val = min(arr)
		max_val = max(arr)
		temp = sorted(list(set(arr)))
		if temp != list(range(min_val, max_val + 1)):
			return False
		else:
			return True


class Solution:

	def areElementsContiguous(self, arr, n):
		arr.sort()
		arr = list(set(arr))
		for i in range(len(arr) - 1):
			if arr[i] + 1 == arr[i + 1]:
				continue
			else:
				return False
		return True
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ob = Solution()
	res = ob.areElementsContiguous(arr, n)
	if res:
		print('Yes')
	else:
		print('No')


class Solution:

	def areElementsContiguous(self, arr, n):
		flag = 0
		minimum = min(arr)
		maximum = max(arr)
		for i in range(minimum, maximum + 1):
			if i in arr:
				flag = 1
			else:
				return 0
		if flag == 1:
			return 1
for _ in range(0, int(input())):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	ob = Solution()
	res = ob.areElementsContiguous(arr, n)
	if res:
		print('Yes')
	else:
		print('No')


class Solution:

	def areElementsContiguous(self, arr, n):
		a = min(arr)
		b = max(arr)
		for i in range(a + 1, b):
			if i not in arr:
				return False
		return True


class Solution:

	def areElementsContiguous(self, arr, n):
		arr = list(set(arr))
		ma = max(arr)
		mi = min(arr)
		if len(arr) == ma - mi + 1:
			return 1
		return 0


from collections import defaultdict

class Solution:

	def areElementsContiguous(self, arr, n):
		d = defaultdict(int)
		small = 999999
		largest = 0
		for i in arr:
			d[i] = i
			small = min(small, i)
			largest = max(largest, i)
		for i in range(small, largest + 1):
			if d[i] == 0:
				return 0
		return 1


from collections import defaultdict

class Solution:

	def areElementsContiguous(self, arr, n):
		d = defaultdict(int)
		for i in arr:
			d[i] = i
		l = sorted(d.items(), key=lambda x: x[0])
		for i in range(len(l) - 1):
			if l[i][0] + 1 != l[i + 1][0]:
				return 0
		return 1