📌  相关文章
📜  选择位于给定范围内的 K 个数组元素的计数方法

📅  最后修改于: 2021-09-03 03:39:44             🧑  作者: Mango

给定三个正整数LRK和一个由N 个正整数组成的数组arr[] ,任务是计算从给定数组中选择至少K 个数组元素的方法的数量,数组元素的值在[L, R]

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个变量,比如cntWays ,以存储选择至少K 个值在[L, R]范围内的数组元素的方法的计数。
  • 初始化一个变量,比如cntNum来存储给定数组中其值位于给定范围内的数字计数。
  • 最后,打印所有可能值的总和{cntNum\choose k + i}        使得(K + i)小于或等于cntNum

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to calculate factorial
// of all the numbers up to N
vector calculateFactorial(int N)
{
    vector fact(N + 1);
 
    // Factorial of 0 is 1
    fact[0] = 1;
 
    // Calculate factorial of
    // all the numbers upto N
    for (int i = 1; i <= N; i++) {
 
        // Calculate factorial of i
        fact[i] = fact[i - 1] * i;
    }
 
    return fact;
}
 
// Function to find the count of ways to select
// at least K elements whose values in range [L, R]
int cntWaysSelection(int arr[], int N, int K,
                     int L, int R)
{
 
    // Stores count of ways to select at least
    // K elements whose values in range [L, R]
    int cntWays = 0;
 
    // Stores count of numbers having
    // value lies in the range [L, R]
    int cntNum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Checks if the array elements
        // lie in the given range
        if (arr[i] >= L && arr[i] <= R) {
 
            // Update cntNum
            cntNum++;
        }
    }
 
    // Stores factorial of numbers upto N
    vector fact
        = calculateFactorial(cntNum);
 
    // Calculate total ways to select at least
    // K elements whose values lies in [L, R]
    for (int i = K; i <= cntNum; i++) {
 
        // Update cntWays
        cntWays += fact[cntNum] / (fact[i]
                                   * fact[cntNum - i]);
    }
 
    return cntWays;
}
 
// Driver Code
int main()
{
    int arr[] = { 12, 4, 6, 13, 5, 10 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 3;
    int L = 4;
    int R = 10;
 
    cout << cntWaysSelection(arr, N, K, L, R);
}


Java
// Java program to implement
// the above approach
class GFG{
 
// Function to calculate factorial
// of all the numbers up to N
static int[] calculateFactorial(int N)
{
    int []fact = new int[N + 1];
 
    // Factorial of 0 is 1
    fact[0] = 1;
 
    // Calculate factorial of
    // all the numbers upto N
    for (int i = 1; i <= N; i++) {
 
        // Calculate factorial of i
        fact[i] = fact[i - 1] * i;
    }
 
    return fact;
}
 
// Function to find the count of ways to select
// at least K elements whose values in range [L, R]
static int cntWaysSelection(int arr[], int N, int K,
                     int L, int R)
{
 
    // Stores count of ways to select at least
    // K elements whose values in range [L, R]
    int cntWays = 0;
 
    // Stores count of numbers having
    // value lies in the range [L, R]
    int cntNum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // Checks if the array elements
        // lie in the given range
        if (arr[i] >= L && arr[i] <= R) {
 
            // Update cntNum
            cntNum++;
        }
    }
 
    // Stores factorial of numbers upto N
    int []fact
        = calculateFactorial(cntNum);
 
    // Calculate total ways to select at least
    // K elements whose values lies in [L, R]
    for (int i = K; i <= cntNum; i++) {
 
        // Update cntWays
        cntWays += fact[cntNum] / (fact[i]
                                   * fact[cntNum - i]);
    }
 
    return cntWays;
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 12, 4, 6, 13, 5, 10 };
    int N = arr.length;
    int K = 3;
    int L = 4;
    int R = 10;
 
    System.out.print(cntWaysSelection(arr, N, K, L, R));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to implement the
# above approach
 
# Function to calculate factorial
# of all the numbers up to N
def calculateFactorial(N):
 
    fact = [0] * (N + 1)
 
    # Factorial of 0 is 1
    fact[0] = 1
 
    # Calculate factorial of all
    # the numbers upto N
    for i in range(1, N + 1):
 
        # Calculate factorial of i
        fact[i] = fact[i - 1] * i
         
    return fact
 
# Function to find count of ways to select
# at least K elements whose values in range[L,R]
def cntWaysSelection(arr, N, K, L, R):
     
    # Stores count of ways to select at leas
    # K elements whose values in range[L,R]
    cntWays = 0
 
    # Stores count of numbers having
    # Value lies in the range[L,R]
    cntNum = 0
 
    # Traverse the array
    for i in range(0, N):
         
        # Check if the array elements
        # Lie in the given range
        if (arr[i] >= L and arr[i] <= R):
             
            # Update cntNum
            cntNum += 1
 
    # Stores factorial of numbers upto N
    fact = list(calculateFactorial(cntNum))
 
    # Calculate total ways to select at least
    # K elements whose values Lies in [L,R]
    for i in range(K, cntNum + 1):
         
        # Update cntWays
        cntWays += fact[cntNum] // (fact[i] *
                                    fact[cntNum - i])
                                     
    return cntWays
 
# Driver code
if __name__ == "__main__":
     
    arr = [ 12, 4, 6, 13, 5, 10 ]
    N = len(arr)
    K = 3
    L = 4
    R = 10
     
    print(cntWaysSelection(arr, N, K, L, R))
 
# This code is contributed by Virusbuddah


C#
// C# program to implement
// the above approach
using System;
  
class GFG{
  
// Function to calculate factorial
// of all the numbers up to N
static int[] calculateFactorial(int N)
{
    int[] fact = new int[(N + 1)];
     
    // Factorial of 0 is 1
    fact[0] = 1;
     
    // Calculate factorial of
    // all the numbers upto N
    for(int i = 1; i <= N; i++)
    {
         
        // Calculate factorial of i
        fact[i] = fact[i - 1] * i;
    }
    return fact;
}
  
// Function to find the count of ways to select
// at least K elements whose values in range [L, R]
static int cntWaysSelection(int[] arr, int N, int K,
                            int L, int R)
{
     
    // Stores count of ways to select at least
    // K elements whose values in range [L, R]
    int cntWays = 0;
     
    // Stores count of numbers having
    // value lies in the range [L, R]
    int cntNum = 0;
     
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // Checks if the array elements
        // lie in the given range
        if (arr[i] >= L && arr[i] <= R)
        {
             
            // Update cntNum
            cntNum++;
        }
    }
  
    // Stores factorial of numbers upto N
    int[] fact = calculateFactorial(cntNum);
  
    // Calculate total ways to select at least
    // K elements whose values lies in [L, R]
    for(int i = K; i <= cntNum; i++)
    {
         
        // Update cntWays
        cntWays += fact[cntNum] / (fact[i] *
                   fact[cntNum - i]);
    }
    return cntWays;
}
  
// Driver Code
public static void Main()
{
    int[] arr = { 12, 4, 6, 13, 5, 10 };
    int N = arr.Length;
    int K = 3;
    int L = 4;
    int R = 10;
     
    Console.WriteLine(cntWaysSelection(
        arr, N, K, L, R));
}
}
 
// This code is contributed by code_hunt


Javascript


输出:
5

时间复杂度: O(N)
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live