Number of Permutations such that no Three Terms forms Increasing Subsequence
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 n’th 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:
C++
// C++ program to find the // nth catalan number #include <bits/stdc++.h> using namespace std; // Returns value of Binomial Coefficient C(n, k) unsigned long int binomialCoeff(unsigned int n, unsigned int k) { unsigned long int res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n*(n-1)*---*(n-k+1)] / [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // A Binomial coefficient based function // to find nth catalan // number in O(n) time unsigned long int catalan(unsigned int n) { // Calculate value of 2nCn unsigned long int c = binomialCoeff(2 * n, n); // return 2nCn/(n+1) return c / (n + 1); } // Driver code int main() { int n = 3; cout << catalan(n) << endl; return 0; } |
Java
// Java program to find the // nth catalan number import java.io.*; class GFG { // Returns value of Binomial // Coefficient C(n, k) static long binomialCoeff(long n, long k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n*(n-1)*---*(n-k+1)] / // [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // A Binomial coefficient based // function to find nth catalan // number in O(n) time static long catalan(long n) { // Calculate value of 2nCn long c = binomialCoeff(2 * n, n); // return 2nCn/(n+1) return c / (n + 1); } // Driver code public static void main (String[] args) { int n = 3; System.out.println(catalan(n)); } } // This code has been contributed // by inder_verma. |
Python3
# Python program to find the # nth catalan number # Returns value of Binomial # Coefficient C(n, k) def binomialCoeff(n, k): res = 1 # Since C(n, k) = C(n, n-k) if k > n - k: k=n-k # Calculate value of # [n*(n-1)*---*(n-k+1)] // # [k*(k-1)*---*1] for i in range(k): res = res * (n - i) res = res // (i + 1) return res # A Binomial coefficient based # function to find nth catalan # number in O(n) time def catalan(n): # Calculate value of 2nCn c = binomialCoeff(2 * n, n) # return 2nCn/(n+1) return c // (n + 1) # Driver code n = 3print(catalan(n)) # This code is contributed # by sahil shelangia |
C#
// C# program to find the // nth catalan number using System; class GFG { // Returns value of Binomial // Coefficient C(n, k) static long binomialCoeff(long n, long k) { long res = 1; // Since C(n, k) = C(n, n-k) if (k > n - k) k = n - k; // Calculate value of // [n*(n-1)*---*(n-k+1)] / // [k*(k-1)*---*1] for (int i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } // A Binomial coefficient based // function to find nth catalan // number in O(n) time static long catalan(long n) { // Calculate value of 2nCn long c = binomialCoeff(2 * n, n); // return 2nCn/(n+1) return c / (n + 1); } // Driver code public static void Main (String[] args) { int n = 3; Console.WriteLine(catalan(n)); } } // This code is contributed // by Kirti_Mangal |
PHP
$n – $k)
$k = $n – $k;
// Calculate value of
// [n*(n-1)*—*(n-k+1)] //
// [k*(k-1)*—*1]
for ($i = 0; $i < $k; $i++)
{
$res = $res * ($n - $i);
$res = $res / ($i + 1);
}
return $res;
}
// A Binomial coefficient based
// function to find nth catalan
// number in O(n) time
function catalan($n)
{
// Calculate value of 2nCn
$c = binomialCoeff(2 * $n, $n);
// return 2nCn/(n+1)
return $c / ($n + 1);
}
// Driver code
$n = 3;
print(catalan($n));
// This code is contributed
// by mits
?>
5
Recommended Posts:
- Minimum number of elements which are not part of Increasing or decreasing subsequence in array
- Count permutations that are first decreasing then increasing.
- Longest common subsequence with permutations allowed
- Longest Increasing Odd Even Subsequence
- Longest Increasing Subsequence Size (N log N)
- Longest Common Increasing Subsequence (LCS + LIS)
- Printing Maximum Sum Increasing Subsequence
- Print all Increasing Subsequence of a List
- Number of quadruples where the first three terms are in AP and last three terms are in GP
- Construction of Longest Increasing Subsequence(LIS) and printing LIS sequence
- Find the Increasing subsequence of length three with maximum product
- Divide array into increasing and decreasing subsequence without changing the order
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Check if the first and last digit of the smallest number forms a prime
- Number of possible permutations when absolute difference between number of elements to the right and left are given
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



