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 <iostream>
#include <vector>
using namespace std;
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);
}
}
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);
}
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;
}
int main()
{
vector< int > arr = {1, 2, 3};
auto res = odd_element_subsequences(arr);
cout<< "[" ;
for ( auto v:res){
cout<< "[" ;
for ( int x:v)
cout<<x;
cout<< "]" ;
}
cout<< "]" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
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);
}
}
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);
}
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)
{
List<Integer> arr = new ArrayList<Integer>();
arr.add( 1 );
arr.add( 2 );
arr.add( 3 );
List<List<Integer> > res
= odd_element_subsequences(arr);
System.out.print(res);
}
}
|
Python3
def odd_element_subsequences(lst):
odd_elements = []
for x in lst:
if x % 2 = = 1 :
odd_elements.append(x)
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)
odd_length_subsequences = [x for x in subsequences if len (x) % 2 = = 1 ]
return odd_length_subsequences
arr = [ 1 , 2 , 3 ]
print (odd_element_subsequences(arr))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
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);
}
}
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);
}
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()
{
List< int > arr = new List< int >{ 1, 2, 3 };
List<List< int > > res = OddElementSubsequences(arr);
Console.Write( "[" );
foreach ( var sublist in res)
{
Console.Write( "[" );
Console.Write( string .Join( ", " , sublist));
Console.Write( "]" );
}
Console.Write( "]" );
}
}
|
Javascript
function odd_element_subsequences(lst)
{
let odd_elements=[];
for (let x of lst)
{
if (x % 2 == 1)
{
odd_elements.push(x);
}
}
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);
}
let odd_length_subsequences=[];
for (let x of subsequences)
{
if (x.length % 2 == 1)
{
odd_length_subsequences.push(x);
}
}
return odd_length_subsequences;
}
let arr = [1, 2, 3];
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( "]" );
|
Time Complexity: O(N * 2^M)
Auxiliary Space: O(N * 2^M)
Related Articles:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...