![]() |
VOOZH | about |
Given a number N. The task is to find the number of permutations of 1 to N such that no three terms of the permutation form an increasing subsequence.
Examples:
Input : N = 3 Output : 5 Valid permutations : 132, 213, 231, 312 and 321 and not 123 Input : N = 4 Output : 14
The above problem is an application of Catalan numbers. So, the task is to only find the nth Catalan Number. First few Catalan numbers are 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, … (considered from 0th number)
Below is the program to find Nth Catalan Number:
5
Time Complexity: O(n)
Auxiliary Space: O(1)