Find the subsequence with given sum in a superincreasing sequence
Last Updated :
19 Oct, 2023
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 = 272
Input: S[] = {1, 2, 4, 8, 16}, sum = 12
Output: 4 8
Brute Force Approach :
- Generate all possible subsequences of the array
- For each subsequence, check if the sum of its elements is equal to the given sum
- Return the subsequence that satisfies the condition, or null if no such subsequence exists
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findSubSeq( int arr[], int n, int sum)
{
for ( int i = 0; i < (1 << n); i++) {
vector< int > subSeq;
for ( int j = 0; j < n; j++) {
if (i & (1 << j)) {
subSeq.push_back(
arr[j]);
}
}
int currentSum
= accumulate(subSeq.begin(), subSeq.end(),
0);
if (currentSum == sum) {
for ( int k = 0; k < subSeq.size(); k++) {
cout << subSeq[k] << " " ;
}
return ;
}
}
cout << "null" ;
}
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;
}
|
Java
import java.io.*;
import java.util.*;
public class Main {
public static void findSubSeq( int [] arr, int n, int sum)
{
for ( int i = 0 ; i < ( 1 << n); i++) {
List<Integer> subSeq = new ArrayList<>();
for ( int j = 0 ; j < n; j++) {
if ((i & ( 1 << j)) != 0 ) {
subSeq.add(
arr[j]);
}
}
int currentSum = 0 ;
for ( int k = 0 ; k < subSeq.size(); k++) {
currentSum += subSeq.get(k);
}
if (currentSum
== sum) {
for ( int k = 0 ; k < subSeq.size(); k++) {
System.out.print(subSeq.get(k) + " " );
}
return ;
}
}
System.out.println( "null" );
}
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);
}
}
|
Python3
def findSubSeq(arr, n, summ):
for i in range ( 1 << n):
subSeq = []
for j in range (n):
if (i & ( 1 << j)) ! = 0 :
subSeq.append(arr[j])
currentSum = sum (subSeq)
if currentSum = = summ:
print (subSeq)
return
print ( "null" )
arr = [ 17 , 25 , 46 , 94 , 201 , 400 ]
n = len (arr)
summ = 272
findSubSeq(arr, n, summ)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void FindSubsequence( int [] arr, int sum)
{
int n = arr.Length;
for ( int i = 0; i < (1 << n); i++)
{
List< int > subSeq = new List< int >();
for ( int j = 0; j < n; j++)
{
if ((i & (1 << j)) != 0)
{
subSeq.Add(arr[j]);
}
}
int currentSum = subSeq.Sum();
if (currentSum == sum)
{
Console.WriteLine( string .Join( " " , subSeq));
return ;
}
}
Console.WriteLine( "null" );
}
static void Main()
{
int [] arr = { 17, 25, 46, 94, 201, 400 };
int sum = 272;
FindSubsequence(arr, sum);
}
}
|
Javascript
function findSubSeq(arr, n, summ) {
for (let i = 0; i < (1 << n); i++) {
let subSeq = [];
for (let j = 0; j < n; j++) {
if ((i & (1 << j)) !== 0) {
subSeq.push(arr[j]);
}
}
let currentSum = subSeq.reduce((a, b) => a + b, 0);
if (currentSum === summ) {
console.log(subSeq);
return ;
}
}
console.log( "null" );
}
let arr = [17, 25, 46, 94, 201, 400];
let n = arr.length;
let summ = 272;
findSubSeq(arr, n, summ);
|
Time complexity : O(2^n * n)
Auxiliary Space : O(2^n * n)
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++
#include <bits/stdc++.h>
using namespace std;
void findSubSeq( int arr[], int n, int sum)
{
for ( int i = n - 1; i >= 0; i--) {
if (sum < arr[i])
arr[i] = -1;
else
sum -= arr[i];
}
for ( int i = 0; i < n; i++) {
if (arr[i] != -1)
cout << arr[i] << " " ;
}
}
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;
}
|
Java
class GFG
{
static void findSubSeq( int arr[], int n, int sum)
{
for ( int i = n - 1 ; i >= 0 ; i--)
{
if (sum < arr[i])
arr[i] = - 1 ;
else
sum -= arr[i];
}
for ( int i = 0 ; i < n; i++)
{
if (arr[i] != - 1 )
System.out.print(arr[i] + " " );
}
}
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);
}
}
|
Python3
def findSubSeq(arr, n, sum ) :
for i in range (n - 1 , - 1 , - 1 ) :
if ( sum < arr[i]) :
arr[i] = - 1 ;
else :
sum - = arr[i];
for i in range (n) :
if (arr[i] ! = - 1 ) :
print (arr[i], end = " " );
if __name__ = = "__main__" :
arr = [ 17 , 25 , 46 , 94 , 201 , 400 ];
n = len (arr);
sum = 272 ;
findSubSeq(arr, n, sum );
|
C#
using System;
class GFG
{
static void findSubSeq( int []arr,
int n, int sum)
{
for ( int i = n - 1; i >= 0; i--)
{
if (sum < arr[i])
arr[i] = -1;
else
sum -= arr[i];
}
for ( int i = 0; i < n; i++)
{
if (arr[i] != -1)
Console.Write(arr[i] + " " );
}
}
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);
}
}
|
Javascript
<script>
function findSubSeq(arr, n, sum) {
for (let i = n - 1; i >= 0; i--) {
if (sum < arr[i])
arr[i] = -1;
else
sum -= arr[i];
}
for (let i = 0; i < n; i++) {
if (arr[i] != -1)
document.write(arr[i] + " " );
}
}
let arr = [17, 25, 46, 94, 201, 400];
let n = arr.length;
let sum = 272;
findSubSeq(arr, n, sum);
</script>
|
Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Share your thoughts in the comments
Please Login to comment...