Check whether a subsequence exists with sum equal to k if arr[i]> 2*arr[i-1]
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++
#include <iostream>
using namespace std;
bool CheckForSequence( int arr[], int n, int k)
{
for ( int i = n - 1; i >= 0; i--) {
if (k >= arr[i])
k -= arr[i];
}
if (k != 0)
return false ;
else
return true ;
}
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
import java.io.*;
class GFG
{
static boolean CheckForSequence( int arr[],
int n, int k)
{
for ( int i = n - 1 ; i >= 0 ; i--)
{
if (k >= arr[i])
k -= arr[i];
}
if (k != 0 )
return false ;
else
return true ;
}
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" );
}
}
|
Python3
def CheckForSequence(arr, n, k) :
for i in range (n - 1 , - 1 , - 1 ) :
if (k > = arr[i]) :
k - = arr[i];
if (k ! = 0 ) :
return False ;
else :
return True ;
if __name__ = = "__main__" :
A = [ 1 , 3 , 7 , 15 , 31 ];
n = len (A);
if (CheckForSequence(A, n, 18 )) :
print ( True )
else :
print ( False )
|
C#
using System;
class GFG
{
static bool CheckForSequence( int []arr,
int n, int k)
{
for ( int i = n - 1; i >= 0; i--)
{
if (k >= arr[i])
k -= arr[i];
}
if (k != 0)
return false ;
else
return true ;
}
public static void Main ()
{
int []A = { 1, 3, 7, 15, 31 };
int n = A.Length;
Console.WriteLine(CheckForSequence(A, n, 18) ?
"True" : "False" );
}
}
|
Javascript
<script>
function CheckForSequence( arr,n,k)
{
for ( var i = n - 1; i >= 0; i--) {
if (k >= arr[i])
k -= arr[i];
}
if (k != 0)
return false ;
else
return true ;
}
var A = [ 1, 3, 7, 15, 31 ];
var n = A.length;
document.write( (CheckForSequence(A, n, 18) ? "True" : "False" ) + "<br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...