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. k * nCk
- The count of total element in all K length subsequences is k * nCk , possibility of appearing of each element is same.
- So each element appears((k * nCk) / n ) times and it contributes arr[i] * ( (k*nCk)/n ) in the result.
- Hence, the sum of all K length subsequence is sum(array) * ( (k * nCk) / n )
Below is the implementation of the above mentioned approach:
C++
// C++ implementation to find sum
// of all subsequences of length K
#include <bits/stdc++.h>
using namespace std;
int fact(int n);
// Function to find nCr
int nCr(int n, int r)
{
return fact(n)
/ (fact(r)
* fact(n - r));
}
// Function that returns
// factorial of n
int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Function for finding sum
// of all K length subsequences
int sumSubsequences(
int arr[], int n, int k)
{
int sum = 0;
// Calculate the sum of array
for (int i = 0; i < n; i++) {
sum += arr[i];
}
int kLengthSubSequence;
// Calculate nCk
kLengthSubSequence = nCr(n, k);
int ans
= sum
* ((k * kLengthSubSequence)
/ n);
// Return the final result
return ans;
}
// Driver code
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
// Java implementation to find sum
// of all subsequences of length K
class GFG{
// Function to find nCr
static int nCr(int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
// Function that returns
// factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Function for finding sum
// of all K length subsequences
static int sumSubsequences(int arr[],
int n, int k)
{
int sum = 0;
// Calculate the sum of array
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
int kLengthSubSequence;
// Calculate nCk
kLengthSubSequence = nCr(n, k);
int ans = sum * ((k * kLengthSubSequence) / n);
// Return the final result
return ans;
}
// Driver code
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));
}
}
// This code contributed by Rajput-Ji
Python3
# Python3 implementation to find sum
# of all subsequences of length K
# Function to find nCr
def nCr(n, r):
return fact(n) / (fact(r) *
fact(n - r))
# Function that returns
# factorial of n
def fact(n):
res = 1
for i in range(2, n + 1):
res = res * i
return res
# Function for finding sum
# of all K length subsequences
def sumSubsequences(arr, n, k):
sum = 0
# Calculate the sum of array
for i in range(0, n):
sum = sum + arr[i]
# Calculate nCk
kLengthSubSequence = nCr(n, k)
ans = sum * ((k * kLengthSubSequence) / n);
# Return the final result
return ans
# Driver Code
arr = [ 7, 8, 9, 2 ]
k = 2
n = len(arr)
print(sumSubsequences(arr, n, k))
# This code is contributed by skylags
C#
// C# implementation to find sum
// of all subsequences of length K
using System;
class GFG{
// Function to find nCr
static int nCr(int n, int r)
{
return fact(n) / (fact(r) * fact(n - r));
}
// Function that returns
// factorial of n
static int fact(int n)
{
int res = 1;
for(int i = 2; i <= n; i++)
res = res * i;
return res;
}
// Function for finding sum
// of all K length subsequences
static int sumSubsequences(int[] arr,
int n, int k)
{
int sum = 0;
// Calculate the sum of array
for(int i = 0; i < n; i++)
{
sum += arr[i];
}
int kLengthSubSequence;
// Calculate nCk
kLengthSubSequence = nCr(n, k);
int ans = sum * ((k * kLengthSubSequence) / n);
// Return the final result
return ans;
}
// Driver code
static void Main()
{
int[] arr = { 7, 8, 9, 2 };
int K = 2;
int n = arr.Length;
Console.Write(sumSubsequences(arr, n, K));
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
<script>
// Javascript implementation to find sum
// of all subsequences of length K
// Function to find nCr
function nCr(n, r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
// Function that returns
// factorial of n
function fact(n)
{
var res = 1;
for(var i = 2; i <= n; i++)
res = res * i;
return res;
}
// Function for finding sum
// of all K length subsequences
function sumSubsequences(arr, n, k)
{
var sum = 0;
// Calculate the sum of array
for(var i = 0; i < n; i++)
{
sum += arr[i];
}
var kLengthSubSequence;
// Calculate nCk
kLengthSubSequence = nCr(n, k);
var ans = sum * ((k *
kLengthSubSequence) / n);
// Return the final result
return ans;
}
// Driver code
var arr = [ 7, 8, 9, 2 ];
var K = 2;
var n = arr.length;
document.write(sumSubsequences(arr, n, K));
// This code is contributed by noob2000
</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.
Similar Reads
Sum of all subsequences of a number Given a number as string s, find the sum of all the elements present in all possible subsequences of s.Examples : Input : s = "123" Output : 24 Explanation : all possible sub-sequences are 1, 2, 3, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3} which add up to 24 Input : s = "453" Output : 48 Recommended Practic
14 min read
Sum of all subsequences of an array Given an array of n integers. Find the sum of all possible subsequences of an array. Examples : Input : arr[] = { 1, 2 } Output : 6 All possible subsequences are {}, {1}, {2} and { 1, 2 } Input : arr[] = { 1, 2, 3 } Output : 24Recommended: Please solve it on âPRACTICE â first, before moving on to th
4 min read
Maximum Sum Subsequence of length k Given an array sequence [A1, A2 ...An], the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3.........<=Sk. Examples: Input : n = 8 k = 3 A=[8 5 9 10 5 6 21 8] Output : 40 Possible Increasing subsequence of Length 3 with maximum possible s
11 min read
Maximum Sum Subsequence of length k Given an array sequence [A1, A2 ...An], the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3.........<=Sk. Examples: Input : n = 8 k = 3 A=[8 5 9 10 5 6 21 8] Output : 40 Possible Increasing subsequence of Length 3 with maximum possible s
11 min read
Maximum Sum Subsequence of length k Given an array sequence [A1, A2 ...An], the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3.........<=Sk. Examples: Input : n = 8 k = 3 A=[8 5 9 10 5 6 21 8] Output : 40 Possible Increasing subsequence of Length 3 with maximum possible s
11 min read
Maximum sum subsequence of length K | Set 2 Given an array sequence arr[] i.e [A1, A2 â¦An] and an integer k, the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3â¦â¦â¦<=Sk. Examples: Input: arr[] = {-1, 3, 4, 2, 5}, K = 3Output: 3 4 5Explanation: Subsequence 3 4 5 with sum 12 is the s
7 min read