📜  长度为K且具有给定总和的唯一子序列

📅  最后修改于: 2021-05-20 08:50:50             🧑  作者: Mango

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

方法:想法是使用Backtracking打印给定总和S的所有子序列。步骤如下:

  • 迭代数组arr []的所有值,然后执行以下操作:
    1. 如果我们将当前元素包括在结果子序列中,则将K和当前元素的上述值减至总和S。
    2. 从元素的下一个索引递归地迭代到数组的末尾,以找到生成的子序列。
    3. 如果K为0S为0,则得到长度KS的结果子序列之一,打印此子序列并回溯到下一个结果子序列。
    4. 如果我们不包括当前元素,则通过排除当前元素并对数组中其余元素重复上述过程来找到结果子序列。
  • 步骤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


Javascript


输出:

6, 2, 12, 

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。