📌  相关文章
📜  具有给定和的长度为 K 的唯一子序列

📅  最后修改于: 2021-09-07 05:13:49             🧑  作者: Mango

给定一个由N 个整数和两个数字KS 组成的数组arr[] ,任务是打印长度为K 的所有子序列,其总和为S
例子:

方法:这个想法是使用回溯打印给定总和S 的所有子序列。以下是步骤:

  • 迭代数组arr[] 的所有值并执行以下操作:
    1. 如果我们在结果子序列中包含当前元素,则将K和当前元素的上述值减少到总和S
    2. 从元素的下一个索引到数组末尾递归迭代以找到结果子序列。
    3. 如果K 是 0并且S 是 0,那么我们得到长度为K和总和S的结果子序列之一,打印这个子序列并回溯到下一个结果子序列。
    4. 如果我们不包括当前元素,则通过排除当前元素并对数组中的其余元素重复上述过程来找到结果子序列。
  • 步骤 3 中的结果数组将给出所有可能的长度为K 的子序列,其总和为S

下面是上述方法的实现:

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 subsequence
    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 subsequence
    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 subsequence
    int []op = new int[N];
 
    // Function call
    comb(arr, N, K, 0, op, 0, S);
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:

6, 2, 12, 

时间复杂度: O(N*N!)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live