Given an array arr[] of N integers, the task is to find the length of the longest sub-sequence such that adjacent elements of the sub-sequence have at least one digit in common.
Examples:
Input: arr[] = {1, 12, 44, 29, 33, 96, 89}
Output: 5
The longest sub-sequence is {1 12 29 96 89}
Input: arr[] = {12, 23, 45, 43, 36, 97}
Output: 4
The longest sub-sequence is {12 23 43 36}
Approach: The idea is to store the length of longest sub-sequence for each digit present in an element of the array.
- dp[i][d] represents the length of the longest sub-sequence up to ith element if digit d is the common digit.
- Declare a cnt array and set cnt[d] = 1 for each digit present in current element.
- Consider each digit d as common digit and find maximum length sub-sequence ending at arr[i] as dp[i][d] = max(dp[j][d]+1) (0<=j<i).
- Also find a local maximum max(dp[i][d]) for current element.
- After finding local maximum update dp[i][d] for all digits present in the current element to a local maximum.
- This is required as local maximum represents maximum length sub-sequence for an element having digit d.
E.g. Consider arr[] = {24, 49, 96}.
The local maximum for 49 is 2 obtain from digit 4.
This local maximum will be used in finding the local maximum for 96 with common digit 9.
For that it is required for all digits in 49, dp[i][d] should be set to local maximum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSubsequence( int arr[], int n)
{
int len = 1;
int tmp;
int i, j, d;
int dp[n][10];
memset (dp, 0, sizeof (dp));
int cnt[10];
int locMax;
tmp = arr[0];
while (tmp > 0) {
dp[0][tmp % 10] = 1;
tmp /= 10;
}
for (i = 1; i < n; i++) {
tmp = arr[i];
locMax = 1;
memset (cnt, 0, sizeof (cnt));
while (tmp > 0) {
cnt[tmp % 10] = 1;
tmp /= 10;
}
for (d = 0; d <= 9; d++) {
if (cnt[d]) {
dp[i][d] = 1;
for (j = 0; j < i; j++) {
dp[i][d] = max(dp[i][d], dp[j][d] + 1);
locMax = max(dp[i][d], locMax);
}
}
}
for (d = 0; d <= 9; d++) {
if (cnt[d]) {
dp[i][d] = locMax;
}
}
len = max(len, locMax);
}
return len;
}
int main()
{
int arr[] = { 1, 12, 44, 29, 33, 96, 89 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findSubsequence(arr, n);
return 0;
}
|
Java
class GFG
{
static int findSubsequence( int arr[], int n)
{
int len = 1 ;
int tmp;
int i, j, d;
int [][] dp = new int [n][ 10 ];
int [] cnt = new int [ 10 ];
int locMax;
tmp = arr[ 0 ];
while (tmp > 0 )
{
dp[ 0 ][tmp % 10 ] = 1 ;
tmp /= 10 ;
}
for (i = 1 ; i < n; i++)
{
tmp = arr[i];
locMax = 1 ;
for ( int x = 0 ; x < 10 ; x++)
cnt[x]= 0 ;
while (tmp > 0 )
{
cnt[tmp % 10 ] = 1 ;
tmp /= 10 ;
}
for (d = 0 ; d <= 9 ; d++)
{
if (cnt[d] > 0 )
{
dp[i][d] = 1 ;
for (j = 0 ; j < i; j++)
{
dp[i][d] = Math.max(dp[i][d], dp[j][d] + 1 );
locMax = Math.max(dp[i][d], locMax);
}
}
}
for (d = 0 ; d <= 9 ; d++)
{
if (cnt[d] > 0 )
{
dp[i][d] = locMax;
}
}
len = Math.max(len, locMax);
}
return len;
}
public static void main (String[] args)
{
int arr[] = { 1 , 12 , 44 , 29 , 33 , 96 , 89 };
int n = arr.length;
System.out.println(findSubsequence(arr, n));
}
}
|
Python3
def findSubsequence(arr, n):
Len = 1
tmp = 0
i, j, d = 0 , 0 , 0
dp = [[ 0 for i in range ( 10 )]
for i in range (n)]
cnt = [ 0 for i in range ( 10 )]
locMax = 0
tmp = arr[ 0 ]
while (tmp > 0 ):
dp[ 0 ][tmp % 10 ] = 1
tmp / / = 10
for i in range ( 1 , n):
tmp = arr[i]
locMax = 1
cnt = [ 0 for i in range ( 10 )]
while (tmp > 0 ):
cnt[tmp % 10 ] = 1
tmp / / = 10
for d in range ( 10 ):
if (cnt[d]):
dp[i][d] = 1
for j in range (i):
dp[i][d] = max (dp[i][d],
dp[j][d] + 1 )
locMax = max (dp[i][d], locMax)
for d in range ( 10 ):
if (cnt[d]):
dp[i][d] = locMax
Len = max ( Len , locMax)
return Len
arr = [ 1 , 12 , 44 , 29 , 33 , 96 , 89 ]
n = len (arr)
print (findSubsequence(arr, n))
|
C#
using System;
class GFG
{
static int findSubsequence( int []arr, int n)
{
int len = 1;
int tmp;
int i, j, d;
int [,] dp = new int [n, 10];
int [] cnt = new int [10];
int locMax;
tmp = arr[0];
while (tmp > 0)
{
dp[0, tmp % 10] = 1;
tmp /= 10;
}
for (i = 1; i < n; i++)
{
tmp = arr[i];
locMax = 1;
for ( int x = 0; x < 10; x++)
cnt[x] = 0;
while (tmp > 0)
{
cnt[tmp % 10] = 1;
tmp /= 10;
}
for (d = 0; d <= 9; d++)
{
if (cnt[d] > 0)
{
dp[i, d] = 1;
for (j = 0; j < i; j++)
{
dp[i, d] = Math.Max(dp[i, d], dp[j, d] + 1);
locMax = Math.Max(dp[i, d], locMax);
}
}
}
for (d = 0; d <= 9; d++)
{
if (cnt[d] > 0)
{
dp[i, d] = locMax;
}
}
len = Math.Max(len, locMax);
}
return len;
}
public static void Main()
{
int []arr = { 1, 12, 44, 29, 33, 96, 89 };
int n = arr.Length;
Console.WriteLine(findSubsequence(arr, n));
}
}
|
Javascript
<script>
function findSubsequence(arr,n)
{
let len = 1;
let tmp;
let i, j, d;
let dp = new Array(n);
for (let i = 0; i < n; i++)
{
dp[i] = new Array(10);
for (let j = 0; j < 10; j++)
{
dp[i][j] = 0;
}
}
let cnt = new Array(10);
let locMax;
tmp = arr[0];
while (tmp > 0)
{
dp[0][tmp % 10] = 1;
tmp = Math.floor(tmp/10);
}
for (i = 1; i < n; i++)
{
tmp = arr[i];
locMax = 1;
for (let x = 0; x < 10; x++)
cnt[x]=0;
while (tmp > 0)
{
cnt[tmp % 10] = 1;
tmp = Math.floor(tmp/10);
}
for (d = 0; d <= 9; d++)
{
if (cnt[d] > 0)
{
dp[i][d] = 1;
for (j = 0; j < i; j++)
{
dp[i][d] = Math.max(dp[i][d], dp[j][d] + 1);
locMax = Math.max(dp[i][d], locMax);
}
}
}
for (d = 0; d <= 9; d++)
{
if (cnt[d] > 0)
{
dp[i][d] = locMax;
}
}
len = Math.max(len, locMax);
}
return len;
}
let arr=[ 1, 12, 44, 29, 33, 96, 89];
let n = arr.length;
document.write(findSubsequence(arr, n));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(n)
The auxiliary space required for above solution can be further reduced. Observe that for each digit d present in arr[i], it is required to find maximum length sub-sequence upto that digit irrespective of the fact that at which element the sub-sequence end. This reduces auxiliary space required to O(1). For each arr[i], find local maximum and update dig[d] for each digit d in arr[i] to local maximum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSubsequence( int arr[], int n)
{
int len = 1;
int tmp;
int i, j, d;
int dp[10];
memset (dp, 0, sizeof (dp));
int cnt[10];
int locMax;
tmp = arr[0];
while (tmp > 0) {
dp[tmp % 10] = 1;
tmp /= 10;
}
for (i = 1; i < n; i++) {
tmp = arr[i];
locMax = 1;
memset (cnt, 0, sizeof (cnt));
while (tmp > 0) {
cnt[tmp % 10] = 1;
tmp /= 10;
}
for (d = 0; d <= 9; d++) {
if (cnt[d]) {
dp[d]++;
locMax = max(locMax, dp[d]);
}
}
for (d = 0; d <= 9; d++) {
if (cnt[d]) {
dp[d] = locMax;
}
}
len = max(len, locMax);
}
return len;
}
int main()
{
int arr[] = { 1, 12, 44, 29, 33, 96, 89 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << findSubsequence(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int findSubsequence( int arr[], int n)
{
int len = 1 ;
int tmp;
int i, j, d;
int dp[] = new int [ 10 ];
int cnt[] = new int [ 10 ];
int locMax;
tmp = arr[ 0 ];
while (tmp > 0 )
{
dp[tmp % 10 ] = 1 ;
tmp /= 10 ;
}
for (i = 1 ; i < n; i++)
{
tmp = arr[i];
locMax = 1 ;
Arrays.fill(cnt, 0 );
while (tmp > 0 )
{
cnt[tmp % 10 ] = 1 ;
tmp /= 10 ;
}
for (d = 0 ; d <= 9 ; d++)
{
if (cnt[d] == 1 )
{
dp[d]++;
locMax = Math.max(locMax, dp[d]);
}
}
for (d = 0 ; d <= 9 ; d++)
{
if (cnt[d] == 1 )
{
dp[d] = locMax;
}
}
len = Math.max(len, locMax);
}
return len;
}
public static void main(String[] args)
{
int arr[] = { 1 , 12 , 44 , 29 , 33 , 96 , 89 };
int n = arr.length;
System.out.print(findSubsequence(arr, n));
}
}
|
Python3
def findSubsequence(arr, n) :
length = 1 ;
dp = [ 0 ] * 10 ;
tmp = arr[ 0 ];
while (tmp > 0 ) :
dp[tmp % 10 ] = 1 ;
tmp / / = 10 ;
for i in range ( 1 , n) :
tmp = arr[i];
locMax = 1 ;
cnt = [ 0 ] * 10
while (tmp > 0 ) :
cnt[tmp % 10 ] = 1 ;
tmp / / = 10 ;
for d in range ( 10 ) :
if (cnt[d]) :
dp[d] + = 1 ;
locMax = max (locMax, dp[d]);
for d in range ( 10 ) :
if (cnt[d]) :
dp[d] = locMax;
length = max (length, locMax);
return length;
if __name__ = = "__main__" :
arr = [ 1 , 12 , 44 , 29 , 33 , 96 , 89 ];
n = len (arr)
print (findSubsequence(arr, n));
|
C#
using System;
class GFG
{
static int findSubsequence( int []arr, int n)
{
int len = 1;
int tmp;
int i, j, d;
int []dp = new int [10];
int []cnt = new int [10];
int locMax;
tmp = arr[0];
while (tmp > 0)
{
dp[tmp % 10] = 1;
tmp /= 10;
}
for (i = 1; i < n; i++)
{
tmp = arr[i];
locMax = 1;
for ( int k = 0; k < 10; k++)
{
cnt[k] = 0;
}
while (tmp > 0)
{
cnt[tmp % 10] = 1;
tmp /= 10;
}
for (d = 0; d <= 9; d++)
{
if (cnt[d] == 1)
{
dp[d]++;
locMax = Math.Max(locMax, dp[d]);
}
}
for (d = 0; d <= 9; d++)
{
if (cnt[d] == 1)
{
dp[d] = locMax;
}
}
len = Math.Max(len, locMax);
}
return len;
}
public static void Main(String[] args)
{
int []arr = { 1, 12, 44, 29, 33, 96, 89 };
int n = arr.Length;
Console.WriteLine(findSubsequence(arr, n));
}
}
|
Javascript
<script>
function findSubsequence(arr , n) {
var len = 1;
var tmp;
var i, j, d;
var dp = Array(10).fill(0);
var cnt = Array(10).fill(0);
var locMax;
tmp = arr[0];
while (tmp > 0) {
dp[tmp % 10] = 1;
tmp = parseInt(tmp/10);
}
for ( var i = 1; i < n; i++) {
tmp = arr[i];
locMax = 1;
cnt.fill( 0);
while (tmp > 0) {
cnt[tmp % 10] = 1;
tmp = parseInt(tmp/10);
}
for (d = 0; d <= 9; d++) {
if (cnt[d] == 1) {
dp[d]++;
locMax = Math.max(locMax, dp[d]);
}
}
for (d = 0; d <= 9; d++) {
if (cnt[d] == 1) {
dp[d] = locMax;
}
}
len = Math.max(len, locMax);
}
return len;
}
var arr = [ 1, 12, 44, 29, 33, 96, 89 ];
var n = arr.length;
document.write(findSubsequence(arr, n));
</script>
|
PHP
<?php
function findSubsequence( $arr , $n )
{
$len = 1;
$dp = array_fill (0, 10, NULL);
$tmp = $arr [0];
while ( $tmp > 0)
{
$dp [ $tmp % 10] = 1;
$tmp = intval ( $tmp / 10);
}
for ( $i = 1; $i < $n ; $i ++)
{
$tmp = $arr [ $i ];
$locMax = 1;
$cnt = array_fill (0, 10, NULL);
while ( $tmp > 0)
{
$cnt [ $tmp % 10] = 1;
$tmp = intval ( $tmp / 10);
}
for ( $d = 0; $d <= 9; $d ++)
{
if ( $cnt [ $d ])
{
$dp [ $d ]++;
$locMax = max( $locMax , $dp [ $d ]);
}
}
for ( $d = 0; $d <= 9; $d ++)
{
if ( $cnt [ $d ])
{
$dp [ $d ] = $locMax ;
}
}
$len = max( $len , $locMax );
}
return $len ;
}
$arr = array ( 1, 12, 44, 29, 33, 96, 89 );
$n = sizeof( $arr );
echo findSubsequence( $arr , $n );
?>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Using Brute Force:
Approach:
We can generate all possible sub-sequences of the given sequence and check if any adjacent elements have at least one common digit 3. We can then return the length of the longest sub-sequence that satisfies this condition.
Define a function has_common_digit(a, b) that takes two integers a and b as input and returns True if they have at least one digit in common, otherwise False.
Define a function longest_subsequence(arr) that takes a list of integers arr as input and returns the length of the longest subsequence of arr such that adjacent elements have at least one common digit.
Initialize a variable max_len to 0, which will store the length of the longest subsequence found so far.
Generate all possible subsequences of arr using a binary representation of integers from 0 to 2^n – 1, where n is the length of arr. Each bit in the binary representation represents the presence or absence of the corresponding element in the subsequence.
Check each subsequence generated in step 4 for validity: iterate over the subsequence and check if adjacent elements have at least one common digit using the has_common_digit function. If any adjacent elements do not have a common digit, mark the subsequence as invalid and move on to the next one.
If a valid subsequence is found, compare its length to max_len and update max_len if the length of the new subsequence is longer.
Return max_len as the length of the longest subsequence.
(Optional) Generate the longest subsequence itself by constructing a new list from the elements of arr that correspond to the 1-bits in the binary representation of the longest subsequence.
C++
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool has_common_digit( int a, int b) {
string a_str = to_string(a);
string b_str = to_string(b);
for ( char digit : a_str) {
if (b_str.find(digit) != string::npos) {
return true ;
}
}
return false ;
}
vector< int > longest_subsequence(vector< int >& arr) {
int n = arr.size();
int max_len = 0;
vector< int > max_subsequence;
for ( int i = 0; i < (1 << n); i++) {
vector< int > subsequence;
for ( int j = 0; j < n; j++) {
if (i & (1 << j)) {
subsequence.push_back(arr[j]);
}
}
if (subsequence.size() > max_len) {
bool valid = true ;
for ( int j = 0; j < subsequence.size() - 1; j++) {
if (!has_common_digit(subsequence[j], subsequence[j+1])) {
valid = false ;
break ;
}
}
if (valid) {
max_len = subsequence.size();
max_subsequence = subsequence;
}
}
}
return max_subsequence;
}
int main() {
vector< int > arr1 = {1, 12, 44, 29, 33, 96, 89};
vector< int > arr2 = {12, 23, 45, 43, 36, 97};
cout << "Input: arr1 = " ;
for ( int x : arr1) {
cout << x << " " ;
}
cout << endl;
vector< int > max_subsequence1 = longest_subsequence(arr1);
cout << "Output: " << max_subsequence1.size() << endl;
cout << "The longest sub-sequence is " ;
for ( int x : max_subsequence1) {
cout << x << " " ;
}
cout << endl;
cout << "Input: arr2 = " ;
for ( int x : arr2) {
cout << x << " " ;
}
cout << endl;
vector< int > max_subsequence2 = longest_subsequence(arr2);
cout << "Output: " << max_subsequence2.size() << endl;
cout << "The longest sub-sequence is " ;
for ( int x : max_subsequence2) {
cout << x << " " ;
}
cout << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
class GFG {
public static boolean hasCommonDigit( int a, int b) {
String aStr = Integer.toString(a);
String bStr = Integer.toString(b);
for ( char digit : aStr.toCharArray()) {
if (bStr.indexOf(digit) != - 1 ) {
return true ;
}
}
return false ;
}
public static List<Integer> longestSubsequence(List<Integer> arr) {
int n = arr.size();
int maxLen = 0 ;
List<Integer> maxSubsequence = new ArrayList<>();
for ( int i = 0 ; i < ( 1 << n); i++) {
List<Integer> subsequence = new ArrayList<>();
for ( int j = 0 ; j < n; j++) {
if ((i & ( 1 << j)) != 0 ) {
subsequence.add(arr.get(j));
}
}
if (subsequence.size() > maxLen) {
boolean valid = true ;
for ( int j = 0 ; j < subsequence.size() - 1 ; j++) {
if (!hasCommonDigit(subsequence.get(j), subsequence.get(j + 1 ))) {
valid = false ;
break ;
}
}
if (valid) {
maxLen = subsequence.size();
maxSubsequence = subsequence;
}
}
}
return maxSubsequence;
}
public static void main(String[] args) {
List<Integer> arr1 = List.of( 1 , 12 , 44 , 29 , 33 , 96 , 89 );
List<Integer> arr2 = List.of( 12 , 23 , 45 , 43 , 36 , 97 );
System.out.print( "Input: arr1 = " );
for ( int x : arr1) {
System.out.print(x + " " );
}
System.out.println();
List<Integer> maxSubsequence1 = longestSubsequence(arr1);
System.out.println( "Output: " + maxSubsequence1.size());
System.out.print( "The longest sub-sequence is " );
for ( int x : maxSubsequence1) {
System.out.print(x + " " );
}
System.out.println();
System.out.print( "Input: arr2 = " );
for ( int x : arr2) {
System.out.print(x + " " );
}
System.out.println();
List<Integer> maxSubsequence2 = longestSubsequence(arr2);
System.out.println( "Output: " + maxSubsequence2.size());
System.out.print( "The longest sub-sequence is " );
for ( int x : maxSubsequence2) {
System.out.print(x + " " );
}
System.out.println();
}
}
|
Python3
def has_common_digit(a, b):
for digit in str (a):
if digit in str (b):
return True
return False
def longest_subsequence(arr):
n = len (arr)
max_len = 0
max_subsequence = []
for i in range ( 2 * * n):
subsequence = [arr[j] for j in range (n) if (i & ( 1 << j))]
if len (subsequence) > max_len:
valid = True
for j in range ( len (subsequence) - 1 ):
if not has_common_digit(subsequence[j], subsequence[j + 1 ]):
valid = False
break
if valid:
max_len = len (subsequence)
max_subsequence = subsequence
return max_subsequence
arr1 = [ 1 , 12 , 44 , 29 , 33 , 96 , 89 ]
arr2 = [ 12 , 23 , 45 , 43 , 36 , 97 ]
print ( "Input: arr1 =" , arr1)
max_subsequence1 = longest_subsequence(arr1)
print ( "Output:" , len (max_subsequence1))
print ( "The longest sub-sequence is" , max_subsequence1)
print ( "Input: arr2 =" , arr2)
max_subsequence2 = longest_subsequence(arr2)
print ( "Output:" , len (max_subsequence2))
print ( "The longest sub-sequence is" , max_subsequence2)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG {
static bool HasCommonDigit( int a, int b)
{
string aStr = a.ToString();
string bStr = b.ToString();
foreach ( char digit in aStr)
{
if (bStr.Contains(digit)) {
return true ;
}
}
return false ;
}
static List< int > LongestSubsequence(List< int > arr)
{
int n = arr.Count;
int maxLen = 0;
List< int > maxSubsequence = new List< int >();
for ( int i = 0; i < (1 << n); i++) {
List< int > subsequence = new List< int >();
for ( int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
subsequence.Add(arr[j]);
}
}
if (subsequence.Count > maxLen) {
bool valid = true ;
for ( int j = 0; j < subsequence.Count - 1;
j++) {
if (!HasCommonDigit(
subsequence[j],
subsequence[j + 1])) {
valid = false ;
break ;
}
}
if (valid) {
maxLen = subsequence.Count;
maxSubsequence = subsequence;
}
}
}
return maxSubsequence;
}
static void Main()
{
List< int > arr1
= new List< int >{ 1, 12, 44, 29, 33, 96, 89 };
List< int > arr2
= new List< int >{ 12, 23, 45, 43, 36, 97 };
Console.Write( "Input: arr1 = " );
Console.WriteLine( string .Join( " " , arr1));
List< int > maxSubsequence1
= LongestSubsequence(arr1);
Console.WriteLine( "Output: "
+ maxSubsequence1.Count);
Console.Write( "The longest sub-sequence is " );
Console.WriteLine(
string .Join( " " , maxSubsequence1));
Console.Write( "Input: arr2 = " );
Console.WriteLine( string .Join( " " , arr2));
List< int > maxSubsequence2
= LongestSubsequence(arr2);
Console.WriteLine( "Output: "
+ maxSubsequence2.Count);
Console.Write( "The longest sub-sequence is " );
Console.WriteLine(
string .Join( " " , maxSubsequence2));
}
}
|
Javascript
function hasCommonDigit(a, b) {
const aStr = a.toString();
const bStr = b.toString();
for (const digit of aStr) {
if (bStr.includes(digit)) {
return true ;
}
}
return false ;
}
function longestSubsequence(arr) {
const n = arr.length;
let maxLen = 0;
let maxSubsequence = [];
for (let i = 0; i < (1 << n); i++) {
const subsequence = [];
for (let j = 0; j < n; j++) {
if (i & (1 << j)) {
subsequence.push(arr[j]);
}
}
if (subsequence.length > maxLen) {
let valid = true ;
for (let j = 0; j < subsequence.length - 1; j++) {
if (!hasCommonDigit(subsequence[j], subsequence[j + 1])) {
valid = false ;
break ;
}
}
if (valid) {
maxLen = subsequence.length;
maxSubsequence = subsequence;
}
}
}
return maxSubsequence;
}
const arr1 = [1, 12, 44, 29, 33, 96, 89];
const arr2 = [12, 23, 45, 43, 36, 97];
console.log( "Input: arr1 =" , arr1.join( " " ));
const maxSubsequence1 = longestSubsequence(arr1);
console.log( "Output:" , maxSubsequence1.length);
console.log( "The longest sub-sequence is" , maxSubsequence1.join( " " ));
console.log( "Input: arr2 =" , arr2.join( " " ));
const maxSubsequence2 = longestSubsequence(arr2);
console.log( "Output:" , maxSubsequence2.length);
console.log( "The longest sub-sequence is" , maxSubsequence2.join( " " ));
|
OutputInput: arr1 = [1, 12, 44, 29, 33, 96, 89]
Output: 5
The longest sub-sequence is [1, 12, 29, 96, 89]
Input: arr2 = [12, 23, 45, 43, 36, 97]
Output: 4
The longest sub-sequence is [12, 23, 43, 36]
Time Complexity: O(2^n) where n is the length of the sequence
Space Complexity: O(n)