Check whether a subsequence exists with sum equal to k if arr [ i ] > 2 * arr [ i | Function to check whether sum of any set of the array element is equal to k or not ; Traverse the array from end to start ; if k is greater than arr [ i ] then subtract it from k ; If there is any subsequence whose sum is equal to k ; Driver code
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 ) ? " " : " " ) + " " ) ;