Check if Array with mean X can be made using N elements of given Array
Given an array arr[] and two integers N and X, the task is to find if it is possible to create an array using N distinct elements from arr[] such that the mean of the newly formed array is X.
Examples:
Input: N = 5, X = 8, arr[] = {1, 10, 3, 2, 6, 7, 4, 5}
Output: YES
Explanation: Many arrays using 5 distinct elements from the array are possible like {10, 6, 7, 4, 5, 10, 10, 10, 10}
Input: N = 3, X = 4, arr[] = {9, 7, 5}
Output: NO
Explanation: There is no possible array by a given finite set. So the mean of the array becomes exact to X.Therefore, the answer is NO.
Approach: Implement the idea below to solve the problem:
It is always possible to make mean X from a given arr[] of the integer if it lies in between the minimum and maximum integer in an arr[]. Formally, if ( X >= MinValue && X <= MaxValue ) then, the answer will be YES only for those cases otherwise NO.
Steps were taken to solve the problem:
- Create an integer variable min and store the minimum value in this variable by iterating on arr[].
- Create an integer variable max and store the maximum value in this variable by iterating on arr[].
- if X lies in the range of min and max value of arr[], Then print YES else print NO.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
string isPossible( int N, int X, int arr[])
{
int sz = sizeof (arr) / sizeof (arr[0]);
int min = INT_MAX;
int max = INT_MIN;
for ( int i = 0; i < sz; i++) {
min = arr[i] < min ? arr[i] : min;
max = arr[i] > max ? arr[i] : max;
}
return X >= min && X <= max ? "YES" : "NO" ;
}
int main()
{
int N = 5, X = 8;
int arr[] = { 1, 10, 3, 2, 6, 7, 4, 5 };
cout << isPossible(N, X, arr);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 5 , X = 8 ;
int [] arr = { 1 , 10 , 3 , 2 , 6 , 7 , 4 , 5 };
System.out.println(isPossible(N, X, arr));
}
static String isPossible( int N, int X, int arr[])
{
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for ( int i = 0 ; i < arr.length; i++) {
min = arr[i] < min ? arr[i] : min;
max = arr[i] > max ? arr[i] : max;
}
return X >= min && X <= max ? "YES" : "NO" ;
}
}
|
Python3
def isPossible(N, X, arr):
sz = len (arr)
mini = 9223372036854775807
maxi = - 9223372036854775808
for i in range ( 0 , sz):
if (arr[i] < mini):
mini = arr[i]
if (arr[i] > maxi):
maxi = arr[i]
if (X > = mini and X < = maxi):
return "YES"
else :
return "NO"
N = 5
X = 8
arr = [ 1 , 10 , 3 , 2 , 6 , 7 , 4 , 5 ]
print (isPossible(N, X, arr))
|
C#
using System;
class GFG {
public static void Main()
{
int N = 5, X = 8;
int [] arr = { 1, 10, 3, 2, 6, 7, 4, 5 };
Console.WriteLine(isPossible(N, X, arr));
}
static string isPossible( int N, int X, int [] arr)
{
int min = Int32.MaxValue;
int max = Int32.MinValue;
for ( int i = 0; i < arr.Length; i++) {
min = arr[i] < min ? arr[i] : min;
max = arr[i] > max ? arr[i] : max;
}
return X >= min && X <= max ? "YES" : "NO" ;
}
}
|
Javascript
function isPossible(N,X,arr)
{
let sz = arr.length;
let mn = Number.MAX_VALUE;
let mx = Number.MIN_VALUE;
for (let i = 0; i < sz; i++) {
mn = arr[i] < mn ? arr[i] : mn;
mx = arr[i] > mx ? arr[i] : mx;
}
return X >= mn && X <= mx ? "YES" : "NO" ;
}
let N = 5, X = 8;
let arr = [ 1, 10, 3, 2, 6, 7, 4, 5 ];
console.log(isPossible(N, X, arr));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
26 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...