📌  相关文章
📜  从给定数组中选择具有最大总和的 K 个元素的方法计数

📅  最后修改于: 2022-05-13 01:56:06.505000             🧑  作者: Mango

从给定数组中选择具有最大总和的 K 个元素的方法计数

给定一个大小为N的数组arr[]和一个整数K ,任务是找出选择K个数组元素的方法的数量,使得这些K个元素的总和是最大可能的总和。

例子:

方法:可以通过对数组进行降序排序来解决问题。请按照以下步骤解决问题:

  • 按降序对数组进行排序。
  • 计算第K元素的次数,在数组的K-1的前缀中,然后将其存储在一个变量中,比如说P。
  • 计算数组中第K元素的次数,然后将其存储在变量中,例如Q。
  • 最后,打印的值 路数 从Q个元素中选择P个元素,即C(Q, P)作为答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the factorial of an
// integer
int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
 
// Function to find the value of nCr
int C(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
 
// Function to find the number of ways
// to select K elements with maximum
// sum
int findWays(int arr[], int N, int K)
{
    // Sort the array in descending order
    sort(arr, arr + N, greater());
 
    // Stores the frequency of arr[K-1]
    // in the prefix of K-1
    int p = 0;
 
    // Stores the frequency of arr[K-1]
    // in the array arr[]
    int q = 0;
 
    // Iterate over the range [0, K]
    for (int i = 0; i < K; i++) {
        // If arr[i] is equal to arr[K-1]
        if (arr[i] == arr[K - 1]) {
            p++;
        }
    }
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++) {
        // If arr[i] is equal to arr[K-1]
        if (arr[i] == arr[K - 1]) {
            q += 1;
        }
    }
    // Stores the number of ways of
    // selecting p from q elements
    int ans = C(q, p);
 
    // Return ans
    return ans;
}
 
// Driver Code
int main()
{
    // Input
    int arr[] = { 2, 3, 4, 5, 2, 2 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 4;
 
    // Function call
    cout << findWays(arr, N, K);
    return 0;
}


Java
// Java program for the above approach
 
import java.util.*;
 
class GFG{
 
// Function to find the factorial of an
// integer
static int fact(int n)
{
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
 
// Function to find the value of nCr
static int C(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
 
// Function to find the number of ways
// to select K elements with maximum
// sum
static int findWays(Integer arr[], int N, int K)
{
   
    // Sort the array in descending order
    Arrays.sort(arr, Collections.reverseOrder());
 
    // Stores the frequency of arr[K-1]
    // in the prefix of K-1
    int p = 0;
 
    // Stores the frequency of arr[K-1]
    // in the array arr[]
    int q = 0;
 
    // Iterate over the range [0, K]
    for (int i = 0; i < K; i++)
    {
       
        // If arr[i] is equal to arr[K-1]
        if (arr[i] == arr[K - 1]) {
            p++;
        }
    }
 
    // Traverse the array arr[]
    for (int i = 0; i < N; i++)
    {
       
        // If arr[i] is equal to arr[K-1]
        if (arr[i] == arr[K - 1]) {
            q += 1;
        }
    }
   
    // Stores the number of ways of
    // selecting p from q elements
    int ans = C(q, p);
 
    // Return ans
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Input
    Integer arr[] = { 2, 3, 4, 5, 2, 2 };
    int N = arr.length;
    int K = 4;
 
    // Function call
    System.out.print(findWays(arr, N, K));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python for the above approach
 
# Function to find the factorial of an
# integer
def fact(n):
    res = 1
    for i in range(2, n + 1):
        res = res * i
 
    return res
 
# Function to find the value of nCr
def C(n, r):
    return fact(n) / (fact(r) * fact(n - r))
 
# Function to find the number of ways
# to select K elements with maximum
# sum
def findWays(arr, N, K):
 
    # Sort the array in descending order
    arr.sort(reverse=True)
 
    # Stores the frequency of arr[K-1]
    # in the prefix of K-1
    p = 0
 
    # Stores the frequency of arr[K-1]
    # in the array arr[]
    q = 0
 
    # Iterate over the range [0, K]
    for i in range(K):
 
        # If arr[i] is equal to arr[K-1]
        if (arr[i] == arr[K - 1]):
            p += 1
 
    # Traverse the array arr[]
    for i in range(N):
 
        # If arr[i] is equal to arr[K-1]
        if (arr[i] == arr[K - 1]):
            q += 1
 
    # Stores the number of ways of
    # selecting p from q elements
    ans = C(q, p)
 
    # Return ans
    return int(ans)
 
# Driver Code
 
 
# Input
arr = [2, 3, 4, 5, 2, 2]
N = len(arr)
K = 4
 
# Function call
print(findWays(arr, N, K))
 
# This code is contributed by gfgking.


C#
// C# program for the above approach
using System;
 
class Program{
     
// Function to find the factorial of an
// integer
static int fact(int n)
{
    int res = 1;
    for(int i = 2; i <= n; i++)
        res = res * i;
         
    return res;
}
 
// Function to find the value of nCr
static int C(int n, int r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
 
// Function to find the number of ways
// to select K elements with maximum
// sum
static int findWays(int []arr, int N, int K)
{
     
    // Sort the array in descending order
    Array.Sort(arr);
    Array.Reverse(arr);
 
    // Stores the frequency of arr[K-1]
    // in the prefix of K-1
    int p = 0;
 
    // Stores the frequency of arr[K-1]
    // in the array arr[]
    int q = 0;
 
    // Iterate over the range [0, K]
    for(int i = 0; i < K; i++)
    {
         
        // If arr[i] is equal to arr[K-1]
        if (arr[i] == arr[K - 1])
        {
            p++;
        }
    }
 
    // Traverse the array arr[]
    for(int i = 0; i < N; i++)
    {
         
        // If arr[i] is equal to arr[K-1]
        if (arr[i] == arr[K - 1])
        {
            q += 1;
        }
    }
   
    // Stores the number of ways of
    // selecting p from q elements
    int ans = C(q, p);
 
    // Return ans
    return ans;
}
 
// Driver code
static void Main()
{
    int []arr = { 2, 3, 4, 5, 2, 2 };
    int N = arr.Length;
    int K = 4;
     
    // Function call
    Console.Write(findWays(arr, N, K));
}
}
 
// This code is contributed by SoumikMondal


Javascript


输出
3

时间复杂度: O(N*log(N) + K)
辅助空间: O(1)