Number of K length subsequences with minimum sum
Last Updated :
10 Mar, 2022
Given an array arr[] of size N and an integer K, the task is to find the number of K length subsequences of this array such that the sum of these subsequences is the minimum possible.
Examples:
Input: arr[] = {1, 2, 3, 4}, K = 2
Output: 1
Subsequences of length 2 are (1, 2), (1, 3), (1, 4),
(2, 3), (2, 4) and (3, 4).
The minimum sum is 3 and the only subsequence
with this sum is (1, 2).
Input: arr[] = {2, 1, 2, 2, 2, 1}, K = 3
Output: 4
Approach: The minimum possible sum of a subsequence of length K from the given array is the sum of the K smallest elements of the array. Let X be the maximum element among the K smallest elements of the array, and let the number of times it occurs among the K, the smallest elements of the array, be Y, and, its total occurrence, in the complete array, be cntX. Now, there are cntXCY ways to select this element, in the K smallest elements, which is the count of required subsequences.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binomialCoeff( int n, int k)
{
int C[n + 1][k + 1];
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
return C[n][k];
}
int cntSubSeq( int arr[], int n, int k)
{
sort(arr, arr + n);
int num = arr[k - 1];
int Y = 0;
for ( int i = k - 1; i >= 0; i--) {
if (arr[i] == num)
Y++;
}
int cntX = Y;
for ( int i = k; i < n; i++) {
if (arr[i] == num)
cntX++;
}
return binomialCoeff(cntX, Y);
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int n = sizeof (arr) / sizeof ( int );
int k = 2;
cout << cntSubSeq(arr, n, k);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int binomialCoeff( int n, int k)
{
int C[][] = new int [n + 1 ][k + 1 ];
int i, j;
for (i = 0 ; i <= n; i++)
{
for (j = 0 ; j <= Math.min(i, k); j++)
{
if (j == 0 || j == i)
C[i][j] = 1 ;
else
C[i][j] = C[i - 1 ][j - 1 ] +
C[i - 1 ][j];
}
}
return C[n][k];
}
static int cntSubSeq( int arr[], int n, int k)
{
Arrays.sort(arr);
int num = arr[k - 1 ];
int Y = 0 ;
for ( int i = k - 1 ; i >= 0 ; i--)
{
if (arr[i] == num)
Y++;
}
int cntX = Y;
for ( int i = k; i < n; i++)
{
if (arr[i] == num)
cntX++;
}
return binomialCoeff(cntX, Y);
}
public static void main (String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int n = arr.length;
int k = 2 ;
System.out.println(cntSubSeq(arr, n, k));
}
}
|
C#
using System;
class GFG
{
static int binomialCoeff( int n, int k)
{
int [,]C = new int [n + 1, k + 1];
int i, j;
for (i = 0; i <= n; i++)
{
for (j = 0; j <= Math.Min(i, k); j++)
{
if (j == 0 || j == i)
C[i, j] = 1;
else
C[i, j] = C[i - 1, j - 1] +
C[i - 1, j];
}
}
return C[n, k];
}
static int cntSubSeq( int []arr, int n, int k)
{
Array.Sort(arr);
int num = arr[k - 1];
int Y = 0;
for ( int i = k - 1; i >= 0; i--)
{
if (arr[i] == num)
Y++;
}
int cntX = Y;
for ( int i = k; i < n; i++)
{
if (arr[i] == num)
cntX++;
}
return binomialCoeff(cntX, Y);
}
public static void Main (String[] args)
{
int []arr = { 1, 2, 3, 4 };
int n = arr.Length;
int k = 2;
Console.WriteLine(cntSubSeq(arr, n, k));
}
}
|
Python3
def binomialCoeff(n, k) :
C = [[ 0 for i in range (n + 1 )]
for j in range (k + 1 )]
for i in range ( 0 , n + 1 ):
for j in range ( 0 , min (i, k) + 1 ):
if (j = = 0 or j = = i):
C[i][j] = 1
else :
C[i][j] = C[i - 1 ][j - 1 ] + C[i - 1 ][j]
return C[n][k]
def cntSubSeq(arr, n, k) :
arr.sort()
num = arr[k - 1 ];
Y = 0 ;
for i in range (k - 1 , - 1 , 1 ) :
if (arr[i] = = num):
Y + = 1
cntX = Y;
for i in range (k, n):
if (arr[i] = = num) :
cntX + = 1
return binomialCoeff(cntX, Y)
arr = [ 1 , 2 , 3 , 4 ]
n = len (arr)
k = 2
print (cntSubSeq(arr, n, k))
|
Javascript
<script>
function binomialCoeff(n, k)
{
var C = new Array(n + 1);
for ( var i = 0; i < C.length; i++) {
C[i] = new Array(k + 1);
}
var i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= Math.min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
return C[n][k];
}
function cntSubSeq(arr, n, k)
{
arr.sort();
var num = arr[k - 1];
var Y = 0;
for ( var i = k - 1; i >= 0; i--) {
if (arr[i] == num)
Y+=1;
}
var cntX = Y;
for ( var i = k; i < n; i++) {
if (arr[i] == num)
cntX+=1;
}
return binomialCoeff(cntX, Y);
}
var arr = [ 1, 2, 3, 4 ];
var n = arr.length;
var k = 2;
document.write(cntSubSeq(arr, n, k));
</script>
|
Time Complexity: o(n2)
Auxiliary Space: O(n * k)
Share your thoughts in the comments
Please Login to comment...