Open In App

Mean of minimum of all possible K-size subsets from first N natural numbers

Last Updated : 17 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given two positive integers N and K, the task is to find the mean of the minimum of all possible subsets of size K from the first N natural numbers.

Examples:

Input: N = 3, K = 2
Output: 1.33333
Explanation:
All possible subsets of size K are {1, 2}, {1, 3}, {2, 3}. The minimum values in all the subsets are 1, 1, and 2 respectively. The mean of all the minimum values is (1 + 1 + 2)/3 = 1.3333.

Input: N = 3, K = 1
Output: 2.00000

Naive Approach: The simplest approach to solve the given problem is to find all the subsets formed from elements [1, N] and find the mean of all the minimum elements of the subsets of size K.

Time Complexity: O(N*2N)
Auxiliary Space: O(1)

Efficient Approach: The above approach can also be optimized by observing the fact that the total number of subsets formed of size K is given by NCK and each element say i occurs (N – i)C(K – 1) number of times as the minimum element.

Therefore, the idea is to find the sum of (N – iCK – 1))*i over all possible values of i and divide it by the total number of subsets formed i.e., NCK.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the value of nCr
int nCr(int n, int r, int f[])
{
    // Base Case
    if (n < r) {
        return 0;
    }
 
    // Find nCr recursively
    return f[n] / (f[r] * f[n - r]);
}
 
// Function to find the expected minimum
// values of all the subsets of size K
int findMean(int N, int X)
{
    // Find the factorials that will
    // be used later
    int f[N + 1];
    f[0] = 1;
 
    // Find the factorials
    for (int i = 1; i <= N; i++) {
        f[i] = f[i - 1] * i;
    }
 
    // Total number of subsets
    int total = nCr(N, X, f);
 
    // Stores the sum of minimum over
    // all possible subsets
    int count = 0;
 
    // Iterate over all possible minimum
    for (int i = 1; i <= N; i++) {
        count += nCr(N - i, X - 1, f) * i;
    }
 
    // Find the mean over all subsets
    double E_X = double(count) / double(total);
 
    cout << setprecision(10) << E_X;
 
    return 0;
}
 
// Driver Code
int main()
{
    int N = 3, X = 2;
    findMean(N, X);
 
    return 0;
}


Java




// Java program for the above approach
 
import java.util.*;
 
class GFG {
 
    // Function to find the value of nCr
    static int nCr(int n, int r, int f[]) {
        // Base Case
        if (n < r) {
            return 0;
        }
 
        // Find nCr recursively
        return f[n] / (f[r] * f[n - r]);
    }
 
    // Function to find the expected minimum
    // values of all the subsets of size K
    static int findMean(int N, int X) {
        // Find the factorials that will
        // be used later
        int[] f = new int[N + 1];
        f[0] = 1;
 
        // Find the factorials
        for (int i = 1; i <= N; i++) {
            f[i] = f[i - 1] * i;
        }
 
        // Total number of subsets
        int total = nCr(N, X, f);
 
        // Stores the sum of minimum over
        // all possible subsets
        int count = 0;
 
        // Iterate over all possible minimum
        for (int i = 1; i <= N; i++) {
            count += nCr(N - i, X - 1, f) * i;
        }
 
        // Find the mean over all subsets
        double E_X = (double) (count) / (double) (total);
 
        System.out.print(String.format("%.6f", E_X));
 
        return 0;
    }
 
    // Driver Code
    public static void main(String[] args) {
        int N = 3, X = 2;
        findMean(N, X);
 
    }
}
 
// This code contributed by Princi Singh


Python3




# Python 3 program for the above approach
 
# Function to find the value of nCr
def nCr(n, r, f):
   
    # Base Case
    if (n < r):
        return 0
 
    # Find nCr recursively
    return f[n] / (f[r] * f[n - r])
 
# Function to find the expected minimum
# values of all the subsets of size K
def findMean(N, X):
   
    # Find the factorials that will
    # be used later
    f = [0 for i in range(N + 1)]
    f[0] = 1
 
    # Find the factorials
    for i in range(1,N+1,1):
        f[i] = f[i - 1] * i
 
    # Total number of subsets
    total = nCr(N, X, f)
 
    # Stores the sum of minimum over
    # all possible subsets
    count = 0
 
    # Iterate over all possible minimum
    for i in range(1,N+1,1):
        count += nCr(N - i, X - 1, f) * i
 
    # Find the mean over all subsets
    E_X = (count) / (total)
 
    print("{0:.9f}".format(E_X))
 
    return 0
 
# Driver Code
if __name__ == '__main__':
    N = 3
    X = 2
    findMean(N, X)
     
# This code is contributed by ipg201607.


C#




// C# code for the above approach
using System;
 
public class GFG
{
   
// Function to find the value of nCr
    static int nCr(int n, int r, int[] f)
    {
       
        // Base Case
        if (n < r) {
            return 0;
        }
 
        // Find nCr recursively
        return f[n] / (f[r] * f[n - r]);
    }
 
    // Function to find the expected minimum
    // values of all the subsets of size K
    static int findMean(int N, int X)
    {
       
        // Find the factorials that will
        // be used later
        int[] f = new int[N + 1];
        f[0] = 1;
 
        // Find the factorials
        for (int i = 1; i <= N; i++) {
            f[i] = f[i - 1] * i;
        }
 
        // Total number of subsets
        int total = nCr(N, X, f);
 
        // Stores the sum of minimum over
        // all possible subsets
        int count = 0;
 
        // Iterate over all possible minimum
        for (int i = 1; i <= N; i++) {
            count += nCr(N - i, X - 1, f) * i;
        }
 
        // Find the mean over all subsets
        double E_X = (double) (count) / (double) (total);
 
        Console.Write("{0:F9}", E_X);
 
        return 0;
    }
 
    // Driver Code
    static public void Main (){
 
        // Code
       int N = 3, X = 2;
        findMean(N, X);
    }
}
 
// This code is contributed by Potta Lokesh


Javascript




<script>
// Javascript program for the above approach
 
// Function to find the value of nCr
function nCr(n, r, f) {
  // Base Case
  if (n < r) {
    return 0;
  }
 
  // Find nCr recursively
  return f[n] / (f[r] * f[n - r]);
}
 
// Function to find the expected minimum
// values of all the subsets of size K
function findMean(N, X) {
  // Find the factorials that will
  // be used later
  let f = new Array(N + 1).fill(0);
  f[0] = 1;
 
  // Find the factorials
  for (let i = 1; i <= N; i++) {
    f[i] = f[i - 1] * i;
  }
 
  // Total number of subsets
  let total = nCr(N, X, f);
 
  // Stores the sum of minimum over
  // all possible subsets
  let count = 0;
 
  // Iterate over all possible minimum
  for (let i = 1; i <= N; i++) {
    count += nCr(N - i, X - 1, f) * i;
  }
 
  // Find the mean over all subsets
  let E_X = count / total;
 
  document.write(parseFloat(E_X).toFixed(9));
 
  return 0;
}
 
// Driver Code
 
let N = 3,
  X = 2;
findMean(N, X);
 
// This code is contributed by gfgking.
</script>


Output: 

1.333333333

 

Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads