Find a subsequence with sum in range [(K+1)/2, K]
Last Updated :
14 Jul, 2021
Given an array arr[] consisting of N positive integers and a positive integer K, The task is to find a subsequence of an array arr[] whose sum of the elements is in the range [(K+1)/2, K]. If there is a subsequence then print the indices of the subsequence, otherwise print -1.
Note: There can be multiple answers possible for this problem, print any one of them.
Examples:
Input: arr[ ] = {6, 2, 20, 3, 5, 6}, K = 13
Output: 0 1 3
Explanation:
Sum of elements of the subsequence {6, 2, 3} is 11 that is in the range[(K+1)/2, K].
Input: arr[ ] = {20, 24, 33, 100}, K = 2
Output: -1
Approach: This problem can be solved by traversing over the array arr[ ]. Follow the steps below to solve this problem:
- Initialize a vector ans to store indices of the resultant subsequence and totalSum to store the sum of elements of the subsequence.
- Iterate in the range [0, N-1] using the variable i and perform the following steps:
- If arr[i] > K, continue traversing the elements of the array.
- If arr[i] is in the range [(K+1)/2, K], then return the index of the current element as the answer and terminate the loop.
- If arr[i] +totalSum is less than K, then add the current element to totalSum and add index i in vector ans.
- If the totalSum is not in the given range then print -1, Otherwise print the vector ans.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void isSumOfSubSeqInRange( int arr[], int n, int k)
{
vector< int > ans;
int totalSum = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] > k) {
continue ;
}
if (arr[i] >= (k + 1) / 2) {
ans.clear();
ans.push_back(i);
totalSum = arr[i];
break ;
}
else if (arr[i] + totalSum <= k) {
totalSum += arr[i];
ans.push_back(i);
}
}
if (2 * totalSum < k) {
cout << -1 << endl;
return ;
}
for ( int x : ans) {
cout << x << " " ;
}
cout << endl;
}
int main()
{
int arr[] = { 6, 2, 20, 3, 5, 6 };
int N = 6;
int K = 13;
isSumOfSubSeqInRange(arr, N, K);
return 0;
}
|
Java
import java.util.Vector;
public class GFG {
static void isSumOfSubSeqInRange( int arr[], int n,
int k)
{
Vector<Integer> ans = new Vector<>();
int totalSum = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] > k) {
continue ;
}
if (arr[i] >= (k + 1 ) / 2 ) {
ans.clear();
ans.add(i);
totalSum = arr[i];
break ;
}
else if (arr[i] + totalSum <= k) {
totalSum += arr[i];
ans.add(i);
}
}
if ( 2 * totalSum < k) {
System.out.println(- 1 );
return ;
}
for ( int x : ans) {
System.out.print(x + " " );
}
System.out.println();
}
public static void main(String[] args)
{
int arr[] = { 6 , 2 , 20 , 3 , 5 , 6 };
int N = 6 ;
int K = 13 ;
isSumOfSubSeqInRange(arr, N, K);
}
}
|
Python3
def isSumOfSubSeqInRange(arr, n, k):
ans = []
totalSum = 0
for i in range (n):
if (arr[i] > k):
continue
if (arr[i] > = (k + 1 ) / 2 ):
ans.clear()
ans.append(i)
totalSum = arr[i]
break
elif (arr[i] + totalSum < = k):
totalSum + = arr[i]
ans.append(i)
if ( 2 * totalSum < k):
print ( - 1 )
return
for x in ans:
print (x, end = " " )
if __name__ = = '__main__' :
arr = [ 6 , 2 , 20 , 3 , 5 , 6 ]
N = 6
K = 13
isSumOfSubSeqInRange(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
static void isSumOfSubSeqInRange( int [] arr, int n,
int k)
{
List< int > ans = new List< int >();
int totalSum = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] > k) {
continue ;
}
if (arr[i] >= (k + 1) / 2) {
ans.Clear();
ans.Add(i);
totalSum = arr[i];
break ;
}
else if (arr[i] + totalSum <= k) {
totalSum += arr[i];
ans.Add(i);
}
}
if (2 * totalSum < k) {
Console.WriteLine(-1);
return ;
}
foreach ( int x in ans)
{
Console.Write(x + " " );
}
Console.WriteLine();
}
static public void Main ()
{
int [] arr = { 6, 2, 20, 3, 5, 6 };
int N = 6;
int K = 13;
isSumOfSubSeqInRange(arr, N, K);
}
}
|
Javascript
<script>
function isSumOfSubSeqInRange(arr, n, k) {
let ans = [];
let totalSum = 0;
for (let i = 0; i < n; i++) {
if (arr[i] > k) {
continue ;
}
if (arr[i] >= (k + 1) / 2) {
ans = [];
ans.push(i);
totalSum = arr[i];
break ;
}
else if (arr[i] + totalSum <= k) {
totalSum += arr[i];
ans.push(i);
}
}
if (2 * totalSum < k) {
document.write(-1 + "<br>" );
return ;
}
for (let x of ans) {
document.write(x + " " );
}
document.write( "<br>" );
}
let arr = [6, 2, 20, 3, 5, 6];
let N = 6;
let K = 13;
isSumOfSubSeqInRange(arr, N, K);
</script>
|
Time complexity: O(N)
Auxiliary space: O(N)
Share your thoughts in the comments
Please Login to comment...