Length of Smallest Subsequence such that sum of elements is greater than equal to K
Last Updated :
28 Jun, 2022
Given an array arr[] of size N and a number K, the task is to find the length of the smallest subsequence such that the sum of the subsequence is greater than or equal to number K.
Example:
Input: arr[] = {2, 3, 1, 5, 6, 3, 7, 9, 14, 10, 2, 5}, K = 35
Output: 4
Smallest subsequence with the sum greater than or equal to the given sum K is {7, 9, 14, 10}
Input: arr[] = {1, 2, 2, 2, 3, 4, 5, 4, 7, 6, 5, 12}, K = 70
Output:-1
Subsequence with sum greater than equal to the given sum is not possible.
Approach:
- This problem can be solved with the help of priority queue
- Traverse input array and insert every array element into priority queue.
- Initialize variables that holds the sum of picked element from priority queue and the variable to get the count of picked element from priority queue to 0
- Pop the elements out from the priority queue until the priority queue is not empty
- Add the element into the sum
- Increase the count because the element is picked to contribute to the total sum
- Check if the sum is greater than the given number K, If yes then stop checking and output the count.
Below is the implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int lengthOfSmallestSubsequence( int K, vector< int > v)
{
priority_queue< int > pq;
for ( int i = 0; i < v.size(); i++) {
pq.push(v[i]);
}
int sum = 0, count = 0;
while (!pq.empty() && sum < K) {
sum += pq.top();
pq.pop();
count++;
}
if (sum < K) {
return -1;
}
return count;
}
int main()
{
vector< int > v{ 2, 3, 1, 5,
6, 3, 7, 9,
14, 10, 2, 5 };
int K = 35;
cout << lengthOfSmallestSubsequence(K, v);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int lengthOfSmallestSubsequence( int K, int []v)
{
Queue<Integer> pq =
new PriorityQueue<Integer>(Collections.reverseOrder());
for ( int i = 0 ; i < v.length; i++)
{
pq.add(v[i]);
}
int sum = 0 , count = 0 ;
while (!pq.isEmpty() && sum < K)
{
sum += pq.peek();
pq.remove();
count++;
}
if (sum < K)
{
return - 1 ;
}
return count;
}
public static void main(String[] args)
{
int []v = { 2 , 3 , 1 , 5 ,
6 , 3 , 7 , 9 ,
14 , 10 , 2 , 5 };
int K = 35 ;
System.out.print(lengthOfSmallestSubsequence(K, v));
}
}
|
Python3
def lengthOfSmallestSubsequence(K, v):
pq = []
for i in v:
pq.append(i)
pq.sort()
sum = 0
count = 0
while ( len (pq) > 0 and sum < K):
sum + = pq[ - 1 ]
del pq[ - 1 ]
count + = 1
if ( sum < K):
return - 1
return count
v = [ 2 , 3 , 1 , 5 ,
6 , 3 , 7 , 9 ,
14 , 10 , 2 , 5 ]
K = 35
print (lengthOfSmallestSubsequence(K, v))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static int lengthOfSmallestSubsequence( int K, int []v)
{
List< int > pq = new List< int >();
for ( int i = 0; i < v.Length; i++)
{
pq.Add(v[i]);
}
pq.Sort();
pq.Reverse();
int sum = 0;
int count = 0;
while (pq.Count > 0 && sum < K)
{
sum += pq[0];
pq.RemoveAt(0);
count++;
}
if (sum < K)
{
return -1;
}
return count;
}
static public void Main ()
{
int []v = { 2, 3, 1, 5,6, 3, 7, 9, 14, 10, 2, 5 };
int K = 35;
Console.WriteLine(lengthOfSmallestSubsequence(K, v));
}
}
|
Javascript
<script>
function lengthOfSmallestSubsequence(K, v) {
let pq = new Array();
for (let i = 0; i < v.length; i++) {
pq.push(v[i]);
}
pq.sort((a, b) => b - a);
let sum = 0;
let count = 0;
while (pq.length > 0 && sum < K) {
sum += pq[0];
pq.splice(0, 1);
count++;
}
if (sum < K) {
return -1;
}
return count;
}
let v = [2, 3, 1, 5, 6, 3, 7, 9, 14, 10, 2, 5];
let K = 35;
document.write(lengthOfSmallestSubsequence(K, v));
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...