Given three integers N, M and X, the task is to find the number of ways to form an array, such that all consecutive numbers of the array are distinct, and the value at any index of the array from 2 to N – 1(Considering 1 based indexing) lies between 1 and M, while the value at index 1 is X and the value at index N is 1.
Note: Value of X lies between 1 and M.
Input: N = 4, M = 3, X = 2
The following arrays are possible:
1) 2, 1, 2, 1
2) 2, 1, 3, 1
3) 2, 3, 2, 1
Input: N = 2, M = 3, X = 2
The only possible array is: 2, 1
Approach: The problem can be solved using Dynamic Programming. Let, f(i) represent the number of ways to form the array till the ith index, such that every consecutive element of the array is distinct. Let f(i, One) represent the number of ways to form the array till the i-th index such that every consecutive element of the array is distinct and arri = 1.
Similarly, let f(i, Non-One) represent the number of ways to form the array till the ith index, such that every consecutive element of the array is distinct and arri is not equal to 1.
The following recurrence is formed:
f(i, Non-One) = f(i - 1, One) * (M - 1) + f(i - 1, Non-One) * (M - 2)
which means that the number of ways to form the array till the ith index with arrayi not equal to 1 is formed using two cases:
- If the number at arrayi – 1 is 1, then opt one number out of (M – 1) options to place at the ith index, since arrayi is not equal to 1.
- If the number at arrayi – 1 is not 1, then we need to opt one number out of (M – 2) options, since arrayi is not equal to 1 and arrayi ≠ arrayi – 1.
Similarly, f(i, One) = f(i – 1, Non-One), since the number of ways to form the array till the ith index with arrayi = 1, is same as number of ways to form the array till the (i – 1)th index with arrayi – 1 ≠ 1, thus at the ith index there is only one option. At the end the required answer if f(N, One) since arrayN needs to be equal to 1.
Below is the implementation of the above approach:
Time Complexity: O(N), where N is the size of the array
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Number of ways to form a heap with n distinct integers
- Find number of ways to form sets from N distinct things with no set of size A or B
- Minimize the cost to make all the adjacent elements distinct in an Array
- Number of distinct ways to represent a number as sum of K unique primes
- Count ways to generate N-length array with 0s, 1s, and 2s such that sum of all adjacent pairwise products is K
- Number of ways to paint a tree of N nodes with K distinct colors with given conditions
- Count the number of ways to fill K boxes with N distinct items
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Smallest length string with repeated replacement of two distinct adjacent
- Count of distinct permutations of length N having no similar adjacent characters
- Ways to arrange Balls such that adjacent balls are of different types
- Ways to paint stairs with two colors such that two adjacent are not yellow
- Ways to fill N positions using M colors such that there are exactly K pairs of adjacent different colors
- Ways to paint N paintings such that adjacent paintings don't have same colors
- Maximize sum of given array by rearranging array such that the difference between adjacent elements is atmost 1
- Count ways to partition a string such that both parts have equal distinct characters
- Count ways to split N! into two distinct co-prime factors
- Count ways to arrange N distinct objects if all clockwise arrangements are considered the same
- Count ways to form minimum product triplets
- Ways to form n/2 pairs such that difference of pairs is minimum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.