 Open in App
Not now

# Find odd-length Subsequences containing only odd elements

• Last Updated : 31 Jan, 2023

Given an array arr[] of length N, the task is to find all the possible odd-length subsequences containing odd elements only.

Examples:

Input: N = 6, arr[] = {2, 3, 4, 3, 7, 9}
Output: {{3}, {3}, {7}, {3, 3, 7}, {9}, {3, 3, 9}, {3, 7, 9}, {3, 7, 9}}

Input: N = 2, arr[] = {1, 3}
Output: {{1}, {3}}

Approach: The above problem can be solved with the below:

Iterate through the elements of the input sequence and check if each element is odd. If it is, you can add it to a list of odd elements. Then, you can generate all possible subsequences of this list by using a permutation function or by manually generating the subsequences using nested loops. Finally, you can filter this list of subsequences to only keep those that have an odd length.

Following are the steps of the above approach:

• Initialize an empty list of odd_elements to store the odd elements from the input list.
• Iterate through the elements of the input list and check if each element is odd. If it is, add it to the odd_elements list.
• Initialize an empty list of subsequences to store all possible subsequences of the odd_elements list.
• Use a loop to generate all possible subsequences of odd_elements and add them to the subsequences list.
• This is done using bit manipulation and the 1 << j operator, which generates a binary number with a 1 in the jth position and 0s elsewhere. (For example, 1 << 0 is equal to 1, 1 << 1 is equal to 2, and 1 << 2 is equal to 4).
• Use a list comprehension to filter the subsequences list and only keep subsequences that have an odd length.
• Return the list of odd element subsequences of odd length.

Below is the code that demonstrates this approach:

## C++

 `#include ` `#include `   `using` `namespace` `std;`   `// Function to calculate subsequence of` `// odd length containing odd elements` `vector> odd_element_subsequences(vector<``int``> lst)` `{` `    ``vector<``int``> odd_elements;` `    ``for``(``int` `x : lst)` `    ``{` `        ``if``(x % 2 == 1)` `        ``{` `            ``odd_elements.push_back(x);` `        ``}` `    ``}`   `    ``// Generate all possible subsequences of` `    ``// odd_elements` `    ``vector> subsequences;` `    ``for``(``int` `i = 0; i < (1 << odd_elements.size()); i++)` `    ``{` `        ``vector<``int``> subsequence;` `        ``for``(``int` `j = 0; j < odd_elements.size(); j++)` `        ``{` `            ``if``(i & (1 << j))` `            ``{` `                ``subsequence.push_back(odd_elements[j]);` `            ``}` `        ``}` `        ``subsequences.push_back(subsequence);` `    ``}`   `    ``// Filter to only keep subsequences` `    ``// of odd length` `    ``vector> odd_length_subsequences;` `    ``for``(``auto` `x : subsequences)` `    ``{` `        ``if``(x.size() % 2 == 1)` `        ``{` `            ``odd_length_subsequences.push_back(x);` `        ``}` `    ``}` `    ``return` `odd_length_subsequences;` `}`   `// Driver code` `int` `main()` `{` `    ``// Input` `    ``vector<``int``> arr = {1, 2, 3};`   `    ``// Function call` `    ``auto` `res = odd_element_subsequences(arr);` `  ``cout<<``"["``;` `    ``for``(``auto` `v:res){` `      ``cout<<``"["``;` `        ``for``(``int` `x:v)` `            ``cout<

## Java

 `// Java code implementation for the above program.` `import` `java.io.*;` `import` `java.util.*;`   `class` `GFG {`   `  ``// Function to calculate subsequence of` `  ``// odd length containing odd elements` `  ``static` `List >` `    ``odd_element_subsequences(List lst)` `  ``{` `    ``List odd_elements` `      ``= ``new` `ArrayList();` `    ``for` `(``int` `x : lst) {` `      ``if` `(x % ``2` `== ``1``) {` `        ``odd_elements.add(x);` `      ``}` `    ``}`   `    ``// Generate all possible subsequences of` `    ``// odd_elements` `    ``List > subsequences` `      ``= ``new` `ArrayList >();` `    ``for` `(``int` `i = ``0``; i < (``1` `<< odd_elements.size());` `         ``i++) {` `      ``List subsequence` `        ``= ``new` `ArrayList();` `      ``for` `(``int` `j = ``0``; j < odd_elements.size(); j++) {` `        ``if` `((i & (``1` `<< j)) != ``0``) {` `          ``subsequence.add(odd_elements.get(j));` `        ``}` `      ``}` `      ``subsequences.add(subsequence);` `    ``}`   `    ``// Filter to only keep subsequences` `    ``// of odd length` `    ``List > odd_length_subsequences` `      ``= ``new` `ArrayList >();` `    ``for` `(List x : subsequences) {` `      ``if` `(x.size() % ``2` `== ``1``) {` `        ``odd_length_subsequences.add(x);` `      ``}` `    ``}` `    ``return` `odd_length_subsequences;` `  ``}`   `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``// Input` `    ``List arr = ``new` `ArrayList();` `    ``arr.add(``1``);` `    ``arr.add(``2``);` `    ``arr.add(``3``);`   `    ``// Function call` `    ``List > res` `      ``= odd_element_subsequences(arr);` `    ``System.out.print(res);` `  ``}` `}`   `// This code is contributed by lokesh.`

## Python3

 `# Python code for the above approach`   `# Function to calculate subsequence of` `# odd length containing odd elements`     `def` `odd_element_subsequences(lst):`   `    ``odd_elements ``=` `[]` `    ``for` `x ``in` `lst:` `        ``if` `x ``%` `2` `=``=` `1``:` `            ``odd_elements.append(x)`   `    ``# Generate all possible subsequences of` `    ``# odd_elements` `    ``subsequences ``=` `[]` `    ``for` `i ``in` `range``(``2``*``*``len``(odd_elements)):` `        ``subsequence ``=` `[]` `        ``for` `j ``in` `range``(``len``(odd_elements)):` `            ``if` `i & (``1` `<< j):` `                ``subsequence.append(odd_elements[j])` `        ``subsequences.append(subsequence)`   `    ``# Filter to only keep subsequences` `    ``# of odd length` `    ``odd_length_subsequences ``=` `[x ``for` `x ``in` `subsequences ``if` `len``(x) ``%` `2` `=``=` `1``]` `    ``return` `odd_length_subsequences`     `# Driver code`   `# Input` `arr ``=` `[``1``, ``2``, ``3``]`   `# Function call` `print``(odd_element_subsequences(arr))`

## C#

 `// C# code implementation for the above program.` `using` `System;` `using` `System.Collections.Generic;`   `public` `class` `GFG {`   `  ``// Function to calculate subsequence of` `  ``// odd length containing odd elements` `  ``static` `List >` `    ``OddElementSubsequences(List<``int``> lst)` `  ``{` `    ``List<``int``> oddElements = ``new` `List<``int``>();` `    ``foreach``(``int` `x ``in` `lst)` `    ``{` `      ``if` `(x % 2 == 1) {` `        ``oddElements.Add(x);` `      ``}` `    ``}`   `    ``// Generate all possible subsequences of` `    ``// odd_elements` `    ``List > subsequences` `      ``= ``new` `List >();` `    ``for` `(``int` `i = 0; i < (1 << oddElements.Count); i++) {` `      ``List<``int``> subsequence = ``new` `List<``int``>();` `      ``for` `(``int` `j = 0; j < oddElements.Count; j++) {` `        ``if` `((i & (1 << j)) != 0) {` `          ``subsequence.Add(oddElements[j]);` `        ``}` `      ``}` `      ``subsequences.Add(subsequence);` `    ``}`   `    ``// Filter to only keep subsequences` `    ``// of odd length` `    ``List > oddLengthSubsequences` `      ``= ``new` `List >();` `    ``foreach``(List<``int``> x ``in` `subsequences)` `    ``{` `      ``if` `(x.Count % 2 == 1) {` `        ``oddLengthSubsequences.Add(x);` `      ``}` `    ``}` `    ``return` `oddLengthSubsequences;` `  ``}`   `  ``static` `public` `void` `Main()` `  ``{`   `    ``// Input` `    ``List<``int``> arr = ``new` `List<``int``>{ 1, 2, 3 };`   `    ``// Function call` `    ``List > res = OddElementSubsequences(arr);` `    ``Console.Write(``"["``);` `    ``foreach``(``var` `sublist ``in` `res)` `    ``{` `      ``Console.Write(``"["``);` `      ``Console.Write(``string``.Join(``", "``, sublist));` `      ``Console.Write(``"]"``);` `    ``}` `    ``Console.Write(``"]"``);` `  ``}` `}`   `// This code is contributed by lokeshmvs21.`

## Javascript

 `// Function to calculate subsequence of` `// odd length containing odd elements` `function` `odd_element_subsequences(lst)` `{` `    ``let odd_elements=[];` `    ``for``(let x of lst)` `    ``{` `        ``if``(x % 2 == 1)` `        ``{` `            ``odd_elements.push(x);` `        ``}` `    ``}`   `    ``// Generate all possible subsequences of` `    ``// odd_elements` `    ``let subsequences=[];` `    ``for``(let i = 0; i < (1 << odd_elements.length); i++)` `    ``{` `        ``let subsequence=[];` `        ``for``(let j = 0; j < odd_elements.length; j++)` `        ``{` `            ``if``(i & (1 << j))` `            ``{` `                ``subsequence.push(odd_elements[j]);` `            ``}` `        ``}` `        ``subsequences.push(subsequence);` `    ``}`   `    ``// Filter to only keep subsequences` `    ``// of odd length` `    ``let odd_length_subsequences=[];` `    ``for``(let x of subsequences)` `    ``{` `        ``if``(x.length % 2 == 1)` `        ``{` `            ``odd_length_subsequences.push(x);` `        ``}` `    ``}` `    ``return` `odd_length_subsequences;` `}`   `// Driver code` `    ``// Input` `let arr = [1, 2, 3];`   `// Function call` `let res = odd_element_subsequences(arr);` `console.log(``"["``);` `for``(let v of res)` `{` `    ``console.log(``"["``);` `    ``for``(let x of v)` `        ``console.log(x);` `    ``console.log(``"]"``);` `}` `console.log(``"]"``);` `   `  `// This code is contributed by ritaagarwal.`

Output

`[, ]`

Time Complexity: O(N * 2^M)
Auxiliary Space: O(N * 2^M)

Related Articles:

My Personal Notes arrow_drop_up
Related Articles