# Length of Smallest Subsequence such that sum of elements is greater than equal to K

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 untill the priority queue is not empty
- Add the element in into the
**sum** - Increase the count because the element is picked to contribute into the total sum
- Check if the
**sum**is greater than the given number**K**, If yes then stop checking and output the count.

- Add the element in into the

Below is the implementation of above approach.

## C++

`// C++ implementation to find length of smallest ` `// subsequence such that sum of elements ` `// is greater than equal to given number K ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the smallest ` `// subsequence such that sum of elements ` `// is greater than equal to given number K ` `int` `lengthOfSmallestSubsequence(` `int` `K, vector<` `int` `> v) ` `{ ` ` ` `// Initialize priority queue ` ` ` `priority_queue<` `int` `> pq; ` ` ` ` ` `// Loop to insert all elements into ` ` ` `// the priority queue ` ` ` `for` `(` `int` `i = 0; i < v.size(); i++) { ` ` ` `pq.push(v[i]); ` ` ` `} ` ` ` `int` `sum = 0, count = 0; ` ` ` ` ` `// Loop to find the smallest ` ` ` `// subsequence such that sum of elements ` ` ` `// is greater than equal to given number K ` ` ` `while` `(!pq.empty() && sum < K) { ` ` ` `sum += pq.top(); ` ` ` `pq.pop(); ` ` ` `count++; ` ` ` `} ` ` ` `// If sum is less then K ` ` ` `// then return -1 else return count. ` ` ` `if` `(sum < K) { ` ` ` `return` `-1; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `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; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find length of smallest ` `// subsequence such that sum of elements ` `// is greater than equal to given number K ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the smallest ` `// subsequence such that sum of elements ` `// is greater than equal to given number K ` `static` `int` `lengthOfSmallestSubsequence(` `int` `K, ` `int` `[]v) ` `{ ` ` ` `// Initialize priority queue ` ` ` `Queue<Integer> pq = ` ` ` `new` `PriorityQueue<Integer>(Collections.reverseOrder()); ` ` ` ` ` `// Loop to insert all elements into ` ` ` `// the priority queue ` ` ` `for` `(` `int` `i = ` `0` `; i < v.length; i++) ` ` ` `{ ` ` ` `pq.add(v[i]); ` ` ` `} ` ` ` `int` `sum = ` `0` `, count = ` `0` `; ` ` ` ` ` `// Loop to find the smallest ` ` ` `// subsequence such that sum of elements ` ` ` `// is greater than equal to given number K ` ` ` `while` `(!pq.isEmpty() && sum < K) ` ` ` `{ ` ` ` `sum += pq.peek(); ` ` ` `pq.remove(); ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// If sum is less then K ` ` ` `// then return -1 else return count. ` ` ` `if` `(sum < K) ` ` ` `{ ` ` ` `return` `-` `1` `; ` ` ` `} ` ` ` `return` `count; ` `} ` ` ` `// Driver code ` `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)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find length of smallest ` `# subsequence such that sum of elements ` `# is greater than equal to given number K ` ` ` `# Function to find the smallest ` `# subsequence such that sum of elements ` `# is greater than equal to given number K ` `def` `lengthOfSmallestSubsequence(K, v): ` ` ` ` ` `# Initialize priority queue ` ` ` `pq ` `=` `[] ` ` ` ` ` `# Loop to insert all elements into ` ` ` `# the priority queue ` ` ` `for` `i ` `in` `v: ` ` ` `pq.append(i) ` ` ` `pq.sort() ` ` ` ` ` `sum` `=` `0` ` ` `count ` `=` `0` ` ` ` ` `# Loop to find the smallest ` ` ` `# subsequence such that sum of elements ` ` ` `# is greater than equal to given number K ` ` ` `while` `(` `len` `(pq) > ` `0` `and` `sum` `< K): ` ` ` `sum` `+` `=` `pq[` `-` `1` `] ` ` ` `del` `pq[` `-` `1` `] ` ` ` `count ` `+` `=` `1` ` ` ` ` `# If sum is less then K ` ` ` `# then return -1 else return count. ` ` ` `if` `(` `sum` `< K): ` ` ` `return` `-` `1` ` ` `return` `count ` ` ` `# Driver code ` `v ` `=` `[` `2` `, ` `3` `, ` `1` `, ` `5` `, ` ` ` `6` `, ` `3` `, ` `7` `, ` `9` `, ` ` ` `14` `, ` `10` `, ` `2` `, ` `5` `] ` `K ` `=` `35` ` ` `print` `(lengthOfSmallestSubsequence(K, v)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

4

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest Palindromic Subsequence of Even Length in Range [L, R]
- Length of Smallest subarray in range 1 to N with sum greater than a given value
- Longest subsequence with first and last element greater than all other elements
- Number of non-decreasing sub-arrays of length greater than or equal to K
- Length of longest subarray with product greater than or equal to 0
- Smallest subset with sum greater than all other elements
- Smallest subarray such that all elements are greater than K
- Smallest greater elements in whole array
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Count elements such that there are exactly X elements with values greater than or equal to X
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Cost of creating smallest subsequence with sum of difference between adjacent elements maximum
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
- Adding elements of an array until every element becomes greater than or equal to k
- Sum of elements in an array with frequencies greater than or equal to that element
- Minimize sum of smallest elements from K subsequences of length L
- Noble integers in an array (count of greater elements is equal to value)
- Make sum of all subarrays of length K equal by only inserting elements

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.