A sequence of positive real numbers **S _{1}**,

**S**,

_{2}**S**, …,

_{3}**S**is called a

_{N}**superincreasing sequence**if every element of the sequence is greater than the sum of all the previous elements in the sequence. For example,

**1, 3, 6, 13, 27, 52**is such subsequence.

Now, given a superincreasing sequence

**S**and the

**sum**of a subsequence of this sequence, the task is to find the subsequence.

**Examples:**

Input:S[] = {17, 25, 46, 94, 201, 400}, sum = 272

Output:25 46 201

25 + 46 + 201 = 272

Input:S[] = {1, 2, 4, 8, 16}, sum = 12

Output:4 8

**Approach:** This problem can be solved using the greedy technique. Starting from the last element of the array till the first element, there are two cases:

**sum < arr[i]:**In this case, the current element cannot be a part of the required subsequence as after including it, the sum of the subsequence will exceed the given sum. So discard the current element.**sum ≥ arr[i]:**In this case, the current element has to be included in the required subsequence. This is because if the current element is not included then the sum of the previous elements in the array will be smaller than the current element (as it is a superincreasing sequence) which will in turn be smaller than the required sum. So take the current element and update the sum as**sum = sum – arr[i]**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the required subsequence ` `void` `findSubSeq(` `int` `arr[], ` `int` `n, ` `int` `sum) ` `{ ` ` ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) { ` ` ` ` ` `// Current element cannot be a part ` ` ` `// of the required subsequence ` ` ` `if` `(sum < arr[i]) ` ` ` `arr[i] = -1; ` ` ` ` ` `// Include current element in ` ` ` `// the requried subsequence ` ` ` `// So update the sum ` ` ` `else` ` ` `sum -= arr[i]; ` ` ` `} ` ` ` ` ` `// Print the elements of the ` ` ` `// required subsequence ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// If the current element was ` ` ` `// included in the subsequence ` ` ` `if` `(arr[i] != -1) ` ` ` `cout << arr[i] << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 17, 25, 46, 94, 201, 400 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(` `int` `); ` ` ` `int` `sum = 272; ` ` ` ` ` `findSubSeq(arr, n, sum); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the required subsequence ` ` ` `static` `void` `findSubSeq(` `int` `arr[], ` `int` `n, ` `int` `sum) ` ` ` `{ ` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) ` ` ` `{ ` ` ` ` ` `// Current element cannot be a part ` ` ` `// of the required subsequence ` ` ` `if` `(sum < arr[i]) ` ` ` `arr[i] = -` `1` `; ` ` ` ` ` `// Include current element in ` ` ` `// the requried subsequence ` ` ` `// So update the sum ` ` ` `else` ` ` `sum -= arr[i]; ` ` ` `} ` ` ` ` ` `// Print the elements of the ` ` ` `// required subsequence ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// If the current element was ` ` ` `// included in the subsequence ` ` ` `if` `(arr[i] != -` `1` `) ` ` ` `System.out.print(arr[i] + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `arr[] = { ` `17` `, ` `25` `, ` `46` `, ` `94` `, ` `201` `, ` `400` `}; ` ` ` `int` `n = arr.length; ` ` ` `int` `sum = ` `272` `; ` ` ` ` ` `findSubSeq(arr, n, sum); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to find the required subsequence ` `def` `findSubSeq(arr, n, ` `sum` `) : ` ` ` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `, ` `-` `1` `, ` `-` `1` `) : ` ` ` ` ` `# Current element cannot be a part ` ` ` `# of the required subsequence ` ` ` `if` `(` `sum` `< arr[i]) : ` ` ` `arr[i] ` `=` `-` `1` `; ` ` ` ` ` `# Include current element in ` ` ` `# the requried subsequence ` ` ` `# So update the sum ` ` ` `else` `: ` ` ` `sum` `-` `=` `arr[i]; ` ` ` ` ` `# Print the elements of the ` ` ` `# required subsequence ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` ` ` `# If the current element was ` ` ` `# included in the subsequence ` ` ` `if` `(arr[i] !` `=` `-` `1` `) : ` ` ` `print` `(arr[i], end ` `=` `" "` `); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `arr ` `=` `[ ` `17` `, ` `25` `, ` `46` `, ` `94` `, ` `201` `, ` `400` `]; ` ` ` `n ` `=` `len` `(arr); ` ` ` `sum` `=` `272` `; ` ` ` ` ` `findSubSeq(arr, n, ` `sum` `); ` ` ` `# This code is contributed by kanugargng ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to find the required subsequence ` ` ` `static` `void` `findSubSeq(` `int` `[]arr, ` ` ` `int` `n, ` `int` `sum) ` ` ` `{ ` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) ` ` ` `{ ` ` ` ` ` `// Current element cannot be a part ` ` ` `// of the required subsequence ` ` ` `if` `(sum < arr[i]) ` ` ` `arr[i] = -1; ` ` ` ` ` `// Include current element in ` ` ` `// the requried subsequence ` ` ` `// So update the sum ` ` ` `else` ` ` `sum -= arr[i]; ` ` ` `} ` ` ` ` ` `// Print the elements of the ` ` ` `// required subsequence ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// If the current element was ` ` ` `// included in the subsequence ` ` ` `if` `(arr[i] != -1) ` ` ` `Console.Write(arr[i] + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main (String[] args) ` ` ` `{ ` ` ` `int` `[]arr = { 17, 25, 46, 94, 201, 400 }; ` ` ` `int` `n = arr.Length; ` ` ` `int` `sum = 272; ` ` ` ` ` `findSubSeq(arr, n, sum); ` ` ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

25 46 201

**Time Complexity:** O(n)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find a valid parenthesis sequence of length K from a given valid parenthesis sequence
- Find the equal pairs of subsequence of S and subsequence of T
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Construction of Longest Increasing Subsequence(LIS) and printing LIS sequence
- Find the Maximum Alternate Subsequence Sum from a given array
- Find maximum Subsequence Sum according to given conditions
- Find sum in range L to R in given sequence of integers
- Longest subsequence whose sum is divisible by a given number
- Maximum subsequence sum from a given array which is a perfect square
- Maximize the Sum of a Subsequence from an Array based on given conditions
- Given two strings, find if first string is a subsequence of second
- Find number of times a string occurs as a subsequence in given string
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Find minimum length sub-array which has given sub-sequence in it
- Printing Maximum Sum Increasing Subsequence
- Subsequence with maximum odd sum
- Non-decreasing subsequence of size k with minimum sum
- Maximum even sum subsequence
- Maximum Sum Subsequence of length k
- Check whether a subsequence exists with sum equal to k if arr[i]> 2*arr[i-1]

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.