📌  相关文章
📜  如果arr [i]> 2 * arr [i-1],检查是否存在子序列的和等于k

📅  最后修改于: 2021-05-04 07:59:21             🧑  作者: Mango

给定arr [i]> 2 * arr [i-1]的正整数排序数组,请检查是否存在子序列的和等于k。

例子:

天真的解决方案:基本的解决方案是检查所有2 ^ n个可能的组合,并检查是否有任何子序列的总和等于K。此过程不适用于N较大的值,N> 20。
时间复杂度:O(2 ^ N)

有效的解决方案:我们给了arr [i]> 2 * arr [i-1],所以我们可以说arr [i]>(arr [i-1] + arr [i-2] +…+ arr [2] + arr [1] + arr [0])

让我们假设arr [i] <= K(arr [i-1] + arr [i-2] +…+ arr [2] + arr [1] + arr [0])),所以我们必须包括set中的arr [i]。因此,我们必须以降序遍历数组,并且当我们找到arr [i] <= k时,我们将把arr [i]包含在集合中,并从K中减去arr [i],然后继续循环直到K的值是等于零。

如果K的值为零,则没有子序列。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to check whether sum of any set
// of the array element is equal
// to k or not
bool CheckForSequence(int arr[], int n, int k)
{
    // Traverse the array from end
    // to start
    for (int i = n - 1; i >= 0; i--) {
        // if k is greater than
        // arr[i] then subtract
        // it from k
        if (k >= arr[i])
            k -= arr[i];
    }
  
    // If there is any subsequence
    // whose sum is equal to k
    if (k != 0)
        return false;
    else
        return true;
}
  
// Driver code
int main()
{
    int A[] = { 1, 3, 7, 15, 31 };
    int n = sizeof(A) / sizeof(int);
    cout << (CheckForSequence(A, n, 18)
                 ? "True": "False") << endl;
    return 0;
}


Java
// Java implementation of above approach
import java.io.*;
  
class GFG 
{
      
// Function to check whether 
// sum of any set of the array element 
// is equal to k or not
static boolean CheckForSequence(int arr[],
                                int n, int k)
{
    // Traverse the array from end
    // to start
    for (int i = n - 1; i >= 0; i--) 
    {
        // if k is greater than
        // arr[i] then subtract
        // it from k
        if (k >= arr[i])
            k -= arr[i];
    }
  
    // If there is any subsequence
    // whose sum is equal to k
    if (k != 0)
        return false;
    else
        return true;
}
  
// Driver code
public static void main (String[] args) 
{
  
    int A[] = { 1, 3, 7, 15, 31 };
    int n = A.length;
    System.out.println(CheckForSequence(A, n, 18) ? 
                                            "True": "False");
}
}
  
// This code is contributed by jit_t


Python3
# Python3 implementation of above approach 
  
# Function to check whether sum of any set 
# of the array element is equal 
# to k or not 
def CheckForSequence(arr, n, k) :
  
    # Traverse the array from end 
    # to start 
    for i in range(n - 1, -1, -1) :
        # if k is greater than 
        # arr[i] then subtract 
        # it from k 
        if (k >= arr[i]) :
            k -= arr[i]; 
  
    # If there is any subsequence 
    # whose sum is equal to k 
    if (k != 0) :
        return False; 
    else :
        return True; 
  
# Driver code 
if __name__ == "__main__" : 
  
    A = [ 1, 3, 7, 15, 31 ]; 
    n = len(A); 
      
    if (CheckForSequence(A, n, 18)) :
        print(True)
    else :
        print(False)
          
# This code is contributed by AnkitRai01


C#
// C# implementation of above approach
using System;
  
class GFG 
{
      
// Function to check whether 
// sum of any set of the array element 
// is equal to k or not
static bool CheckForSequence(int []arr,
                                int n, int k)
{
    // Traverse the array from end
    // to start
    for (int i = n - 1; i >= 0; i--) 
    {
        // if k is greater than
        // arr[i] then subtract
        // it from k
        if (k >= arr[i])
            k -= arr[i];
    }
  
    // If there is any subsequence
    // whose sum is equal to k
    if (k != 0)
        return false;
    else
        return true;
}
  
// Driver code
public static void Main () 
{
  
    int []A = { 1, 3, 7, 15, 31 };
    int n = A.Length;
    Console.WriteLine(CheckForSequence(A, n, 18) ? 
                                            "True": "False");
}
}
  
// This code is contributed by anuj_67..


输出:
True

时间复杂度:O(N)