- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count permutations that are first decreasing then increasing in C++

We are a variable num. The goal is to find the count of permutations of numbers between [1,num] in which numbers are first decreasing then increasing. For example if num=3 then numbers are 1,2,3. The permutations will be [ 3,1,2 ] and [2,1,3] and count is 2.

We know that in every permutation the change from decreasing of numbers to increasing of numbers will be decided based on position of 1 which is smallest. After each 1 the numbers will start increasing. For a permutation to decrease and then increase, 1 should lie between position 2 and num-1. [ → ...1…. → ].

If 1 is at the start then series will be fully increasing [ 1.. → ] , if it is at the end then series will be fully decreasing [ … → 1 ].

Let’s say we have num=4 then

Placing 1 at 2nd position, [ - , 1, - , - ]. For 1st position we can choose from ( 2,3,4) let’s say we pick 2, then the sequence will be [ 2,1,3,4]. So 3C1 permutations are possible in this case.

Placing 1 at 3rd position, [ -, -, 1, - ]. For 1st and 2nd positions select any two out of three (2,3,4). Total permutations will be ^{3}C_{2}.

So total permutations will be = ^{3}C_{1} + ^{3}C_{2} for num=4

For any num x, count will be = ^{x-1}C_{1} + ^{x-1}C_{2}+.....+^{x-1}C_{c-2} = 2_{x-1} - 2 from binomial theorem.

Let us understand with examples

**Input** − num=4

**Output** − Count of permutations that are first decreasing then increasing are: 6

**Explanation** − Permutations will be −

[ 2, 1, 3, 4 ], [ 3, 1, 2, 4 ], [ 4, 1, 2, 3 ] → 1 at 2nd position [ 2, 3, 1, 4 ], [ 2, 4, 1, 3 ], [ 3, 4, 1, 2 ] → 1 at 3rd position

**Input** − num=6

**Output** − Count of permutations that are first decreasing then increasing are − 30

**Explanation** − Some Permutations will be −

[ 2, 1, 3, 4, 5, 6 ], [ 3, 1, 2, 4, 5, 6 ], [ 4, 1, 2, 3, 5, 6 ], [ 5, 1, 2, 3, 4, 6 ], [ 6, 1, 2, 3, 4, 5 ] ……[ 6, 5, 4, 3, 1, 2].

## Approach used in the below program is as follows

In this approach we will make use of a binomial theorem to directly calculate the permutations from the above formula. Also we will create a function value(long long i, long long num) which returns i^{num}

Take variable num as input.

Function permutations_increase_decrease(int num) takes num and returns the count of permutations that are first decreasing then increasing from numbers 1 to num.

Function value(long long i, long long num) is used to calculate ( inum) % temp. Where temp=1000000007.

Inside permutations_increase_decrease(int num) take temp=1000000007.

If num is 1 no permutation possible so return 0.

Else set count = (value(2, num - 1) - 2) % temp ); using formula.

Return count as result.

## Example

#include <bits/stdc++.h> using namespace std; long long value(long long i, long long num){ int temp = 1000000007; if (num == 0){ return 1; } long long j = value(i, num / 2) % temp; j = (j * j) % temp; if(num & 1){ j = (j * i) % temp; } return j; } int permutations_increase_decrease(int num){ int temp = 1000000007; if (num == 1){ return 0; } int count = (value(2, num - 1) - 2) % temp; return count; } int main(){ int num = 4; cout<<"Count of permutations that are first decreasing then increasing are: "<<permutations_increase_decrease(num); return 0; }

## Output

If we run the above code it will generate the following output −

Count of permutations that are first decreasing then increasing are: 6

- Related Questions & Answers
- Find the maximum element in an array which is first increasing and then decreasing in C++
- Program to find length of longest strictly increasing then decreasing sublist in Python
- Strictly increasing or decreasing array - JavaScript
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python
- Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in C++
- Count no. of columns that are not sorted in increasing order in C++
- Print array elements in alternatively increasing and decreasing order in C++
- Find an element in an array such that elements form a strictly decreasing and increasing sequence in Python
- Program to check right rotation forms increasing or decreasing array with first n natural numbers or not in Python
- Count all increasing subsequences in C++
- Count Strictly Increasing Subarrays in C++
- How to check whether the elements of a vector are arranged in an increasing order or decreasing order?
- Program to check whether list is strictly increasing or strictly decreasing in Python
- Find the count of Strictly decreasing Subarrays in C++
- Minimum toggles to partition a binary array so that it has first 0s then 1s in C++