# Average Gift | codechef solution

## Problem

Chef has a set $�$ containing $�$ distinct integers.

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

• ${�}_{�}\in �$ $\mathrm{\forall }�$. In other words, each element of the array $�$ should belong to the set $�$.
• Mean value of all the elements in $�$ is exactly $�$.

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

### Input Format

• First line will contain $�$, the number of test cases. Then the test cases follow.
• First line of each test case contains two integers $�$ and $�$ - denoting the size of set $�$ and the mean value of the required array.
• Second line contains $�$ distinct integers ${�}_{1},{�}_{2},\dots ,{�}_{�}$ - denoting the set $�$.

### 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 YESyEsyes, and yeS will all be treated as identical).

### Constraints

• $1\le �\le 1000$
• $1\le �\le 1{0}^{5}$
• $1\le {�}_{�}\le 1{0}^{9}$
• $1\le �\le 1{0}^{9}$
• ${�}_{�}\mathrm{\ne }{�}_{�}$ for $�\mathrm{\ne }�$
• Sum of $�$ over all test case do not exceed $2\cdot 1{0}^{5}$.

Input
Output
4
3 2
1 2 3
1 5
3
2 5
4 6
1 5
5
YES
NO
YES
YES

### Explanation:

Test Case $1$: One of the valid arrays is $�=\left[2,2\right]$. Here, $2\in \left\{1,2,3\right\}$. 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 $�$ cannot be equal to $5$.

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

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

=5

Code(C++);-

#include<bits/stdc++.h>
using namespace std;
int main()
{ int t;
cin>>t;
while(t--)
{ int n,x;
cin>>n>>x;
vector<int>arr(n);
for(int i=0;i<n;i++)
cin>>arr[i];
int m1=*max_element(arr.begin(),arr.end());
int m2=*min_element(arr.begin(),arr.end());
if(x<=m1&&x>=m2)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}

Code(JAVA):-

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc =new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int x = sc.nextInt();
int mini, maxi;
mini = sc.nextInt();
maxi = mini;
for(int i=0; i<n-1; ++i) {
int tmp = sc.nextInt();
mini = Math.min(tmp, mini);
maxi = Math.max(tmp, maxi);
}
if(x<=maxi && x>=mini) System.out.println("YES");
else System.out.println("NO");
}
}
}

### Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-
iMocha coding Questions:-
Tech Mahindra coding questions:-
Unthinkable Solutions coding questions:-