Setter: Utkarsh Gupta
Tester: Manan Grover, Abhinav sharma
Editorialist: Devendra Singh

1701

# PREREQUISITES:

The average of N values is : \displaystyle\frac{Sum\:of\:all\:values}{N}

# PROBLEM:

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.

# EXPLANATION:

Let min be the minimum of N given integers and max be the maximum of N given integers, then the average of these N integers always lies in the range [min, max]. Therefore if min>X or max< X the answer is NO. Otherwise the answer is always Yes.

Proof that the answer is always yes for the latter scenario:

Case 1: X is present in S, take a single occurrence of X, it has an average of X.
Case 2: X is not present in S,
Let a=X-min and b=max-X.
Then \displaystyle\frac{(a\cdot max + b\cdot min)}{(a + b)} = (\displaystyle\frac{(max-X)\cdot min+(X-min)\cdot max)}{(max-X+X-min)}=\displaystyle\frac{X\cdot (max-min)}{(max-min)} = X.

This means we can take a occurrences of the maximum element in S and b occurrences of the minimum element in S to get an average of X.

Thus, if min\leq X\leq max the answer is always Yes else No

# TIME COMPLEXITY:

O(N) for each test case.

# SOLUTION:

Setter's solution
//Utkarsh.25dec
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd){
if(is_neg){
x= -x;
}

if(!(l <= x && x <= r))
{
cerr << l << ' ' << r << ' ' << x << '\n';
assert(1 == 0);
}

return x;
} else {
assert(false);
}
}
}
string ret="";
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
}
long long readIntLn(long long l,long long r){
}
}
}
int sumN=0;
void solve()
{
sumN+=n;
assert(sumN<=200000);
set <ll> s;
int maxi=0,mini=mod;
for(int i=1;i<=n;i++)
{
int c;
if(i==n)
else
mini=min(mini,c);
maxi=max(maxi,c);
s.insert(c);
}
assert(s.size()==n);
if(x>=mini && x<=maxi)
cout<<"YES\n";
else
cout<<"NO\n";
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
//cin>>T;
while(T--)
solve();
assert(getchar()==-1);
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}

Editorialist's Solution
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define pb push_back
#define all(_obj) _obj.begin(), _obj.end()
#define F first
#define S second
#define pll pair<ll, ll>
#define vll vector<ll>
const int N = 1e5 + 11, mod = 1e9 + 7;
ll max(ll a, ll b) { return ((a > b) ? a : b); }
ll min(ll a, ll b) { return ((a > b) ? b : a); }
void sol(void)
{
int n, x, mi = 1e9, mx = 1;
cin >> n >> x;
vll v(n);
for (int i = 0; i < n; i++)
{
cin >> v[i];
mx = max(mx, v[i]);
mi = min(mi, v[i]);
}
cout<<((mx>=x && mi<=x)?"YES\n":"NO\n");
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
int test = 1;
cin >> test;
while (test--)
sol();
}

7 Likes

The mathematical equation explaining the case when minimum<=X<=maximum is not intuitive enough. Can you explain? Thanks

16 Likes

Please care to elaborate, how you arrived at the conclusion using the maths done in step 2?

Edit: Just now realising that we could take multiple occurance of the same number from set. sed lyf.

7 Likes

One of the ways to think is if you subtract X from each value of the set, the question changes to whether you can achieve an average of 0 which means a sum of 0
If we have a 0 now, just take it otherwise we can just take x and y values where x is the minimum and y is the maximum value in the set. Now to solve ax+by = 0 where a and b are positive integers put a=y and b=-x to get a sum of 0. If x is negative and y is positive then the answer always exists otherwise if both x and y are negative or positive then the sum can’t be 0 as a and b are positive and the answer does not exist
.

13 Likes

i just got the approach and will try my best to explain it…
suppose you have s={-1,5} and want to achieve mean of 0.
then we can take 5 occurrences of -1 and 1 occurrence of 5
so -1-1-1-1-1+5=0
similarly, let s={2,6,11} then x=7
then take 4 occurrences of 2 and 5 occurrences of 11
then mean= (42+511)/(4+5) => 63/9=> 7

Note: try subtracting x from every elem from set S then problem will be reduced to make sum 0 using some elements from set s

Let us take two elements a and b from the set S and form the equation

(q1 * (a) + q2 * (b))/(q1 + q2) = x , such that q1 , q2 belong to Z+ and atleast one of them must be non zero

which now can be changed to → q1 * (a-x) + q2 * (b-x) = 0; — (1)

Now what if x is less than minimum of the set S ?
Then both q1 * (a-x) and q2 * (b-x) would add up to become greater than 0

Now what if x is greater than maximum of the set S?
Then both q1 * (a-x) and q2 * (b-x) would add up to become less than 0

So to make the solution equal to zero , minimum(S) <= x <= maximum(S)

Now the explanation is clear perhaps why we should take x between those ranges for solution to come up.

Now moving to the second part of solving these equations to reach the value , we won’t be needing 2 cases like the ones showed in the editorial

We can take a single case .
Let us assume a is the maximum element present in set S and b is the minimum element present in set S

So now the equation 1 becomes

q1 * (max - x) + q2 * (min -x) = 0

q1/q2 = (x - min)/(max - x)

Now as we need postitive integral values of q1 and q2 and we already stated the solution would exist for x>=minimum(S) and x<=maximum(S) ,

q1 = (x - min) and q2 = (max - x) ( both would be positive integers )

1 Like

I also solved this question using python
try:
for _ in range(int(input())):
n,x=map(int,input().split())
elements=list(map(int,input().split()))
l=sorted(elements)
if (x < l[0] or x > l[n - 1]):
print(“NO”)
else:
print(“YES”)
except:
pass

we can simply check the bound range and then its easily done and is accepted in the contest

n=3 and x=5;
array is 2 ,4, 12

if [1, 5] is the array and X = 4, then how can we achieve the average equal to 4 ?
Since mn(=1) < 4 and mx(=5) > 4 but I can’t a valid answer.
Can someone help me here?

This one is valid, A= \{1,5,5,5\}

1 Like

hmm, I read the question again. Didn’t knew that you can pick the elements more than once…

(12+4+2+2)/4=5

It should have been properly specified that the same elements can be taken again…

what about n = 3 & x = 9
1, 3 ,10

Take {1,10,10,10,10,10,10,10,10}. sum = 81, avg = (81/9) = 9

thank you