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 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,…,�� - 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 YES, yEs, yes, and yeS will all be treated as identical).

Constraints

  • 1≤�≤1000
  • 1≤�≤105
  • 1≤��≤109
  • 1≤�≤109
  • ��≠�� for �≠�
  • Sum of � over all test case do not exceed 2⋅105.

Sample 1:

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 �=[2,2]. Here, 2∈{1,2,3}. Also, mean value of the array is 2+22=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 �=[4,6]. Here, 4∈{4,6} and 6∈{4,6}. Also, mean value of the array is 4+62=5.

Test Case 4: One of the valid arrays is �=[5]. Here, 5∈{5}. Also, mean value of the array is 51=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:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-