Sum of all subsequences of length K
Last Updated :
17 Jan, 2023
Given an array arr[]and an integer K, the task is to find the sum of all K length subsequences from the given array.
Example:
Input: arr[] = {2, 3, 4}, K = 2
Output: 18
Explanation:
There are 3 possible subsequences of length 2 which are {2, 3}, {2, 4} and {3, 4}
The sum of all 2 length subsequences is 5 + 6 + 7 = 18
Input: arr[] = {7, 8, 9, 2}, K = 2
Output: 78
Explanation:
There are 6 subsequences of length 2 which are {7, 8}, {7, 9}, {7, 2}, {8, 9}, {8, 2} and {9, 2}.
The sum of all 2 length sub sequences is 15 + 16 + 9 + 17 + 10 + 11 = 78
Approach:
To solve the problem mentioned above we have to consider all K length subsequence that is “n choose k”, i.e.
- The count of total element in all K length subsequences is , possibility of appearing of each element is same.
- So each element appearstimes and it contributes in the result.
- Hence, the sum of all K length subsequence is
Below is the implementation of the above mentioned approach:
C++
#include <bits/stdc++.h>
using namespace std;
int fact( int n);
int nCr( int n, int r)
{
return fact(n)
/ (fact(r)
* fact(n - r));
}
int fact( int n)
{
int res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
int sumSubsequences(
int arr[], int n, int k)
{
int sum = 0;
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
int kLengthSubSequence;
kLengthSubSequence = nCr(n, k);
int ans
= sum
* ((k * kLengthSubSequence)
/ n);
return ans;
}
int main()
{
int arr[] = { 7, 8, 9, 2 };
int K = 2;
int n = sizeof (arr) / sizeof (arr[0]);
cout << sumSubsequences(arr, n, K);
return 0;
}
|
Java
class GFG{
static int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
static int fact( int n)
{
int res = 1 ;
for ( int i = 2 ; i <= n; i++)
res = res * i;
return res;
}
static int sumSubsequences( int arr[],
int n, int k)
{
int sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
sum += arr[i];
}
int kLengthSubSequence;
kLengthSubSequence = nCr(n, k);
int ans = sum * ((k * kLengthSubSequence) / n);
return ans;
}
public static void main(String[] args)
{
int arr[] = { 7 , 8 , 9 , 2 };
int K = 2 ;
int n = arr.length;
System.out.print(sumSubsequences(arr, n, K));
}
}
|
Python3
def nCr(n, r):
return fact(n) / (fact(r) *
fact(n - r))
def fact(n):
res = 1
for i in range ( 2 , n + 1 ):
res = res * i
return res
def sumSubsequences(arr, n, k):
sum = 0
for i in range ( 0 , n):
sum = sum + arr[i]
kLengthSubSequence = nCr(n, k)
ans = sum * ((k * kLengthSubSequence) / n);
return ans
arr = [ 7 , 8 , 9 , 2 ]
k = 2
n = len (arr)
print (sumSubsequences(arr, n, k))
|
C#
using System;
class GFG{
static int nCr( int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
static int fact( int n)
{
int res = 1;
for ( int i = 2; i <= n; i++)
res = res * i;
return res;
}
static int sumSubsequences( int [] arr,
int n, int k)
{
int sum = 0;
for ( int i = 0; i < n; i++)
{
sum += arr[i];
}
int kLengthSubSequence;
kLengthSubSequence = nCr(n, k);
int ans = sum * ((k * kLengthSubSequence) / n);
return ans;
}
static void Main()
{
int [] arr = { 7, 8, 9, 2 };
int K = 2;
int n = arr.Length;
Console.Write(sumSubsequences(arr, n, K));
}
}
|
Javascript
<script>
function nCr(n, r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
function fact(n)
{
var res = 1;
for ( var i = 2; i <= n; i++)
res = res * i;
return res;
}
function sumSubsequences(arr, n, k)
{
var sum = 0;
for ( var i = 0; i < n; i++)
{
sum += arr[i];
}
var kLengthSubSequence;
kLengthSubSequence = nCr(n, k);
var ans = sum * ((k *
kLengthSubSequence) / n);
return ans;
}
var arr = [ 7, 8, 9, 2 ];
var K = 2;
var n = arr.length;
document.write(sumSubsequences(arr, n, K));
</script>
|
Time complexity: O(n),the time complexity of this algorithm is O(n) where n is the length of the array. We use a single loop to iterate over the given array and calculate the sum.
Auxiliary Space complexity: O(1),the space complexity of this algorithm is O(1) as we are not using any extra space.
Share your thoughts in the comments
Please Login to comment...