Count of ways to choose K elements from given Array with maximum sum
Last Updated :
04 Aug, 2021
Given an array, arr[] of size N and an integer K, the task is to find the number of ways of selecting K array elements, such that the sum of these K elements is the maximum possible sum.
Examples:
Input: arr[] = {3, 1, 1, 2}, K = 3
Output: 2
Explanation:
The possible ways of selecting 3 elements are:
- {arr[0] (=3), arr[1] (=1), arr[2] (=1)}, the sum of the subset is equal to (3+1+1 = 5).
- {arr[0] (=3), arr[1] (=1), arr[3] (=2)}, the sum of the subset is equal to (3+1+2 = 6).
- {arr[0] (=3), arr[2] (=1), arr[3] (=2)}, the sum of the subset is equal to (3+1+2 = 6).
- {arr[1] (=1), arr[2] (=1), arr[3] (=2)}, the sum of the subset is equal to (1+1+2 = 4).
Therefore, the total number of ways of selecting the K element with the maximum sum(= 6) is equal to 2.
Input: arr[] = { 2, 3, 4, 5, 2, 2 }, K = 4
Output: 3
Input: arr[]= {5, 4, 3, 3, 3, 3, 3, 1, 1}, K = 4
Output: 10
Approach: The problem can be solved by sorting the array in descending order. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int n)
{
int res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
int C( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
int findWays( int arr[], int N, int K)
{
sort(arr, arr + N, greater< int >());
int p = 0;
int q = 0;
for ( int i = 0; i < K; i++) {
if (arr[i] == arr[K - 1]) {
p++;
}
}
for ( int i = 0; i < N; i++) {
if (arr[i] == arr[K - 1]) {
q += 1;
}
}
int ans = C(q, p);
return ans;
}
int main()
{
int arr[] = { 2, 3, 4, 5, 2, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 4;
cout << findWays(arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int fact( int n)
{
int res = 1 ;
for ( int i = 2 ; i <= n; i++)
res = res * i;
return res;
}
static int C( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
static int findWays(Integer arr[], int N, int K)
{
Arrays.sort(arr, Collections.reverseOrder());
int p = 0 ;
int q = 0 ;
for ( int i = 0 ; i < K; i++)
{
if (arr[i] == arr[K - 1 ]) {
p++;
}
}
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == arr[K - 1 ]) {
q += 1 ;
}
}
int ans = C(q, p);
return ans;
}
public static void main(String[] args)
{
Integer arr[] = { 2 , 3 , 4 , 5 , 2 , 2 };
int N = arr.length;
int K = 4 ;
System.out.print(findWays(arr, N, K));
}
}
|
Python3
def fact(n):
res = 1
for i in range ( 2 , n + 1 ):
res = res * i
return res
def C(n, r):
return fact(n) / (fact(r) * fact(n - r))
def findWays(arr, N, K):
arr.sort(reverse = True )
p = 0
q = 0
for i in range (K):
if (arr[i] = = arr[K - 1 ]):
p + = 1
for i in range (N):
if (arr[i] = = arr[K - 1 ]):
q + = 1
ans = C(q, p)
return int (ans)
arr = [ 2 , 3 , 4 , 5 , 2 , 2 ]
N = len (arr)
K = 4
print (findWays(arr, N, K))
|
C#
using System;
class Program{
static int fact( int n)
{
int res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
static int C( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
static int findWays( int []arr, int N, int K)
{
Array.Sort(arr);
Array.Reverse(arr);
int p = 0;
int q = 0;
for ( int i = 0; i < K; i++)
{
if (arr[i] == arr[K - 1])
{
p++;
}
}
for ( int i = 0; i < N; i++)
{
if (arr[i] == arr[K - 1])
{
q += 1;
}
}
int ans = C(q, p);
return ans;
}
static void Main()
{
int []arr = { 2, 3, 4, 5, 2, 2 };
int N = arr.Length;
int K = 4;
Console.Write(findWays(arr, N, K));
}
}
|
Javascript
<script>
function fact(n)
{
let res = 1;
for (let i = 2; i <= n; i++)
res = res * i;
return res;
}
function C(n, r)
{
return fact(n) / (fact(r) * fact(n - r));
}
function findWays(arr, N, K)
{
arr.sort( function (a, b){ return b - a; });
let p = 0;
let q = 0;
for (let i = 0; i < K; i++)
{
if (arr[i] == arr[K - 1])
{
p++;
}
}
for (let i = 0; i < N; i++)
{
if (arr[i] == arr[K - 1])
{
q += 1;
}
}
let ans = C(q, p);
return ans;
}
let arr = [ 2, 3, 4, 5, 2, 2 ];
let N = arr.length;
let K = 4;
document.write(findWays(arr, N, K));
</script>
|
Time Complexity: O(N*log(N) + K)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...