Check if Array with mean X can be made using N elements of given Array
Last Updated :
23 Jul, 2025
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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is possible
// to create the array satisfying the conditions
string isPossible(int N, int X, int arr[])
{
// Min variable to store minimum
// value from input arr[]
int sz = sizeof(arr) / sizeof(arr[0]);
int min = INT_MAX;
// Max variable to store maximum
// value from input arr[]
int max = INT_MIN;
// Loop for iterating on arr[]
for (int i = 0; i < sz; i++) {
// Updating value of min variable
min = arr[i] < min ? arr[i] : min;
// Updating value of max variable
max = arr[i] > max ? arr[i] : max;
}
// Printing answer as YES/NO by
// comparing mean with min and max
// values
return X >= min && X <= max ? "YES" : "NO";
}
// Driver Code
int main()
{
int N = 5, X = 8;
int arr[] = { 1, 10, 3, 2, 6, 7, 4, 5 };
// Function call
cout << isPossible(N, X, arr);
return 0;
}
// This code is contributed by rohit768.
Java
// Java code to implement the approach
import java.io.*;
// Driver Class
class GFG {
// Driver code
public static void main(String[] args)
{
int N = 5, X = 8;
int[] arr = { 1, 10, 3, 2, 6, 7, 4, 5 };
// Function call
System.out.println(isPossible(N, X, arr));
}
// Function to check if it is possible
// to create the array satisfying the conditions
static String isPossible(int N, int X, int arr[])
{
// Min variable to store minimum
// value from input arr[]
int min = Integer.MAX_VALUE;
// Max variable to store maximum
// value from input arr[]
int max = Integer.MIN_VALUE;
// Loop for iterating on arr[]
for (int i = 0; i < arr.length; i++) {
// Updating value of min variable
min = arr[i] < min ? arr[i] : min;
// Updating value of max variable
max = arr[i] > max ? arr[i] : max;
}
// Printing answer as YES/NO by
// comparing mean with min and max
// values
return X >= min && X <= max ? "YES" : "NO";
}
}
Python3
# python code to implement the approach
# Function to check if it is possible
# to create the array satisfying the conditions
def isPossible(N, X, arr):
# Min variable to store minimum
# value from input arr[]
sz = len(arr)
mini = 9223372036854775807
# Max variable to store maximum
# value from input arr[]
maxi = -9223372036854775808
# Loop for iterating on arr[]
for i in range(0, sz):
# Updating value of min variable
if(arr[i] < mini):
mini = arr[i]
# Updating value of max variable
if(arr[i] > maxi):
maxi = arr[i]
# Print answer as YES/NO by
# comparing mean with min and max
# values
if(X >= mini and X <= maxi):
return "YES"
else:
return "NO"
# Driver Code
N = 5
X = 8
arr = [1, 10, 3, 2, 6, 7, 4, 5]
# Function call
print(isPossible(N, X, arr))
# This code is contributed by ksam24000
C#
// C# code to implement the approach
using System;
// Driver Class
class GFG {
// Driver code
public static void Main()
{
int N = 5, X = 8;
int[] arr = { 1, 10, 3, 2, 6, 7, 4, 5 };
// Function call
Console.WriteLine(isPossible(N, X, arr));
}
// Function to check if it is possible
// to create the array satisfying the conditions
static string isPossible(int N, int X, int[] arr)
{
// Min variable to store minimum
// value from input arr[]
int min = Int32.MaxValue;
// Max variable to store maximum
// value from input arr[]
int max = Int32.MinValue;
// Loop for iterating on arr[]
for (int i = 0; i < arr.Length; i++) {
// Updating value of min variable
min = arr[i] < min ? arr[i] : min;
// Updating value of max variable
max = arr[i] > max ? arr[i] : max;
}
// Printing answer as YES/NO by
// comparing mean with min and max
// values
return X >= min && X <= max ? "YES" : "NO";
}
}
// This code is contributed by Samim Hossain Mondal.
JavaScript
// JS code to implement the approach
// Function to check if it is possible
// to create the array satisfying the conditions
function isPossible(N,X,arr)
{
// Min variable to store minimum
// value from input arr[]
let sz = arr.length;
let mn = Number.MAX_VALUE;
// Max variable to store maximum
// value from input arr[]
let mx = Number.MIN_VALUE;
// Loop for iterating on arr[]
for (let i = 0; i < sz; i++) {
// Updating value of min variable
mn = arr[i] < mn ? arr[i] : mn;
// Updating value of max variable
mx = arr[i] > mx ? arr[i] : mx;
}
// Printing answer as YES/NO by
// comparing mean with min and max
// values
return X >= mn && X <= mx ? "YES" : "NO";
}
// Driver Code
let N = 5, X = 8;
let arr = [ 1, 10, 3, 2, 6, 7, 4, 5 ];
// Function call
console.log(isPossible(N, X, arr));
// This code is contributed by ksam24000.
Time Complexity: O(N)
Auxiliary Space: O(1)
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem