Given an array, **arr[]** and an integer **X**, the task is to count the number of elements greater than X after equally dividing the subset of elements. That is each element of the subset will be equal to

**Examples:**

Input:arr[] = {5, 1, 2, 1}, X = 3

Output:2

Explanation:

Subset which is equally distributes is {5, 2}.

After which the elements will be 3.5 each.

Array => {3.5, 1, 3.5, 1}

Total number of elements greater than X = 2

Input:arr[] = {3, 4, 5}, X = 6

Output:0

Explanation:

There is no way to distribute any subset of array to make the elements greater than 6.

**Approach:** The idea is to sort the array and include the largest elements of the array such that their average is greater than or equal to X. Count of such elements whose average is greater than or equal to X is the desired subset which can be equally divided and each element is greater than X.

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the ` `// maximum number of elements greater ` `// than X by equally distributing ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the ` `// maximum number of elements greater ` `// than X by equally distributing ` `void` `redistribute(` `int` `arr[], ` `int` `n, ` `int` `x) ` `{ ` ` ` `// Sorting the array ` ` ` `sort(arr, arr + n, greater<` `int` `>()); ` ` ` ` ` `int` `i, sum = 0; ` ` ` ` ` `// Loop to iterate over the elements ` ` ` `// of the array ` ` ` `for` `(i = 0; i < n; i++) { ` ` ` `sum += arr[i]; ` ` ` ` ` `// If no more elements can ` ` ` `// become larger than x ` ` ` `if` `(sum / (i + 1) < x) { ` ` ` `cout << i << endl; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `if` `(i == n) ` ` ` `cout << n << endl; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 5, 1, 2, 1 }; ` ` ` `int` `x = 3; ` ` ` `redistribute(arr, 4, x); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// maximum number of elements greater ` `// than X by equally distributing ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// number of elements greater ` `// than X by equally distributing ` `static` `void` `redistribute(Integer arr[], ` `int` `n, ` ` ` `int` `x) ` `{ ` ` ` ` ` `// Sorting the array ` ` ` `Arrays.sort(arr, Collections.reverseOrder()); ` ` ` ` ` `int` `i, sum = ` `0` `; ` ` ` ` ` `// Loop to iterate over the elements ` ` ` `// of the array ` ` ` `for` `(i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `sum += arr[i]; ` ` ` ` ` `// If no more elements can ` ` ` `// become larger than x ` ` ` `if` `(sum / (i + ` `1` `) < x) ` ` ` `{ ` ` ` `System.out.print(i + ` `"\n"` `); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `if` `(i == n) ` ` ` `System.out.print(n + ` `"\n"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `Integer arr[] = { ` `5` `, ` `1` `, ` `2` `, ` `1` `}; ` ` ` `int` `x = ` `3` `; ` ` ` ` ` `redistribute(arr, ` `4` `, x); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# maximum number of elements greater ` `# than X by equally distributing ` ` ` `# Function to find the ` `# maximum number of elements greater ` `# than X by equally distributing ` `def` `redistribute(arr, n, x): ` ` ` ` ` `# Sorting the array ` ` ` `arr.sort(reverse ` `=` `True` `) ` ` ` ` ` `sum` `=` `0` ` ` ` ` `# Loop to iterate over the ` ` ` `# elements of the array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `sum` `+` `=` `arr[i] ` ` ` ` ` `# If no more elements can ` ` ` `# become larger than x ` ` ` `if` `(` `sum` `/` `(i ` `+` `1` `) < x): ` ` ` `print` `(i) ` ` ` `break` ` ` ` ` `if` `(i ` `=` `=` `n): ` ` ` `print` `(n) ` ` ` `# Driver Code ` `arr ` `=` `[ ` `5` `, ` `1` `, ` `2` `, ` `1` `] ` `x ` `=` `3` ` ` `# Function call ` `redistribute(arr, ` `4` `, x) ` ` ` `# This code is contributed by Vishal Maurya. ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// maximum number of elements greater ` `// than X by equally distributing ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the maximum ` `// number of elements greater ` `// than X by equally distributing ` `static` `void` `redistribute(` `int` `[]arr, ` `int` `n, ` ` ` `int` `x) ` `{ ` ` ` ` ` `// Sorting the array ` ` ` `Array.Sort(arr); ` ` ` `Array.Reverse(arr); ` ` ` ` ` `int` `i, sum = 0; ` ` ` ` ` `// Loop to iterate over the elements ` ` ` `// of the array ` ` ` `for` `(i = 0; i < n; i++) ` ` ` `{ ` ` ` `sum += arr[i]; ` ` ` ` ` `// If no more elements can ` ` ` `// become larger than x ` ` ` `if` `(sum / (i + 1) < x) ` ` ` `{ ` ` ` `Console.Write(i + ` `"\n"` `); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `if` `(i == n) ` ` ` `Console.Write(n + ` `"\n"` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 5, 1, 2, 1 }; ` ` ` `int` `x = 3; ` ` ` ` ` `redistribute(arr, 4, x); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Equally divide into two sets such that one set has maximum distinct elements
- Maximum number of chocolates to be distributed equally among k students
- Smallest subset with sum greater than all other elements
- Smallest subarray of size greater than K with sum greater than a given value
- Find maximum subset sum formed by partitioning any subset of array into 2 partitions with equal sum
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Sum of all array elements less than X and greater than Y for Q queries
- Sum of maximum and minimum of Kth subset ordered by increasing subset sum
- Minimum positive integer required to split the array equally
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Find the element before which all the elements are smaller than it, and after which all are greater
- Largest possible Subset from an Array such that no element is K times any other element in the Subset
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Maximum Subset Sum possible by negating the entire sum after selecting the first Array element
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Merge first two minimum elements of the array until all the elements are greater than K
- Reduce the array by deleting elements which are greater than all elements to its left

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.