Chef has a set S containing N distinct integers.

Chef wants to gift Chefina an array A of any finite length such that the following conditions hold true:

A_{i} \in S \forall i. In other words, each element of the array A should belong to the set S.
[Mean] value of all the elements in A is exactly X.

Find whether there exists an array A of finite length satisfying the above conditions.

------ Input Format ------ 

- First line will contain T, the number of test cases. Then the test cases follow.
- First line of each test case contains two integers N and X - denoting the size of set S and the mean value of the required array.
- Second line contains N distinct integers S_{1}, S_{2}, \dots , S_{N} - denoting the set S.

------ Output Format ------ 

For each test case, output in a single line, YES if there exists an array satisfying the given conditions, NO otherwise.

You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).

------ Constraints ------ 

$1 ≤ T ≤ 1000$
$1 ≤ N ≤ 10^{5}$
$1 ≤ S_{i} ≤ 10^{9}$
$1 ≤ X ≤ 10^{9}$
$S_{i} \neq S_{j}$ for $ i \neq j$
- Sum of $N$ over all test case do not exceed $2 \cdot 10^{5}$.

----- Sample Input 1 ------ 
4
3 2
1 2 3
1 5
3
2 5
4 6
1 5
5

----- Sample Output 1 ------ 
YES
NO
YES
YES

----- explanation 1 ------ 
Test Case $1$: One of the valid arrays is $A = [2,2]$. Here, $2 \in \{1, 2, 3\}$. Also, mean value of the array is $\frac{2+2}{2} = 2$.

Test Case $2$: Since all elements of the array can only be equal to $3$, the mean value of $A$ cannot be equal to $5$.

Test Case $3$: One of the valid arrays is $A = [4,6]$. Here, $4 \in \{4, 6\}$ and $6 \in \{4, 6\}$. Also, mean value of the array is $\frac{4+6}{2} = 5$.

Test Case $4$: One of the valid arrays is $A = [5]$. Here, $5 \in \{5\}$. Also, mean value of the array is $\frac{5}{1} = 5$.

Source: codechef | Difficulty: MEDIUM_HARD
Tags: Mathematics

Constructive algorithms

Implementation

Ad-hoc


Solutions:
T = int(input())
while T > 0:
	T -= 1
	L = []
	(N, X) = [int(i) for i in input().split()]
	S = [int(i) for i in input().split()]
	if X >= min(S) and X <= max(S):
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	(N, X) = map(int, input().split())
	S = list((int(e) for e in input().split()))
	S.sort()
	if (S[0] <= X) & (S[N - 1] >= X):
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split(' '))
	s = list(map(int, input().split(' ')))
	if m in s:
		print('YES')
	elif min(s) < m and max(s) > m:
		print('YES')
	else:
		print('NO')


from statistics import mean
for i in range(int(input())):
	(a, b) = map(int, input().split())
	c = list(map(int, input().split()))
	y = max(c)
	x = min(c)
	if b >= x and b <= y:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	(n, x) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	if min(arr) <= x and max(arr) >= x:
		print('YES')
	else:
		print('NO')


n_cases = int(input())
for _ in range(n_cases):
	(_ignore, mean) = (int(x) for x in input().split())
	iset = [int(x) for x in input().split()]
	(ltc, eqc, gtc) = (0, 0, 0)
	for x in iset:
		if x < mean:
			ltc += 1
		elif x == mean:
			eqc += 1
		else:
			gtc += 1
	print('YES' if eqc > 0 or (ltc > 0 and gtc > 0) else 'NO')


for i in range(int(input())):
	(n, x) = map(int, input().split())
	l = list(map(int, input().split()))
	if x in l:
		print('YES')
	elif n > 1 and x > min(l) and (x < max(l)):
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	(N, x) = map(int, input().split())
	num = input()
	list1 = num.split(' ')
	sum = 0
	list2 = []
	for i in list1:
		sum = sum + int(i)
		list2.append(int(i))
	avg = sum / len(list1)
	if x in list2:
		print('YES')
	elif x >= min(list2) and x <= max(list2):
		print('YES')
	else:
		print('NO')


T = int(input())
for i in range(T):
	(N, X) = [int(x) for x in input().split(' ')]
	S = [int(x) for x in input().split(' ', N)]
	(s_min, s_max) = (min(S), max(S))
	print('YES' if s_max >= X and s_min <= X else 'NO')


t = int(input())
while t:
	(n, s) = map(int, input().split())
	x = list(map(int, input().split()))
	(min_, max_) = (min(x), max(x))
	if s >= min_ and s <= max_:
		print('YES')
	else:
		print('NO')
	t -= 1


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	m = pow(10, 9)
	M = 1
	for k in input().split():
		k = int(k)
		if k < m:
			m = k
		if k > M:
			M = k
	if m <= x and x <= M:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	S = list(set(map(int, input().split())))
	print('YES' if min(S) <= x <= max(S) else 'NO')


def main():
	t = int(input())
	for _ in range(t):
		(n, x) = [int(i) for i in input().split(' ')]
		arr = [int(i) for i in input().split(' ')]
		(a1, a2) = (min(arr), max(arr))
		if x >= a1 and x <= a2:
			print('YES')
		else:
			print('NO')
main()


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	s = list(map(int, input().split()))
	(maxi, mini) = (max(s), min(s))
	if mini <= x <= maxi:
		print('YES')
	else:
		print('NO')


for t in range(int(input())):
	(n, x) = map(int, input().split(' '))
	A = [int(j) for j in input().split(' ')]
	mina = min(A)
	maxa = max(A)
	if x >= mina and x <= maxa:
		print('YES')
	else:
		print('NO')


t = int(input())
for _ in range(t):
	(N, X) = map(int, input().split())
	arr = list(map(int, input().split()))
	if X < min(arr) or X > max(arr):
		print('NO')
	else:
		print('YES')


import math

def solve(N, X, arr):
	if X < min(arr) or X > max(arr):
		print('NO')
	else:
		print('YES')
t = int(input())
for _ in range(t):
	(N, X) = map(int, input().split())
	arr = list(map(int, input().split()))
	solve(N, X, arr)


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	s = list(map(int, input().split()))
	s.sort()
	if s[0] <= x <= s[-1]:
		print('YES')
	else:
		print('NO')


for _ in range(int(input())):
	(n, x) = map(int, input().split())
	s = list(map(int, input().split()))
	if x >= min(s) and x <= max(s):
		print('YES')
	else:
		print('NO')


n = int(input())
for i in range(n):
	(useless, cc) = list(map(int, input().split()))
	s = set(map(int, input().split(' ')))
	if cc in s or (max(s) > cc and min(s) < cc):
		print('YES')
	else:
		print('NO')


T = int(input())
for _ in range(T):
	(n, x) = list(map(int, input().split()))
	nums = set(map(int, input().split()))
	if x in nums:
		print('YES')
		continue
	if min(nums) < x and max(nums) > x:
		print('YES')
	else:
		print('NO')


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	s = set()
	count1 = 0
	count2 = 0
	count3 = 0
	s = set(map(int, input().split()))
	for i in s:
		if i > x:
			count1 = count1 + 1
		elif i == x:
			count2 = count2 + 1
		else:
			count3 = count3 + 1
	if (count1 and count3) > 0 or count2 > 0:
		print('YES')
	else:
		print('NO')


T = int(input())
for a in range(T):
	(n, X) = map(int, input().split(' '))
	S = list(map(int, input().split(' ')))
	if X > max(S) or X < min(S):
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	(n, x) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	if x > max(a) or x < min(a):
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	arr = list(map(int, input().split()))
	n = arr[0]
	x = arr[1]
	v = list(map(int, input().split()))
	v.sort()
	if v[0] > x:
		print('NO')
	elif v[n - 1] < x:
		print('NO')
	else:
		print('YES')


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	l = list(map(lambda x: x - m, list(map(int, input().split()))))
	if min(l) > 0 or max(l) < 0:
		print('NO')
	else:
		print('YES')


import numpy

def sum_x(l, s):
	if len(l) == 0:
		return False
	elif len(l) == 1:
		return l[0] == s
	elif len(l) == 2:
		return l[0] == s or l[1] == s or l[0] + l[1] == s
	elif l[0] > s:
		return False
	else:
		for i in range(len(l)):
			new_l = l[:i] + l[i + 1:]
			if sum_x(new_l, s - l[i]):
				return True
		return False
t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	l = list(map(lambda x: x - m, list(map(int, input().split()))))
	if min(l) > 0 or max(l) < 0:
		print('NO')
	else:
		print('YES')


t = int(input())
for i in range(t):
	(n, x) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	if x in a:
		print('YES')
		continue
	a.append(x)
	a.sort()
	if a[0] == x or a[-1] == x:
		print('NO')
	else:
		print('YES')


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	c = list(map(int, input().split()))
	d = sorted(c)
	y = min(d)
	z = max(d)
	if y <= b and z >= b:
		print('YES')
	else:
		print('NO')


t = int(input())
while t > 0:
	(n, x) = map(int, input().split())
	s = sorted(list(map(int, input().split())))
	if min(s) <= x and max(s) >= x:
		print('YES')
	else:
		print('NO')
	t -= 1


t = int(input())
while t > 0:
	(n, x) = map(int, input().split())
	s = sorted(list(map(int, input().split())))
	if any((k == x for k in s)):
		print('YES')
	elif min(s) <= x and max(s) >= x:
		print('YES')
	else:
		print('NO')
	t -= 1


t = int(input())
for i in range(t):
	(n, x) = map(int, input().split())
	lstr = input()
	l = []
	for i in lstr.split(' '):
		l.append(int(i))
	if x in range(min(l), max(l) + 1):
		print('YES')
	else:
		print('NO')


loops = int(input())
for i in range(loops):
	nx = input()
	n = int(nx.split(' ')[0])
	x = int(nx.split(' ')[1])
	lstr = input()
	l = []
	for i in lstr.split(' '):
		l.append(int(i))
	if x in range(min(l), max(l) + 1):
		print('YES')
	else:
		print('NO')


i = int(input())
for _ in range(0, i):
	[n, x] = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	arr.sort()
	if arr[0] <= x and arr[-1] >= x:
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	(n, x) = map(int, input().split())
	s = list(map(int, input().split()))
	s.sort()
	if x in s:
		print('YES')
	elif max(s) < x:
		print('NO')
	elif min(s) > x:
		print('NO')
	else:
		print('YES')


def avg(l, m):
	return m in l or (any((li > m for li in l)) and any((li < m for li in l)))
for _ in range(int(input())):
	(_, m) = [int(x) for x in input().split()]
	l = [int(x) for x in input().split()]
	print('YES' if avg(l, m) else 'NO')


for _ in range(int(input())):
	(p, q) = map(int, input().split())
	r = [int(a) for a in input().split()]
	if q <= max(r) and q >= min(r):
		print('YES')
	else:
		print('NO')


for i in range(int(input())):
	(n, x) = map(int, input().split())
	l = sorted(map(int, input().split()))
	if x < l[0] or x > l[n - 1]:
		print('NO')
	else:
		print('YES')


for i in range(int(input())):
	(m, b) = map(int, input().split())
	l = list(map(int, input().split()))
	l.sort()
	if b < l[0] or b > l[m - 1]:
		print('NO')
	else:
		print('YES')


T = int(input())
for t in range(T):
	(N, X) = map(int, input().split())
	S = set(map(int, input().split()))
	D = {s - X for s in S}
	P = {d for d in D if d > 0}
	N = {d for d in D if d < 0}
	if 0 in D:
		print('YES')
	elif len(P) > 0 and len(N) > 0:
		print('YES')
	else:
		print('NO')


x = int(input())
for i in range(0, x):
	[a, b] = input().split(' ')
	a = int(a)
	b = int(b)
	r = input().split(' ')
	for i in range(0, a):
		r[i] = int(r[i])
	x1 = min(r)
	x2 = max(r)
	if b < x1 or b > x2:
		print('NO')
	else:
		print('YES')