给定一个由N个整数和两个数字K和S组成的数组arr [] ,任务是打印所有和为S的长度为K的子序列。
例子:
Input: N = 5, K = 3, S = 20, arr[] = {4, 6, 8, 2, 12}
Output:
{6, 2, 12}
Explanation:
Only one subsequence of size 3 with a sum 20 is possible i.e., {6, 2, 12} and sum is 6 + 2 + 12 = 20
Input: N = 10, K = 5, S = 25, arr[] = {2, 4, 6, 8, 10, 12, 1, 2, 5, 7}
Output:
{10, 1, 2, 5, 7}
{4, 8, 1, 5, 7}
{4, 8, 10, 1, 2}
{4, 6, 12, 1, 2}
{4, 6, 8, 2, 5}
{2, 10, 1, 5, 7}
{2, 8, 12, 1, 2}
{2, 6, 10, 2, 5}
{2, 6, 8, 2, 7}
{2, 4, 12, 2, 5}
{2, 4, 10, 2, 7}
{2, 4, 8, 10, 1}
{2, 4, 6, 12, 1}
{2, 4, 6, 8, 5}
方法:想法是使用Backtracking打印给定总和S的所有子序列。步骤如下:
- 迭代数组arr []的所有值,然后执行以下操作:
- 如果我们将当前元素包括在结果子序列中,则将K和当前元素的上述值减至总和S。
- 从元素的下一个索引递归地迭代到数组的末尾,以找到生成的子序列。
- 如果K为0且S为0,则得到长度K和S的结果子序列之一,打印此子序列并回溯到下一个结果子序列。
- 如果我们不包括当前元素,则通过排除当前元素并对数组中其余元素重复上述过程来找到结果子序列。
- 步骤3中的结果数组将给出给定总和S的长度K的所有可能子序列。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find all the subsequences
// of a given length and having sum S
void comb(int* arr, int len, int r,
int ipos, int* op, int opos,
int sum)
{
// Termination condition
if (opos == r) {
int sum2 = 0;
for (int i = 0; i < opos; i++) {
// Add value to sum
sum2 = sum2 + op[i];
}
// Check if the resultant sum
// equals to target sum
if (sum == sum2) {
// If true
for (int i = 0; i < opos; i++)
// Print resultant array
cout << op[i] << ", ";
cout << endl;
}
// End this recursion stack
return;
}
if (ipos < len) {
// Check all the combinations
// using backtracking
comb(arr, len, r, ipos + 1,
op, opos, sum);
op[opos] = arr[ipos];
// Check all the combinations
// using backtracking
comb(arr, len, r, ipos + 1,
op, opos + 1, sum);
}
}
// Driver Code
int main()
{
// Given array
int arr[] = { 4, 6, 8, 2, 12 };
int K = 3;
int S = 20;
int N = sizeof(arr) / sizeof(arr[0]);
// To store the subsquence
int op[N] = { 0 };
// Function Call
comb(arr, N, K, 0, op, 0, S);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find all the subsequences
// of a given length and having sum S
static void comb(int []arr, int len, int r,
int ipos, int[] op, int opos,
int sum)
{
// Termination condition
if (opos == r)
{
int sum2 = 0;
for(int i = 0; i < opos; i++)
{
// Add value to sum
sum2 = sum2 + op[i];
}
// Check if the resultant sum
// equals to target sum
if (sum == sum2)
{
// If true
for(int i = 0; i < opos; i++)
// Print resultant array
System.out.print(op[i] + ", ");
System.out.println();
}
// End this recursion stack
return;
}
if (ipos < len)
{
// Check all the combinations
// using backtracking
comb(arr, len, r, ipos + 1,
op, opos, sum);
op[opos] = arr[ipos];
// Check all the combinations
// using backtracking
comb(arr, len, r, ipos + 1,
op, opos + 1, sum);
}
}
// Driver Code
public static void main(String[] args)
{
// Given array
int arr[] = { 4, 6, 8, 2, 12 };
int K = 3;
int S = 20;
int N = arr.length;
// To store the subsquence
int op[] = new int[N];
// Function Call
comb(arr, N, K, 0, op, 0, S);
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 program for the above approach
# Function to find all the subsequences
# of a given length and having sum S
def comb(arr, Len, r, ipos, op, opos, Sum):
# Termination condition
if (opos == r):
sum2 = 0
for i in range(opos):
# Add value to sum
sum2 = sum2 + op[i]
# Check if the resultant sum
# equals to target sum
if (Sum == sum2):
# If true
for i in range(opos):
# Print resultant array
print(op[i], end = ", ")
print()
# End this recursion stack
return
if (ipos < Len):
# Check all the combinations
# using backtracking
comb(arr, Len, r, ipos + 1,
op, opos, Sum)
op[opos] = arr[ipos]
# Check all the combinations
# using backtracking
comb(arr, Len, r, ipos + 1, op,
opos + 1, Sum)
# Driver code
if __name__ == '__main__':
# Given array
arr = [ 4, 6, 8, 2, 12 ]
K = 3
S = 20
N = len(arr)
# To store the subsequence
op = [0] * N
# Function call
comb(arr, N, K, 0, op, 0, S)
# This code is contributed by himanshu77
C#
// C# program for the above approach
using System;
class GFG{
// Function to find all the subsequences
// of a given length and having sum S
static void comb(int []arr, int len, int r,
int ipos, int[] op, int opos,
int sum)
{
// Termination condition
if (opos == r)
{
int sum2 = 0;
for(int i = 0; i < opos; i++)
{
// Add value to sum
sum2 = sum2 + op[i];
}
// Check if the resultant sum
// equals to target sum
if (sum == sum2)
{
// If true
for(int i = 0; i < opos; i++)
// Print resultant array
Console.Write(op[i] + ", ");
Console.WriteLine();
}
// End this recursion stack
return;
}
if (ipos < len)
{
// Check all the combinations
// using backtracking
comb(arr, len, r, ipos + 1,
op, opos, sum);
op[opos] = arr[ipos];
// Check all the combinations
// using backtracking
comb(arr, len, r, ipos + 1,
op, opos + 1, sum);
}
}
// Driver Code
public static void Main(String[] args)
{
// Given array
int []arr = { 4, 6, 8, 2, 12 };
int K = 3;
int S = 20;
int N = arr.Length;
// To store the subsquence
int []op = new int[N];
// Function call
comb(arr, N, K, 0, op, 0, S);
}
}
// This code is contributed by amal kumar choubey
输出:
6, 2, 12,
时间复杂度: O(N * N!)