Find odd-length Subsequences containing only odd elements
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 <iostream> #include <vector> using namespace std; // Function to calculate subsequence of // odd length containing odd elements vector<vector< int >> 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<vector< int >> 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<vector< int >> 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<<x; cout<< "]" ; } cout<< "]" ; return 0; } // This code is contributed by lokeshpotta20. |
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<List<Integer> > odd_element_subsequences(List<Integer> lst) { List<Integer> odd_elements = new ArrayList<Integer>(); for ( int x : lst) { if (x % 2 == 1 ) { odd_elements.add(x); } } // Generate all possible subsequences of // odd_elements List<List<Integer> > subsequences = new ArrayList<List<Integer> >(); for ( int i = 0 ; i < ( 1 << odd_elements.size()); i++) { List<Integer> subsequence = new ArrayList<Integer>(); 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<List<Integer> > odd_length_subsequences = new ArrayList<List<Integer> >(); for (List<Integer> x : subsequences) { if (x.size() % 2 == 1 ) { odd_length_subsequences.add(x); } } return odd_length_subsequences; } public static void main(String[] args) { // Input List<Integer> arr = new ArrayList<Integer>(); arr.add( 1 ); arr.add( 2 ); arr.add( 3 ); // Function call List<List<Integer> > 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<List< int > > 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<List< int > > subsequences = new List<List< int > >(); 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<List< int > > oddLengthSubsequences = new List<List< int > >(); 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<List< int > > 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. |
[[1], [3]]
Time Complexity: O(N * 2^M)
Auxiliary Space: O(N * 2^M)
Related Articles:
Please Login to comment...