Geek and Subset Sum | Practice | GeeksforGeeks

Given an array arr of positive integers of size N and an integer K, the task is to find the sum of all subsets of size K.
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains two integers N and K.
3. The next line contains N space-separated integers.

Output: For each test case, print the answer

Constraints:
1. 1 <= T <= 10
2. 1 <=  K <= N <= 12
3. 1 <= arr[i] <= 100

Example:
Input:
2
4 2
1 2 3 5
1 1
20
Output:
33
20

Explanation:
Test Case 1: Subsets are {1, 2}, {1, 3}, {1, 5}, {2, 3}, {2, 5}, {3, 5}, Summation of all subsets sum = 3 + 4 + 6 + 5 + 7 + 8 = 33
0
anutiger5 months ago
void helper(int ind,int curk,int k,int *arr,int n ,int s,int &sum){
    if(curk == k){
        sum += s;
        return;
    }
    if(ind == n)
        return;
    helper(ind + 1,curk + 1,k,arr,n,s + arr[ind],sum);
    helper(ind + 1,curk,k,arr,n,s,sum);
}
int main()
 {
	int t;
	cin>> t;
	while(t--){
	    int n,k;
	    cin >> n >> k;
	    int arr[n];
	    for(int i =  0 ;i < n ; i ++){
	        cin >> arr[i];
	    }
	    int sum = 0;
	    helper(0,0,k,arr,n,0,sum);
	    cout<<sum<<endl;
	}
	return 0;
}
0
Akash8 months ago
Akash
import sys
def intReadLine():    return int(sys.stdin.readline())
def readLine():    return map(int, sys.stdin.readline().strip().split())
num_cases = intReadLine()
def bitCount(num):    count = 0    while num > 0:        count += (num&1)        num >>= 1    return count
for cas in range(num_cases):    N, K = readLine()    arr = list(readLine())    pre_sum = sum(arr)
    mult = 0
    for i in range(2**N):        bitcount = bitCount(i)        if bitcount == K and (i & 1 == 1):            mult += 1
    ans = int(mult*pre_sum)
    print(ans)
0
Shadab Anwar9 months ago
Shadab Anwar
0
Amit Singh Negi11 months ago
Amit Singh Negi
import java.util.*;import java.lang.*;import java.io.*;import java.util.stream.*;class GFG {  public static void main (String[] args) throws IOException {    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));            int t = Integer.parseInt(br.readLine());            while(t-- > 0){      String[] ip = br.readLine().split(" ");        int n = Integer.parseInt(ip[0]), k = Integer.parseInt(ip[1]), sum = 0;                    int[] nums = Stream.of(br.readLine().split(" ")).mapToInt(Integer::new).toArray();                    for(int i = 1; i < (1 << n); i++){//iterate all the possible 2 ^ n subsets                          if(Integer.bitCount(i) != k) continue;//skip other than k                            for(int j = 0, temp = i; j < n; j++, temp >>= 1)              if((temp & 1) == 1)//current bit is set add nums[i] to sum                sum += nums[j];        }        System.out.println(sum);    }  }}
0
pradeep kumar1 year ago
pradeep kumar
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.