Find the subsequence with given sum in a superincreasing sequence - GeeksforGeeks

07 Mar, 2022
A sequence of positive real numbers S1, S2, S3, ..., SN is called a 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 = 272Input: 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].
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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to find the required subsequencevoid 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 required 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 codeint main(){    int arr[] = { 17, 25, 46, 94, 201, 400 };    int n = sizeof(arr) / sizeof(int);    int sum = 272;     findSubSeq(arr, n, sum);     return 0;}
// Java implementation of the approachclass 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 required 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
# Python3 implementation of the approach # Function to find the required subsequencedef 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 required 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 codeif __name__ == "__main__" :     arr = [ 17, 25, 46, 94, 201, 400 ];    n = len(arr);    sum = 272;     findSubSeq(arr, n, sum); # This code is contributed by kanugargng
// C# implementation of the approachusing 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 required 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
<script>// Javascript implementation of the approach  // Function to find the required subsequencefunction findSubSeq(arr, n, sum) {     for (let 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 required subsequence        // So update the sum        else            sum -= arr[i];    }     // Print the elements of the    // required subsequence    for (let i = 0; i < n; i++) {         // If the current element was        // included in the subsequence        if (arr[i] != -1)            document.write(arr[i] + " ");    }} // Driver code let arr = [17, 25, 46, 94, 201, 400];let n = arr.length;let sum = 272; findSubSeq(arr, n, sum); // This code is contributed by gfgking.</script>
25 46 201
 
Time Complexity: O(n) 
kanugargng
ankthon
princiraj1992
gfgking
anikaseth98
arorakashish0911
subsequence
Arrays
Greedy
Arrays
Greedy
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Building Heap from Array
Trapping Rain Water
Reversal algorithm for array rotation
Program to find sum of elements in a given array
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Huffman Coding | Greedy Algo-3
Write a program to print all permutations of a given string