Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
Given Four integers N, K, P and Q. The task is to calculate the number of ways to arrange N numbers which are in a range from 1 to K such that the first number is P, the last number is Q and no two adjacent numbers are consecutive.
Examples:
Input: N = 4, K = 3, P = 2, Q = 3 Output: 3 Explanation: For N=4, K=3, P=2, Q=3, ways are [2, 1, 2, 3], [2, 3, 1, 3], [2, 3, 2, 3] Input: N = 5, K = 3, P = 2, Q = 1 Output: 5
Approach: The idea is to use Dynamic Programming to solve this problem.

Let’s try to understand this by taking an example, N = 4, K = 3, P = 2, Q = 1.
We will observe all possible arrangements staring from P and try to find any pattern that can be useful to apply Dynamic programming. 
Below is the image showing all possible arrangements starting from P = 2.

Let A be the array that consists of the number of nodes ending at Q at a particular level
A = { 0, 1, 1, 3 }Let B be the array that consists of the number of nodes NOT ending at Q at a particular level
B = {1, 1, 3, 5 } 
On carefull observation it may be noted that:

A[i] = B[i1]
Reason :
All the favourable nodes ( ending at Q ) will only be produced by nonfavourable nodes(NOT ending at Q) of the previous level. 
B[i] = A[i1]*(K – 1) + B[i1]*(K – 2)
Reason : For A[i1]*(K – 1), some of the nonfavourable nodes are produced by favourable nodes of the previous level, multiply by (K – 1) as each favourable node will produce K1 nonfavourable nodes
 For B[i1]*(K – 2), rest of the nonfavourable nodes are produced by nonfavourable nodes of the previous level, multiply by (K2), as one produced node is favourable, so we subtract 2 from this.

A[i] = B[i1]
// C++ program to calculate Number of // ways to arrange N numbers under // given constraints. #include <bits/stdc++.h> using namespace std; class element { public : // For favourable nodes // (ending at Q) int A; // For Nonfavourable nodes // (NOT ending at Q) int B; }; // Function to print Total number // of ways void NumberOfWays( int n, int k, int p, int q) { element* dp = new element[n]; // If the First number and the // last number is same. if (p == q) { dp[0].A = 1; dp[0].B = 0; } else { dp[0].A = 0; dp[0].B = 1; } // DP approach to find current state // with the help of previous state. for ( int i = 1; i < n; i++) { dp[i].A = dp[i  1].B; dp[i].B = (dp[i  1].A * (k  1)) + (dp[i  1].B * (k  2)); } cout << dp[n  1].A << endl; return ; } // Driver code int main() { int N = 5; int K = 3; int P = 2; int Q = 1; // Function call NumberOfWays(N, K, P, Q); } 
5
Time Complexity: O(N).
Recommended Posts:
 Number of ways to arrange N items under given constraints
 Count number of ways to arrange first N numbers
 Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
 Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
 Number of ways to split N as sum of K numbers from the given range
 Number of ways to arrange a word such that all vowels occur together
 Number of ways to arrange a word such that no vowels occur together
 Number of ways in which the substring in range [L, R] can be formed using characters out of the range
 Number of ways to arrange K different objects taking N objects at a time
 Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
 Count ways to build street under given constraints
 Ways to form a group from three groups with given constraints
 Arrange a binary string to get maximum value within a range of indices
 Number of ways to represent a number as sum of k fibonacci numbers
 Number of ways to get even sum by choosing three numbers from 1 to N
 Bell Numbers (Number of ways to Partition a Set)
 Number of ways to change the XOR of two numbers by swapping the bits
 Ways to arrange Balls such that adjacent balls are of different types
 Numbers that are not divisible by any number in the range [2, 10]
 Count of Numbers in Range where the number does not contain more than K non zero digits
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.