Given a sorted array of positive integers where **arr[i] > 2*arr[i-1]**, check whether a sub sequence exists whose sum is equal to k.

**Examples:**

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

**Naive Solution**: The basic solution is to check for all the 2^n possible combinations and check if there is any subsequence whose sum is equal to K. This process will not work for higher values of N, N>20.

Time Complexity: O(2^N)

**Efficient Solution**: We are given **arr[i] >2*arr[i-1] ** so we can say that **arr[i] > ( arr[i-1] + arr[i-2] + …+ arr[2] + arr[1] + arr[0] )**.

Let us assume that arr[i] <= K ( arr[i-1] + arr[i-2] + …+ arr[2] + arr[1] + arr[0] ) ), so we have to include arr[i] in the set . So, we have to traverse the array in descending order and when we find arr[i]<=k, we will include arr[i] in the set and subtract arr[i] from K and continue the loop until value of K is equal to zero.

If the value of K is zero then there is a subsequence else not.

Below is the Implementation of above approach:

## C++

`// C++ implementation of above approach ` `#include <iostream> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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.. ` |

*chevron_right*

*filter_none*

**Output:**

True

**Time Complexity **: O(N)

## Recommended Posts:

- Check whether (i,j) exists such that arr[i] != arr[j] and arr[arr[i]] is equal to arr[arr[j]]
- Check if a subsequence of length K with odd sum exists
- Check whether two strings can be made equal by reversing substring of equal length from both strings
- Find the equal pairs of subsequence of S and subsequence of T
- Check if a subarray of length K with sum equal to factorial of a number exists or not
- Check whether there exists a triplet (i, j, k) such that arr[i] < arr[k] < arr[j] for i < j < k
- Check whether it is possible to make both arrays equal by modifying a single element
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Check whether count of odd and even factors of a number are equal
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Check if a subarray exists with sum greater than the given Array
- Create an array of size N with sum S such that no subarray exists with sum S or S-K
- Find if there exists multiple ways to draw line through (x, y) to cut rectangle in equal halfs
- Check if there exist two elements in an array whose sum is equal to the sum of rest of the array
- Check whether a number can be represented by sum of two squares
- Check whether sum of digits at odd places of a number is divisible by K

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.