给定一个排序的正整数数组,其中arr[i] > 2*arr[i-1] ,检查是否存在总和等于 k 的子序列。
例子:
Input : arr[]={ 1, 3, 7, 15, 31}, K=18
Output :True
A[1] + A[3] = 3 + 15 = 18
We found a subsequence whose sum is 18
Input :arr[]={ 1, 3, 7, 15, 31}, K=20
Output :False
No subsequence can be found with sum 20
朴素的解决方案:基本的解决方案是检查所有 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] ) ),所以我们必须包括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..
Javascript
True
时间复杂度:O(N)