📜  在超增序列中找到给定和的子序列

📅  最后修改于: 2021-10-26 06:45:03             🧑  作者: Mango

一个正实数序列S 1 , S 2 , S 3 , …, S N被称为超增序列,如果该序列的每个元素都大于该序列中所有先前元素的总和。例如, 1、3、6、13、27、52就是这样的子序列。
现在,给定一个超增序列S与此序列的子序列的总和,任务是找到序列。
例子:

方法:这个问题可以使用贪心技术来解决。从数组的最后一个元素开始到第一个元素,有两种情况:

  1. sum < arr[i]:在这种情况下,当前元素不能成为所需子序列的一部分,因为在包含它之后,子序列的总和将超过给定的总和。所以丢弃当前元素。
  2. sum ≥ arr[i]:在这种情况下,当前元素必须包含在所需的子序列中。这是因为如果不包括当前元素,则数组中先前元素的总和将小于当前元素(因为它是一个超增序列),而当前元素又将小于所需的总和。因此,取当前元素并将总和更新为sum = sum – arr[i]

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to find the required subsequence
void findSubSeq(int arr[], int n, int sum)
{
 
    for (int i = n - 1; i >= 0; i--) {
 
        // Current element cannot be a part
        // of the required subsequence
        if (sum < arr[i])
            arr[i] = -1;
 
        // Include current element in
        // the required subsequence
        // So update the sum
        else
            sum -= arr[i];
    }
 
    // Print the elements of the
    // required subsequence
    for (int i = 0; i < n; i++) {
 
        // If the current element was
        // included in the subsequence
        if (arr[i] != -1)
            cout << arr[i] << " ";
    }
}
 
// Driver code
int main()
{
    int arr[] = { 17, 25, 46, 94, 201, 400 };
    int n = sizeof(arr) / sizeof(int);
    int sum = 272;
 
    findSubSeq(arr, n, sum);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
    // Function to find the required subsequence
    static void findSubSeq(int arr[], int n, int sum)
    {
        for (int i = n - 1; i >= 0; i--)
        {
     
            // Current element cannot be a part
            // of the required subsequence
            if (sum < arr[i])
                arr[i] = -1;
     
            // Include current element in
            // the required subsequence
            // So update the sum
            else
                sum -= arr[i];
        }
     
        // Print the elements of the
        // required subsequence
        for (int i = 0; i < n; i++)
        {
     
            // If the current element was
            // included in the subsequence
            if (arr[i] != -1)
                System.out.print(arr[i] + " ");
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { 17, 25, 46, 94, 201, 400 };
        int n = arr.length;
        int sum = 272;
     
        findSubSeq(arr, n, sum);
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
 
# Function to find the required subsequence
def findSubSeq(arr, n, sum) :
 
    for i in range(n - 1, -1, -1) :
 
        # Current element cannot be a part
        # of the required subsequence
        if (sum < arr[i]) :
            arr[i] = -1;
 
        # Include current element in
        # the required subsequence
        # So update the sum
        else :
            sum -= arr[i];
 
    # Print the elements of the
    # required subsequence
    for i in range(n) :
 
        # If the current element was
        # included in the subsequence
        if (arr[i] != -1) :
            print(arr[i], end = " ");
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 17, 25, 46, 94, 201, 400 ];
    n = len(arr);
    sum = 272;
 
    findSubSeq(arr, n, sum);
 
# This code is contributed by kanugargng


C#
// C# implementation of the approach
using System;
     
class GFG
{
     
    // Function to find the required subsequence
    static void findSubSeq(int []arr,
                           int n, int sum)
    {
        for (int i = n - 1; i >= 0; i--)
        {
     
            // Current element cannot be a part
            // of the required subsequence
            if (sum < arr[i])
                arr[i] = -1;
     
            // Include current element in
            // the required subsequence
            // So update the sum
            else
                sum -= arr[i];
        }
     
        // Print the elements of the
        // required subsequence
        for (int i = 0; i < n; i++)
        {
     
            // If the current element was
            // included in the subsequence
            if (arr[i] != -1)
                Console.Write(arr[i] + " ");
        }
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int []arr = { 17, 25, 46, 94, 201, 400 };
        int n = arr.Length;
        int sum = 272;
     
        findSubSeq(arr, n, sum);
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
25 46 201

时间复杂度: O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程