📌  相关文章
📜  在给定的总和中以超递增的顺序找到子序列

📅  最后修改于: 2021-04-28 14:09:44             🧑  作者: Mango

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

例子:

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

  1. sum 在这种情况下,当前元素不能成为所需子序列的一部分,因为将其包括在内后,子序列的总和将超过给定的总和。因此,丢弃当前元素。
  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 requried 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 requried 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 requried 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 requried 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


输出:
25 46 201

时间复杂度: O(n)