Open In App

Number of Permutations such that no Three Terms forms Increasing Subsequence

Last Updated : 01 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:  

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 = 3
print(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




<?php
// PHP program to find the
// nth catalan number
 
// Returns value of Binomial
// Coefficient C(n, k)
function 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 = 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
?>


Javascript




<script>
 
 
// Javascript program to find the
// nth catalan number
 
// Returns value of Binomial Coefficient C(n, k)
function binomialCoeff(n, k)
{
    var 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 (var 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
function catalan(n)
{
    // Calculate value of 2nCn
    var c = binomialCoeff(2 * n, n);
 
    // return 2nCn/(n+1)
    return c / (n + 1);
}
 
// Driver code
var n = 3;
document.write( catalan(n));
 
</script>


Output: 

5

 

Time Complexity: O(n)

Auxiliary Space: O(1)



Similar Reads

Number of quadruples where the first three terms are in AP and last three terms are in GP
Given an array arr[] of N integers. The task is to find the number of index quadruples (i, j, k, l) such that a[i], a[j] and a[k] are in AP and a[j], a[k] and a[l] are in GP. All the quadruples have to be distinct.Examples: Input: arr[] = {2, 6, 4, 9, 2} Output: 2 Indexes of elements in the quadruples are (0, 2, 1, 3) and (4, 2, 1, 3) and correspon
9 min read
Longest increasing subsequence which forms a subarray in the sorted representation of the array
Given an array arr[] of N integers, the task is to find the length of the longest increasing subsequence such that it forms a subarray when the original array is sorted. Examples: Input: arr[] = { 2, 6, 4, 8, 2, 9 }Output: 3Explanation: Sorted array: {2, 2, 4, 6, 8, 9}All possible non-decreasing sequences of the original array are {2}, {6}, {4}, {8
11 min read
Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
Generate all permutations of a given length such that every permutation has more or equals 1's than 0's in all prefixes of the permutation. Examples: Input: len = 4 Output: 1111 1110 1101 1100 1011 1010 Note that a permutation like 0101 can not be in output because there are more 0's from index 0 to 2 in this permutation. Input: len = 3 Output: 111
5 min read
Find the Increasing subsequence of length three with maximum product
Given a sequence of non-negative integers, find the subsequence of length 3 having maximum product with the numbers of the subsequence being in ascending order. Examples: Input: arr[] = {6, 7, 8, 1, 2, 3, 9, 10} Output: 8 9 10 Input: arr[] = {1, 5, 10, 8, 9} Output: 5 8 9 Approach: Since we want to find the maximum product, we need to find followin
10 min read
Longest Increasing Subsequence using Longest Common Subsequence Algorithm
Given an array arr[] of N integers, the task is to find and print the Longest Increasing Subsequence.Examples: Input: arr[] = {12, 34, 1, 5, 40, 80} Output: 4 {12, 34, 40, 80} and {1, 5, 40, 80} are the longest increasing subsequences.Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60, 80} Output: 6 Prerequisite: LCS, LISApproach: The longest increasing
12 min read
Minimum number of characters required to be added to a String such that all lowercase alphabets occurs as a subsequence in increasing order
Given a string S consisting of N characters, the task is to find the minimum number of characters that must be added to S such that all lowercase alphabets occur as a subsequence in increasing order in S. Examples: Input: S = "axyabzaxd"Output: 22Explanation:The characters from b to w (bcdefghijklmnopqrstuvw) whose count is 22, is missing in the gi
7 min read
Find three element from given three arrays such that their sum is X | Set 2
Given three sorted integer arrays A[], B[] and C[], the task is to find three integers, one from each array such that they sum up to a given target value X. Print Yes or No depending on whether such triplet exists or not.Examples: Input: A[] = {2}, B[] = {1, 6, 7}, C[] = {4, 5}, X = 12 Output: Yes A[0] + B[1] + C[0] = 2 + 6 + 4 = 12Input: A[] = {2}
9 min read
Find three element from different three arrays such that a + b + c = sum
Given three integer arrays and a "sum", the task is to check if there are three elements a, b, c such that a + b + c = sum and a, b and c belong to three different arrays. Examples : Input : a1[] = { 1 , 2 , 3 , 4 , 5 }; a2[] = { 2 , 3 , 6 , 1 , 2 }; a3[] = { 3 , 2 , 4 , 5 , 6 }; sum = 9 Output : Yes 1 + 2 + 6 = 9 here 1 from a1[] and 2 from a2[] a
22 min read
Remove minimum elements from array such that no three consecutive element are either increasing or decreasing
Given an array of n distinct positive integers. The task is to find the minimum number of elements to be removed such that no three consecutive elements in the array are either increasing or decreasing. That is, after removal either ai - 1 &gt; ai &lt; ai + 1 or ai - 1 &lt; ai &gt; ai + 1. Examples : Input : arr[] = {5, 2, 3, 6, 1} Output : 1 Given
8 min read
Find n terms of Fibonacci type series with given first two terms
Given first two numbers of series, find n terms of series with these two numbers. The given series follows the same concept as Fibonacci series, i.e., n-th term is sum of (n-1)-th and (n-2)-th terms. Examples: Input: first = 5, sec = 8, n = 5 Output: 5, 8, 13, 21, 34 Input: first = 2, sec = 4, n = 5 Output: 2, 4, 6, 10, 16 Approach: The approach is
7 min read
Article Tags :
Practice Tags :