Given an array arr[] of length n and a number k, the task is to find all the subsequences of the array with sum of its elements equal to k.
Note: A subsequence is a subset that can be derived from an array by removing zero or more elements, without changing the order of the remaining elements.
Examples:
Input: arr[] = [1, 2, 3], k = 3
Output: [ [1, 2], [3] ]
Explanation: All the subsequences of the given array are:
[ [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3], [] ]
Out of which only two subsequences have sum of their elements equal to 3.Input: arr[] = [1, 2, 3], k = 7
Output: []
Explanation: Sum of all the elements of the array is 6, which is smaller than the required sum, thus they are no subsequences with sum of its elements equal to 7.Input: arr[] = [17, 18, 6, 11, 2, 4], k = 6
Output: [ [2, 4], [6] ]

Approach:
The idea is to generate all possible subsequences of the given array arr[], and store the subsequences with sum of its elements equal to k.
- For every element in the array, there are two choices, either to include it in the subsequence or not include it.
- Apply this for every element in the array starting from index 0 until we reach the last index.
- Check if the sum of elements is equal to k, if so, store the subsequence in the result array.
#include <bits/stdc++.h>
using namespace std;
// Recursive function to generate all
// subsequences with sum of elements k
void findSubsequence(int ind, int sum, int k, vector<int> &cur,
vector<int> &arr, vector<vector<int>> &res) {
int n = arr.size();
// base case
if(ind == n) {
// check if sum of elements of current
// subset is equal to k
if(sum == k) {
// add the subset in result
res.push_back(cur);
}
return;
}
// skip the current element arr[ind]
findSubsequence(ind + 1, sum, k, cur, arr, res);
// add the current element in subset
cur.push_back(arr[ind]);
findSubsequence(ind + 1, sum + arr[ind], k, cur, arr, res);
// remove the added element
cur.pop_back();
}
// Function to find the subsequences
// with sum of its elements k
vector<vector<int>> subsequencesSumK(vector<int> &arr, int k) {
// to store the subsequences
// with sum of its elements k
vector<vector<int>> res;
// to store the current subset
vector<int> cur;
findSubsequence(0, 0, k, cur, arr, res);
return res;
}
int main() {
vector<int> arr = {17, 18, 6, 11, 2, 4};
int k = 6;
vector<vector<int>> ans = subsequencesSumK(arr, k);
for(auto i:ans) {
for(auto j:i) {
cout<<j<<" ";
}
cout<<endl;
}
return 0;
}
// Recursive function to generate all
// subsequences with sum of elements k
import java.util.*;
class GfG {
// Recursive function to generate all
// subsequences with sum of elements k
static void findSubsequence(
int ind, int sum, int k, ArrayList<Integer> cur,
ArrayList<Integer> arr, ArrayList<ArrayList<Integer>> res) {
int n = arr.size();
// base case
if(ind == n) {
// check if sum of elements of current
// subset is equal to k
if(sum == k) {
// add the subset in result
res.add(new ArrayList<>(cur));
}
return;
}
// skip the current element arr[ind]
findSubsequence(ind + 1, sum, k, cur, arr, res);
// add the current element in subset
cur.add(arr.get(ind));
findSubsequence(ind + 1, sum + arr.get(ind), k, cur, arr, res);
// remove the added element
cur.remove(cur.size() - 1);
}
// Function to find the subsequences
// with sum of its elements k
static ArrayList<ArrayList<Integer>> subsequencesSumK(
ArrayList<Integer> arr, int k) {
// to store the subsequences
// with sum of its elements k
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
// to store the current subset
ArrayList<Integer> cur = new ArrayList<>();
findSubsequence(0, 0, k, cur, arr, res);
return res;
}
public static void main(String[] args) {
ArrayList<Integer> arr =
new ArrayList<>(Arrays.asList(17, 18, 6, 11, 2, 4));
int k = 6;
ArrayList<ArrayList<Integer>> ans = subsequencesSumK(arr, k);
for(ArrayList<Integer> i : ans) {
for(Integer j : i) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
# Recursive function to generate all
# subsequences with sum of elements k
def findSubsequence(ind, sum, k, cur, arr, res):
n = len(arr)
# base case
if ind == n:
# check if sum of elements of current
# subset is equal to k
if sum == k:
# add the subset in result
res.append(cur.copy())
return
# skip the current element arr[ind]
findSubsequence(ind + 1, sum, k, cur, arr, res)
# add the current element in subset
cur.append(arr[ind])
findSubsequence(ind + 1, sum + arr[ind], k, cur, arr, res)
# remove the added element
cur.pop()
# Function to find the subsequences
# with sum of its elements k
def subsequencesSumK(arr, k):
# to store the subsequences
# with sum of its elements k
res = []
# to store the current subset
cur = []
findSubsequence(0, 0, k, cur, arr, res)
return res
if __name__ == "__main__":
arr = [17, 18, 6, 11, 2, 4]
k = 6
ans = subsequencesSumK(arr, k)
for i in ans:
for j in i:
print(j, end=" ")
print()
// Recursive function to generate all
// subsequences with sum of elements k
using System;
using System.Collections.Generic;
class GfG {
// Recursive function to generate all
// subsequences with sum of elements k
static void findSubsequence(int ind, int sum,
int k, List<int> cur, List<int> arr, List<List<int>> res) {
int n = arr.Count;
// base case
if (ind == n) {
// check if sum of elements of current
// subset is equal to k
if (sum == k) {
// add the subset in result
res.Add(new List<int>(cur));
}
return;
}
// skip the current element arr[ind]
findSubsequence(ind + 1, sum, k, cur, arr, res);
// add the current element in subset
cur.Add(arr[ind]);
findSubsequence(ind + 1, sum + arr[ind], k, cur, arr, res);
// remove the added element
cur.RemoveAt(cur.Count - 1);
}
// Function to find the subsequences
// with sum of its elements k
static List<List<int>> subsequencesSumK(List<int> arr, int k) {
// to store the subsequences
// with sum of its elements k
List<List<int>> res = new List<List<int>>();
// to store the current subset
List<int> cur = new List<int>();
findSubsequence(0, 0, k, cur, arr, res);
return res;
}
static void Main() {
List<int> arr = new List<int> { 17, 18, 6, 11, 2, 4 };
int k = 6;
List<List<int>> ans = subsequencesSumK(arr, k);
foreach (List<int> i in ans) {
foreach (int j in i) {
Console.Write(j + " ");
}
Console.WriteLine();
}
}
}
// Recursive function to generate all
// subsequences with sum of elements k
function findSubsequence(ind, sum, k, cur, arr, res) {
let n = arr.length;
// base case
if (ind === n) {
// check if sum of elements of current
// subset is equal to k
if (sum === k) {
// add the subset in result
res.push(cur.slice());
}
return;
}
// skip the current element arr[ind]
findSubsequence(ind + 1, sum, k, cur, arr, res);
// add the current element in subset
cur.push(arr[ind]);
findSubsequence(ind + 1, sum + arr[ind], k, cur, arr, res);
// remove the added element
cur.pop();
}
// Function to find the subsequences
// with sum of its elements k
function subsequencesSumK(arr, k) {
// to store the subsequences
// with sum of its elements k
let res = [];
// to store the current subset
let cur = [];
findSubsequence(0, 0, k, cur, arr, res);
return res;
}
let arr = [17, 18, 6, 11, 2, 4];
let k = 6;
let ans = subsequencesSumK(arr, k);
for (let i = 0; i < ans.length; i++) {
for (let j = 0; j < ans[i].length; j++) {
process.stdout.write(ans[i][j] + " ");
}
process.stdout.write("\n");
}
Output
2 4 6
Time Complexity: O(2 ^ n), as we are generating all possible subsequences.
Auxiliary Space: O(1)