
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 3C2.
So total permutations will be = 3C1 + 3C2 for num=4
For any num x, count will be = x-1C1 + x-1C2+.....+x-1Cc-2 = 2x-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 inum
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