Given an integer N, calculate the number of permutations of A = [1, 2, …, N] which are first decreasing and then increasing.
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, N-1Ci = 2N-1 - 2 (from binomial theorem)
# Python3 implementation of the above approach
mod = 1000000007
# Function to compute a^n % mod
def power(a, n):
if(n == 0):
p = power(a, int(n / 2)) % mod;
p = (p * p) % mod
if (n & 1):
p = (p * a) % mod
# Function to count permutations that are
# first decreasing and then increasing
# For n = 1 return 0
if (n == 1):
# Calculate and return result
return (power(2, n – 1) – 2) % mod
# Driver code
if __name__ == ‘__main__’:
n = 5
# This code is contributed by
- Minimum in an array which is first decreasing then increasing
- Check if an array is increasing or decreasing
- Sum of array elements that is first continuously increasing then decreasing
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Print array elements in alternatively increasing and decreasing order
- Number of Permutations such that no Three Terms forms Increasing Subsequence
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Count permutation such that sequence is non decreasing
- Find the count of Strictly decreasing Subarrays
- Count the number of special permutations
- Count permutations that produce positive result
- Count the number of non-increasing subarrays
- Generate all binary permutations such that there are more or equal 1's than 0's before every point in all permutations
- Sort an array according to the increasing count of distinct Prime Factors
- Tidy Number (Digits in non-decreasing Order)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.