# Count permutations that are first decreasing then increasing.

Given an integer N, calculate the number of permutations of A = [1, 2, …, N] which are first decreasing and then increasing.
Examples:

Input: N = 5
Output : 14
Following are the sub-sequences which are first decreasing and then increasing:
[2, 1, 3, 4, 5], [3, 1, 2, 4, 5], [4, 1, 2, 3, 5], [5, 1, 2, 3, 4],
[3, 2, 1, 4, 5], [4, 2, 1, 3, 5], [5, 2, 1, 3, 4], [4, 3, 1, 2, 5],
[5, 3, 1, 2, 4], [5, 4, 1, 2, 3], [5, 4, 3, 1, 2], [5, 4, 2, 1, 3],
[5, 3, 2, 1, 4], [4, 3, 2, 1, 5]
Input : N = 1
Output : 0

Approach: It is clear that the point at which sequence becomes increasing from decreasing is occupied by the smallest element of the permutation which is 1. Also, decreasing sequence is always followed by an increasing sequence which means that the smallest element can have positions ranging [2, …, N-1]. Otherwise, it will result in a fully increasing or fully decreasing sequence.
For example, consider N = 5 and position = 2, i.e the smallest element at position 2 in the sequence. Count all possible sequences with Configuration = [_, 1, _, _, _].
Select any 1 element from the remaining 4 elements (2, 3, 4, 5) to fill position 1. For example, we select element = 3. The configuration looks like [3, 1, _, _, _]. Only 1 sequence is possible i.e [3, 1, 2, 4, 5]. Thus, for every selected element to fill position 1, a sequence is possible. With this configuration, a total of 4C1 permutations is possible.
Now, consider the configuration = [_, _, 1, _, _].
A similar approach can be applied by selecting any 2 elements from the remaining 4 elements and for every pair of elements, a single valid permutation is possible. Hence, the number of permutations for this configuration = 4C2
The final configuration possible for N = 5 is [_, _, _, 1, _]
Select any 3 of the remaining 4 elements and for every triplet, a permutation is obtained. Number of permutations, in this case, = 4C3
Final count = 4C1 + 4C2 + 4C3 for N = 5
Generalized result for N:

Count = N-1C1 + N-1C2 + ... + N-1CN-2 which simplifies to,
N-1Ci = 2N-1 - 2 (from binomial theorem)

## C++

 // C++ implementation of the above approach#include  #define ll long longusing namespace std; const int mod = 1000000007; // Function to compute a^n % modll power(ll a, ll n){     if (n == 0)        return 1;     ll p = power(a, n / 2) % mod;    p = (p * p) % mod;    if (n & 1)        p = (p * a) % mod;     return p;} // Function to count permutations that are// first decreasing and then increasingint countPermutations(int n){     // For n = 1 return 0    if (n == 1) {        return 0;    }     // Calculate and return result    return (power(2, n - 1) - 2) % mod;} // Driver codeint main(){    int n = 5;    cout << countPermutations(n);    return 0;}

## Java

 // Java implementation of the above approach class GFG{ static final int mod = 1000000007;  // Function to compute a^n % mod static long power(long a, long n) {      if (n == 0)         return 1;      long p = power(a, n / 2) % mod;     p = (p * p) % mod;     if ((n & 1) == 1)         p = (p * a) % mod;      return p; }  // Function to count permutations that are // first decreasing and then increasing static int countPermutations(int n) {      // For n = 1 return 0     if (n == 1)     {         return 0;     }      // Calculate and return result     return ((int)power(2, n - 1) - 2) % mod; }  // Driver code public static void main(String args[]){     int n = 5;     System.out.println(countPermutations(n)); } } // This code is contributed by Arnab Kundu

## Python3

 # Python3 implementation of the above approachmod = 1000000007 # Function to compute a^n % moddef power(a, n):    if(n == 0):        return 1     p = power(a, int(n / 2)) % mod;    p = (p * p) % mod    if (n & 1):        p = (p * a) % mod     return p # Function to count permutations that are# first decreasing and then increasingdef countPermutations(n):         # For n = 1 return 0    if (n == 1):        return 0     # Calculate and return result    return (power(2, n - 1) - 2) % mod # Driver codeif __name__ == '__main__':    n = 5    print(countPermutations(n))     # This code is contributed by# Surendra_Gangwar

## C#

 // C# implementation of the above approachusing System; class GFG{     static int mod = 1000000007;  // Function to compute a^n % mod static long power(long a, long n) {      if (n == 0)         return 1;      long p = power(a, n / 2) % mod;     p = (p * p) % mod;     if ((n & 1) == 1)         p = (p * a) % mod;      return p; }  // Function to count permutations that are // first decreasing and then increasing static int countPermutations(int n) {      // For n = 1 return 0     if (n == 1)     {         return 0;     }      // Calculate and return result     return ((int)power(2, n - 1) - 2) % mod; }  // Driver code static public void Main (){    int n = 5;     Console.WriteLine(countPermutations(n)); } } // This code is contributed by ajit..

## PHP

 

## Javascript

 

Output:
14

Time Complexity: O(log2n)
Auxiliary Space: O(1), since no extra space has been taken.

Previous
Next