Skip to content
Related Articles

Related Articles

Print all subsequences in first decreasing then increasing by selecting N/2 elements from [1, N]

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 31 Jan, 2022
View Discussion
Improve Article
Save Article

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:

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:

C++




// cpp program for the above approach
#include<bits/stdc++.h>
using namespace std;
 
// Function to check if the sequence is valid
// or not
bool  ValidSubsequence(vector<int>seq)
{
 
  // If first element is greater or last second
  // element is greater than last element
  int n = seq.size();
  if ((seq[0] < seq[1]) || (seq[n - 1] < seq[n - 2]))
    return false;
  int i = 0;
 
  // If the sequence is decreasing or increasing
  // or sequence is increasing return false
  // return 0;
  while (i<(seq.size() - 1))
  {
    if (seq[i] > seq[i + 1])
    {
      i++;
      continue;
    }
    else if (seq[i] < seq[i + 1]){
 
      int j = i + 1;
      if ((j != (seq.size())-1) && (seq[j]>seq[j + 1]))
        return false;
    }
    i+= 1;
 
  }
  // If the sequence do not follow above condition
  // Return True
  return true;
 
}
 
 
int main(){
  int N = 5;
  int K = (N+1)/2;
  vector<int>arr,arr0;
  for(int i = 0; i < N; i++)
  {
    arr.push_back(i+1);
  }
 
  // Generate all permutation of size N / 2 using
  // default function
  vector<vector<int>>sequences;
  do{
    vector<int>temp;
    for(int i = 0; i < K; i++)
    {
      temp.push_back(arr[i]);
    }
    sequences.push_back(temp);
  }while(next_permutation(arr.begin(),arr.end()));
 
  // Print the sequence which is valid valley subsequence
  map<vector<int>,int>vis;
  for (auto seq :sequences)
  {
 
    // Check whether the seq is valid or not
    // Function Call
    if (ValidSubsequence(seq) && !vis[seq])
    {
      vis[seq] = 1;
      for(auto j:seq){
        cout<<j<<" ";
      }
      cout<<endl;
    }
  }
}
 
// This code is contributed by Stream_Cipher

Python3




# Python3 program for the above approach
 
# import the ceil permutations in function
from math import ceil
 
# Function to generate all the permutations
from itertools import permutations
 
# Function to check if the sequence is valid
# or not
def 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 code
N = 5
K = ceil(N / 2)
arr = list(range(1, N + 1))
 
# Generate all permutation of size N / 2 using
# default function
sequences = list(permutations(arr, K))
 
# Print the sequence which is valid valley subsequence
for seq in sequences:
  # Check whether the seq is valid or not
  # Function Call
  if ValidSubsequence(seq):
    print(seq, end =" ")

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)

 

Time Complexity: O(CNN/2  * ceil(N/2)! *N)
Auxiliary Space: O(CNN/2  * ceil(N/2)!)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!