
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
- Related Articles
- Find the maximum element in an array which is first increasing and then decreasing in C++\n
- Program to find length of longest strictly increasing then decreasing sublist in Python
- 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++
- Strictly increasing or decreasing array - JavaScript
- Count no. of columns that are not sorted in increasing order in C++
- Program to check right rotation forms increasing or decreasing array with first n natural numbers or not in Python
- 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
- 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?
- Find the count of Strictly decreasing Subarrays in C++
- Program to check whether list is strictly increasing or strictly decreasing in Python
- According to Mendeleev’s Periodic Law, the elements were arranged in the periodic table in the order of(a) increasing atomic number(b) decreasing atomic number(c) increasing atomic masses(d) decreasing atomic masses
