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 a fully increasing or fully decreasing sequence.

For example, consider N = 5 and position = 2, i.e 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. Configuration looks like [3, 1, _, _, _]. Only 1 sequence is possible i.e [3, 1, 2, 4, 5]. Thus for every selected element to fill at position 1, a sequence is possible. With this configuration, total of 4C1 permutations are 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,
 \sum_{i = 1}^{N-2}  N-1Ci = 2N-1 - 2 (from binomial theorem)

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <bits/stdc++.h>
  
#define ll long long
using namespace std;
  
const int mod = 1000000007;
  
// Function to compute a^n % mod
ll 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 increasing
int 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 code
int main()
{
    int n = 5;
    cout << countPermutations(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach
mod = 1000000007
  
# Function to compute a^n % mod
def 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 increasing
def countPermutations(n):
      
    # For n = 1 return 0
    if (n == 1):
        return 0
  
    # Calculate and return result
    return (power(2, n - 1) - 2) % mod
  
# Driver code
if __name__ == '__main__':
    n = 5
    print(countPermutations(n))
      
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using 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..

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the above approach
  
$mod = 1000000007;
  
// Function to compute a^n % mod
function power($a, $n)
{
    global $mod;
    if ($n == 0)
        return 1;
  
    $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 increasing
function countPermutations($n)
{
    global $mod;
      
    // For n = 1 return 0
    if ($n == 1) 
    {
        return 0;
    }
  
    // Calculate and return result
    return (power(2, $n - 1) - 2) % $mod;
}
  
// Driver Code
$n = 5;
echo countPermutations($n);
  
// This code is contributed by Tushil.
?>

chevron_right


Output:

14


My Personal Notes arrow_drop_up

An enthusiastic Java and web developer with a little affinity for tea, cricket, English, etymology, and reading

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.