📌  相关文章
📜  在平均分配数组子集后,大于X的最大元素数

📅  最后修改于: 2021-06-25 11:53:23             🧑  作者: Mango

给定一个数组arr []和一个整数X ,任务是在均等地划分元素子集之后计算大于X的元素数量。那就是子集的每个元素将等于\frac{Sum of Subset}{Number of Elements}

例子:

方法:想法是对数组进行排序,并包括数组中最大的元素,以使它们的平均值大于或等于X。平均值大于或等于X的元素的计数是所需的子集,该子集可以相等被除,并且每个元素都大于X。

下面是上述方法的实现:

C++
// C++ implementation to find the
// maximum number of elements greater
// than X by equally distributing
 
#include 
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 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;
}


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


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.


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


Javascript


输出:
2