Print all subsequences in first decreasing then increasing by selecting N/2 elements from [1, N]
Given a positive integer N, the task is to print all the subsequences of the array in such a way that the subsequence is first decreasing then increasing by selecting ceil(N/2) elements from 1 to N.
Examples:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: N = 5
Output :
(2, 1, 3), (2, 1, 4), (2, 1, 5), (3, 1, 2), (3, 1, 4), (3, 1, 5), (3, 2, 4), (3, 2, 5), (4, 1, 2),
(4, 1, 3), (4, 1, 5), (4, 2, 3), (4, 2, 5), (4, 3, 5), (5, 1, 2), (5, 1, 3), (5, 1, 4), (5, 2, 3),
(5, 2, 4), (5, 3, 4).
These are the valid sequences of size 3 which first decreases and then increases.
Approach: The approach is based on using the inbuilt function of python itertools.permutation to generate all the subsequences of size ceil(N/2). Follow the steps below to solve the problem:
- Initialize an array arr[] and insert all the elements from 1 to N.
- Initialize a variable K as ceil(N/2).
- Insert all the subsequences in the array “sequences” using the built-in function called itertools.permutations(arr, k).
- Iterate through the array sequences using the variable seq and perform the following steps:
- Check if seq[1]>seq[0] or seq[-1]< seq[-2] or if the seq is increasing or descending then continue else this sequence is not satisfying the condition mentioned above.
- If there is more than 1 element than seq[i]<seq[i-1] and seq[i] <seq[i+1] then also continue and do not print the array.
- If the seq does not follow any of the points above, then print the seq.
Below is the implementation of the above approach:
Python3
# Python3 program for the above approach # import the ceil permutations in function from math import ceil # Function to generate all the permutationsfrom itertools import permutations # Function to check if the sequence is valid# or notdef ValidSubsequence(seq): # If first element is greater or last second # element is greater than last element if (seq[0]<seq[1]) or (seq[-1]<seq[-2]): return False i = 0 # If the sequence is decreasing or increasing # or sequence is increasing return false while i<len(seq)-1: if seq[i]>seq[i + 1]: pass elif seq[i]<seq[i + 1]: j = i + 1 if (j != len(seq)-1) and (seq[j]>seq[j + 1]): return False i+= 1 # If the sequence do not follow above condition # Return True return True # Driver codeN = 5K = ceil(N / 2)arr = list(range(1, N + 1)) # Generate all permutation of size N / 2 using# default functionsequences = list(permutations(arr, K)) # Print the sequence which is valid valley subsequencefor seq in sequences: # Check whether the seq is valid or not # Function Call if ValidSubsequence(seq): print(seq, end =" ") |
(2, 1, 3) (2, 1, 4) (2, 1, 5) (3, 1, 2) (3, 1, 4) (3, 1, 5) (3, 2, 4) (3, 2, 5) (4, 1, 2) (4, 1, 3) (4, 1, 5) (4, 2, 3) (4, 2, 5) (4, 3, 5) (5, 1, 2) (5, 1, 3) (5, 1, 4) (5, 2, 3) (5, 2, 4) (5, 3, 4)
Time Complexity: O(CNN/2 * ceil(N/2)! *N)
Auxiliary Space: O(CNN/2 * ceil(N/2)!)


